performance overhead of the gettext internationalization system in C/C++ -


i worked through documentation of http://www.gnu.org/software/gettext/manual/gettext.html , there no discussion @ performance overhead. on internet, found performance discussions other languages (php , java) nothing c/c++.

therefore questions:

  1. what performance overhead during startup of program uses gettext (load shared library? how translations loaded memory? translations loaded on startup or on-demand?)

  2. what performance penalty during normal operation of program? (i.e. when translation needed) how large increased memory footprint of program , how memory organized? there higher danger/possibility parts of program swapped disk when program idle? (if translations stored in different part of memory rest of program, in understanding chance of page fault higher compared un-internationalized version of program)

  3. does program runs under "c"-locale suffer these performance penalties?

thanks lot.

given alternative approach have large number of builds, each in it:

int main() {     printf( #ifdef swedish            "hej världen\n" #elsif english            "hello, world\n" #elsif portuguese            "olá, mundo\n" #else      #error language not specified.  #endif     );     return 0l; } 

instead get:

int main() {    printf(gettext("hello, world\n"));  } 

which easy read , understand.

i don't know exact structure of gettext implementation, expect hash-table once it's loaded. possibly binary tree, hash-table seems more sensible.

as exact overheads, it's hard put number on - especially, say, if swapped disk, , disk has stopped, takes 3-4 seconds disk speed. how quantify that? yes, it's possible page needed gettext swapped out if system has been busy doing memory intensive.

loading message file should large overhead if file large, again, if disk not spinning, , file not cached, there overhead of several seconds. again, how quantify that. size of file directly proportional actual size of translated (or native language) messages.

regarding point 2:

as far know, in both linux , windows, pages swapped out on "least used" (or other usage statistical) basis, has nothing located. translated messages in different place actual code - there isn't list of 15 different translations in source file, translations loaded @ runtime, , located in different place code itself. however, overhead of similar overhead difference between:

static const char *msg = "hello, world\n"; 

and

static const char *msg = strdup("hello, world\n");  

given text-strings kept in binary of program anyway, don't think "nearness" executing code different dynamically allocated piece of memory somewhere in heap. if call gettext function enough, memory kept "current" , not swapped out. if don't call gettext time, may swapped out. applies "none of strings stored in executable have been used recently, got swapped out".

3) think english (or "no language selected") treated identical other language variant.

i have little further dig in bit, need breakfast first...

very unscientific:

#include <libintl.h> #include <cstdio> #include <cstring>  static __inline__ unsigned long long rdtsc(void) {     unsigned hi, lo;     __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));     return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 ); }   int main() {     char str[10000] = {};     char *s = str;     unsigned long long time;      for(int = 0; < 10; i++)     {     time = rdtsc();     s += sprintf(s, "hello, world %d", i);     time = rdtsc() - time;     printf("time =%lld\n", time);     }     printf("s = %s\n", str);     s = str;      strcpy(s, "");     for(int = 0; < 10; i++)     {     time = rdtsc();     s += sprintf(s, gettext("hello, world %d"), i);     time = rdtsc() - time;     printf("time =%lld\n", time);     }     printf("s = %s\n", str); } 

gives following results:

$ g++ -wall -o2 intl.cpp $ ./a.out time =138647 time =9528 time =6710 time =5537 time =5785 time =5427 time =5406 time =5453 time =5644 time =5431 s = hello, world 0hello, world 1hello, world 2hello, world 3hello, world 4hello, world 5hello, world 6hello, world 7hello, world 8hello, world 9 time =85965 time =11929 time =10123 time =10226 time =10628 time =9613 time =9515 time =9336 time =9440 time =9095 s = hello, world 0hello, world 1hello, world 2hello, world 3hello, world 4hello, world 5hello, world 6hello, world 7hello, world 8hello, world 9 

the code in dcigettext.c uses mixture of binary search in flat array of strings, , hash function hashes string pjw hash (see : http://www.cs.hmc.edu/~geoff/classes/hmc.cs070.200101/homework10/hashfuncs.html ).

so, overhead, once application has started, appears around "just noticeable" (when counting clockcycles), not enormous.

the exact time takes run first sprintf varying in both cases, wouldn't "using gettext" makes sprintf faster on first call - "bad luck" on run (i had few other variants of code, , vary on first call sprintf, , less later calls). setup (possibly caches [printf causing caches overwritten other garbage quite likely], branch prediction, etc) somewhere takes time...

now, doesn't answer questions on paging out, etc. , didn't try make swedish, portuguese or german translation of "hello, world" message. still believe it's not huge, unless indeed running 100s of instantiations of application per second, , application doesn't other print message screen after doing simple calculations, sure, important.

the real way find out how difference makes compile same applicaion #define _(x) x instead of #define _(x) gettext(x), , see if notice difference.

i still think "paged out" red herring. if machine under high memory pressure, running slow no matter (if write piece of code allocates 16gb [i have 16gb ram in machine] on machine, except keyboard (can blink num-lock led) , mouse pointer (can move mouse pointer around on screen) goes unresponsive).


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -