Len7hir
2010-Aug-10 09:30 UTC
[Dovecot] Why p_strdup and other string functions uses loops instead strlen? (dovecot 2.0.rc4)
I did Dovecot profiling on huge e-mail system, and p_strdup was very high on list. I do minor change: p_strdup: /* for (len = 0; (str)[len] != '\0'; ) len++; len++; */ len = strlen(str) + 1; p_strndup: /* len = 0; while (len < max_chars && ((const char *) str)[len] != '\0') len++; */ len = strnlen(str, max_chars); And after changes strdup drop down on the profile list. This is output from oprofile: samples % symbol name 28000 10.9764 p_strdup 8507 3.3349 safe_memset 7857 3.0801 .plt 7627 2.9899 buffer_write 6871 2.6935 parse_body_add_block And after (about four times smaller samples, but shows everything): samples % symbol name 9595 26.2625 parse_next_body_to_boundary 8247 22.5729 parse_body_add_block 772 2.1130 .plt 672 1.8393 buffer_write 658 1.8010 __i686.get_pc_thunk.bx 614 1.6806 safe_memset 586 1.6039 pool_alloconly_malloc 559 1.5300 p_strdup 533 1.4589 hash_table_insert_node I wonder why You use loops instead strlen, which is optimalised on every platforms. Redgards, Len7hir
Timo Sirainen
2010-Aug-10 12:08 UTC
[Dovecot] Why p_strdup and other string functions uses loops instead strlen? (dovecot 2.0.rc4)
On 10.8.2010, at 10.30, Len7hir wrote:> I did Dovecot profiling on huge e-mail system, and p_strdup was very high on list. > > I do minor change: > p_strdup: > /* > for (len = 0; (str)[len] != '\0'; ) > len++; > len++; > */ > len = strlen(str) + 1;Committed: http://hg.dovecot.org/dovecot-2.0/rev/7f550a7bd9d7> p_strndup: > /* > len = 0; > while (len < max_chars && ((const char *) str)[len] != '\0') > len++; > */ > len = strnlen(str, max_chars);strnlen() exists only in Linux and FreeBSD, so if this change is made it has to be behind #ifdef HAVE_STRNLEN. But if it was only p_strdup() that was on the profile listing, it's a bit too much trouble to add support for it.> I wonder why You use loops instead strlen, which is optimalised on every platforms.No reason. I guess the excuse is that it was written 8-9 years ago.