foundation
foundation copied to clipboard
Optimise and benchmark UArray.replace
Following up from #314, we should now:
- Benchmark
replaceagainsttextandbytestringto see how much we are lagging behind; - Optimise
replaceboth in terms of memory and time.
On the second point there are several low-hanging fruit:
- for one, we shouldn't create the intermediate array inside
indicesbut rather using something likeisInfixOfAt :: UArray -> UArray -> Offset -> Boolto check whether the firstUArraycontains exactly the same elements of the second starting at the given offset; - We can probably optimise the case where we search & replace a single character;
- More "exotic" improvements like using a slightly different algorithm for indices searching ( @ndmitchell mentioned one at the ZuriHac which traverses the array backward, but I cannot remember the name)
The algorithm for fast indices is at https://github.com/bos/text/blob/master/Data/Text/Internal/Search.hs, which includes the references at the top. However, I'd just translate the code in that module directly - you're unlikely to do better. That code has a fast-case for a 1 character string, and if you have that, no need for isInfixOfAt.
Agreed, thanks for the pointer!