.
D 2017-05-06T04:23:21.813
J assignee nobody
J closer nobody
J cmimetype text/x-fossil-wiki
J comment Due\sto\sthe\srecent\s[https://research.swtch.com/glob]\swhich\spointed\sout\sthat\s<code>string\smatch</code>\swas\sexponential-time\swhere\sit\scould\sbe\slinear\s(See\sticket\s[a4a7f2abd376b280])\sI\ssemi-remembered\sthat\s<b>Aho-Corasick</b>\s\s(See\s[https://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_algorithm])\sis\sa\sfast\sstring\smatching\salgorithm\swhich\smatches\sa\sfinite\sset\sof\splain\sstrings\s<b>simultaneously</b>.\sExactly\swhat\sis\sneeded\sfor\s<code>string\smap</code>.\sAnd\swondered\sif\s<code>string\smap</code>\swas\susing\sit,\ssuspecting\sthat\sit\swould\snot.\r\n\r\nLooking\sat\sthe\scode\sin\s<code>tclCmdMZ.c</code>\sI\sconfirm\sthat\s<code>string\smap</code>\sdoes\snot\suse\sAC.\sIt\suses\sa\ssimple\snested\sloop\sover\sstring\spositions\sand\smap-keys\sto\slocate\swhere\sreplacements\sshould\shappen.\r\n\r\nI\spropose\sa\sspike\swhich\sre-implements\s<code>string\smap</code>\susing\sAC\sto\scompare\sperformance\swith\sthe\scurrent\simplementation\sbefore\scommitting\sto\sthe\schange.\r\n\r\nAs\sa\sside\snote,\sthe\scurrent\simplementation\sconverts\sthe\svarious\sstrings\sto\sUnicode\srep,\sand\sI\sdo\snot\ssee\swhy.\sOk,\sfor\s<code>-nocase</code>\swe\smay\sbe\sforced\sto\sat\sthe\smoment.\sBut\swithout\swe\scan\ssimply\smatch\son\sthe\sUTF-8\sbytes\sas\sthey\sare.\sDue\sto\sthe\sspecific\sleading\sbit-patterns\sfor\sthe\sfirst\sbyte\sof\sUTF-8\sencoded\schars\s(0,\s10,\s110,\s...)\swe\scannot\smatch\smisaligned.\r\n\r\nNote,\swith\sAC\swe\smight\sbe\sable\sto\shandle\s<code>-nocase</code>\sin\sUTF-8\sas\swell.\sSimply\sadd\sall\sstring\svariants\sto\sthe\strie/state\smachine.\sI.e.\sexpand\s<code>x</code>\sin\sa\spattern\sto\seffectively\s<code><nowiki>[xX]</nowiki></code>\swhen\sextending\sAC's\sdata\sstructures.\sWhile\sthe\snumber\sof\svariants\sis\sO(2^n)\sin\sthe\slength\sof\sthe\spattern\sthe\sAC\strie\smight\skeep\sfrom\sbeing\sexponential\s(parallel\stransitions\sin\sa\slinear\ssequence\sof\sstates).\r\n\r\nWith\sthat\swe\swould\salso\sbe\srid\sof\sconversion\stimes.\r\n\r\nThe\sbig\sissue,\swhich\sneeds\smeasuring,\sis\sthe\stime\sneeded\sto\scompute\sthe\sAC\sdata\sstructures.\sAnd\show\smuch\swe\scan\samortize\sby\susing\sa\scustom\sint-rep\sfor\sthe\smap\sargument\sof\s`string\smap`\swhich\ssaves\sit.\sOr\salternatively,\sa\sglobal\shash-table\sof\srecent\smaps\s(we\sneed\sonly\skey\sthat\sby\sthe\smap-keys,\sthe\sreplacements\sdo\snot\smatter\sfor\sthe\ssearch\sin\shash\stable\sand\sstring\sto\sprocess)\r\n\r\nAs\sa(nother)\sside\snote,\sperformance\sboosts\shere\swill\shelp\ssimple\stemplating\ssystems\sbased\son\sfixed\sstrings\sfor\splace-holders.\r\n\r\nOverall\sI\sconsider\sthis\s"Important".
J is_private 0
J login aku
J priority 5\sMedium
J resolution None
J severity Important
J status Open
J submitter aku
J subsystem 18.\sCommands\sM-Z
J title string\smap\s-\sInvestigate\sAho-Corasick\sas\spossible\sspeed\sbooster
J type Bug
K 268108fc93e2869e890f2b5e8f9668bca5c3f24a
U aku
Z eaeb01480bb6a86d898f543bc2863bca