.
D 2010-02-25T17:07:34
J assignee nijtmans
J cmimetype text/plain
J comment The\souster-hash\sfunction\s(HashStringKey\sin\stclHash.c,\nand\stwo\svariants\sin\sother\splaces)\scan\sbe\sspeed\sup\nfurther.\sHere\sis\smy\ssuggestion,\swhich\sis\s5\sassembler\ninstructions\sfaster\sfor\severy\sfunction\scall.\sFor\nan\sexplanation,\ssee\sbelow\s(read\son).\n\n================\snew\s====================\nif\s((result\s=\sUCHAR(*string))\s!=\s0)\s{\n\s\s\s\swhile\s((c\s=\s*++string)\s!=\s0)\s{\n\s\s\s\s\s\s\s\sresult\s+=\s(result\s<<\s3)\s+\sUCHAR(c);\n\s\s\s\s}\n}\nreturn\sresult;\n================\sold\s=====================\nfor\s(;\s(c=*string++)\s!=\s0\s;)\s{\n\s\s\s\sresult\s+=\s(result<<3)\s+\sUCHAR(c);\n}\nreturn\sresult;\n===========================================\n\nThere\sis\sno\sdifference\sin\susing\sa\sfor\sloop\sand\na\swhile\sloop.\sIt's\sexactly\sthe\ssame,\sapart\sfrom\npersonal\spreference...\n\nThe\sfirst\sloop\sentry,\sresult\sstarts\sbeing\s0,\swe\swill\nshift\sthe\s0\svalue\s3\sbits,\sresulting\sin\s0.\sThen\swe\nare\sadding\s0\sto\sUCHAR(c),\sresulting\sin\sUCHAR(c).\nThe\squestion\sis,\swhen\swe\salready\sknow\sthat\sthe\nresult\safter\sthe\sfirst\sshift\sand\saddition\swill\sbe\nUCHAR(c),\swhy\sdo\swe\sdo\sthe\sshift\sand\sthe\saddition\nat\sall?\n\nIn\sthe\slast\sloop\sentry,\sthe\spointer\sis\sincremented,\nbut\sthe\sincremented\spointer\sis\snot\saccessed\sany\smore.\nSo,\sa\slittle\sre-ordering\scould\sprevent\sthis\sincrement.\nThat's\show\sI\scame\sup\swith\sthe\sfaster\scode\sabove.\n\nHow\smuch\sfaster\sis\sit?\sHere\sis\sthe\sgenerated\si86\sassembler\ncode\sfor\sboth\sversions,\susing\sgcc-4.4.\n================\snew\s====================\nHashStringKey:\npushl%ebp\nmovl%esp,\s%ebp\nmovl12(%ebp),\s%edx\nmovzbl(%edx),\s%eax\ntestl%eax,\s%eax\nje.L32\nleal1(%edx),\s%ecx\nmovzbl1(%edx),\s%edx\ntestb%dl,\s%dl\nje.L32\n.L35:\nmovzbl%dl,\s%edx\naddl$1,\s%ecx\nleal(%edx,%eax,8),\s%edx\naddl%edx,\s%eax\nmovzbl(%ecx),\s%edx\ntestb%dl,\s%dl\njne.L35\n.L32:\npopl%ebp\nret\n================\sold\s=====================\nHashStringKey:\npushl%ebp\nxorl%eax,\s%eax\nmovl%esp,\s%ebp\nmovl12(%ebp),\s%ecx\nmovzbl(%ecx),\s%edx\ntestb%dl,\s%dl\nje.L33\naddl$1,\s%ecx\n.L34:\nmovzbl%dl,\s%edx\nleal(%edx,%eax,8),\s%edx\naddl%edx,\s%eax\nmovzbl(%ecx),\s%edx\naddl$1,\s%ecx\ntestb%dl,\s%dl\njne.L34\n.L33:\npopl%ebp\nret\n===========================================\n\nZo,\sthe\smain\sloop\shas\s7\sinstructions,\sonly\sin\na\sdifferent\sorder.\sThe\spreparation\shas\s10\ninstructions,\sagainst\s8\sbefore,\sbut\sthe\sloop\nhas\sto\sbe\srun\sone\sless\stime!\sSo,\swe\sgain\s5\ninstructions!\sUsing\sanother\sprocessor,\sthe\nresult\swill\sbe\sdifferent,\sbut\sfact\sis\sthat\nwe\ssave\sa\sshift,\san\saddition\sand\san\sincrement.\n\nAnyone,\swho\scan\sdo\sbetter?\n
J foundin None
J is_private 0
J priority 3\sLow
J resolution None
J status Open
J submitter nijtmans
J subsystem 11.\sConversions\sFrom\sString
J title Further\sspeed-up\sof\souster-hash\sfunction
J type RFE
K 2958832fffffffffffffffffffffffffffffffff
U nijtmans
Z 3302a1a92994603a4de11ff7ec60f194