Page 1 of 1
Size of printf
Posted: Sat Aug 06, 2011 6:26 am
by mark3094
Hi,
I have a few functions in my mini C library. These include printf, strlen, strcat, strncat, putchar and int/hex/oct to ASCII.
Most of these functions are very small, however, my printf has been built based on the C99 standard, and it took nearly 600 lines of code to complete.
Does this sound excessive?
When I compile to a static library, it is 47KB without optimisation.
Just wondering if this sounds normal, or too large? I imagine it will push up the size of my kernel, but maybe that's fine.
Any experience that anyone can add to this would be great.
Re: Size of printf
Posted: Sat Aug 06, 2011 6:54 am
by bluemoon
I don't see any immediate need to optimize printf, in size or in speed; until your kernel is very mature.
FYI, my test application linked with newlib is 14764 bytes:
Code: Select all
#include <stdio.h>
int main ( int argc, char *argv[] ) {
printf ("Hello from user application!\n");
return 0xBADC0DE;
}
Re: Size of printf
Posted: Sat Aug 06, 2011 7:26 am
by mark3094
Thankyou
No, my Kernel is non-existant. I've been spending my time building the supporting functions.
I won't worry about the size for now. At least not until it becomes an issue.
Re: Size of printf
Posted: Sat Aug 06, 2011 7:34 am
by Combuster
my local printf backend is 567 lines without float support, so your number sounds about right. when summing the size of my strlen/str(n)cat + strto* + print (courtesy of pdclib) I end up with 17k of object files at -O0 and 15k at -O2, printf and dependencies being responsible for 8k/7k respectively so you might want to compare the designs due to the factor 3 difference observed.
Bluemoon's output seems to be in a similar league, with 14k of output code (which has a printf and its dependencies, plus a main, and probably including a runtime as well)
Bluemoon also mentioned the other important point: don't optimize prematurely. You are likely to introduce bugs that way.
Re: Size of printf
Posted: Sat Aug 06, 2011 7:37 am
by gerryg400
47k sounds large. My printf driver, which admittedly is not fully featured, is 2k.
Re: Size of printf
Posted: Sat Aug 06, 2011 5:43 pm
by mark3094
Is there any reasonable way to begin troubleshooting why the output file is so large?
The printf.obj file is 23KB, and the printf.asm file is 90KB (both of these created by the compiler).
The method I'm using for printf involves making a string in memory, based on the parameters passed to printf, then writing the string to screen with putchar.
Could that be the problem?
Or maybe I should create a cut down version of printf for Kernel use only, which doesn't support floating point, long long integers, etc?