Overview
| Artifact ID: | 9d422502463b8b2b9444566213427c9cb62826b0 |
|---|---|
| Ticket: | 268108fc93e2869e890f2b5e8f9668bca5c3f24a
string map - Investigate Aho-Corasick as possible speed booster |
| User & Date: | aku on 2017-05-06 04:23:21 |
Changes
- assignee changed to: "nobody"
- closer changed to: "nobody"
- cmimetype changed to: "text/x-fossil-wiki"
- comment changed to:
Due to the recent [https://research.swtch.com/glob] which pointed out that <code>string match</code> was exponential-time where it could be linear (See ticket [a4a7f2abd376b280]) I semi-remembered that <b>Aho-Corasick</b> (See [https://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_algorithm]) is a fast string matching algorithm which matches a finite set of plain strings <b>simultaneously</b>. Exactly what is needed for <code>string map</code>. And wondered if <code>string map</code> was using it, suspecting that it would not. Looking at the code in <code>tclCmdMZ.c</code> I confirm that <code>string map</code> does not use AC. It uses a simple nested loop over string positions and map-keys to locate where replacements should happen. I propose a spike which re-implements <code>string map</code> using AC to compare performance with the current implementation before committing to the change. As a side note, the current implementation converts the various strings to Unicode rep, and I do not see why. Ok, for <code>-nocase</code> we may be forced to at the moment. But without we can simply match on the UTF-8 bytes as they are. Due to the specific leading bit-patterns for the first byte of UTF-8 encoded chars (0, 10, 110, ...) we cannot match misaligned. Note, with AC we might be able to handle <code>-nocase</code> in UTF-8 as well. Simply add all string variants to the trie/state machine. I.e. expand <code>x</code> in a pattern to effectively <code><nowiki>[xX]</nowiki></code> when extending AC's data structures. While the number of variants is O(2^n) in the length of the pattern the AC trie might keep from being exponential (parallel transitions in a linear sequence of states). With that we would also be rid of conversion times. The big issue, which needs measuring, is the time needed to compute the AC data structures. And how much we can amortize by using a custom int-rep for the map argument of `string map` which saves it. Or alternatively, a global hash-table of recent maps (we need only key that by the map-keys, the replacements do not matter for the search in hash table and string to process) As a(nother) side note, performance boosts here will help simple templating systems based on fixed strings for place-holders. Overall I consider this "Important".
- is_private changed to: "0"
- login: "aku"
- priority changed to: "5 Medium"
- resolution changed to: "None"
- severity changed to: "Important"
- status changed to: "Open"
- submitter changed to: "aku"
- subsystem changed to: "18. Commands M-Z"
- title changed to:
string map - Investigate Aho-Corasick as possible speed booster
- type changed to: "Bug"