foundation icon indicating copy to clipboard operation
foundation copied to clipboard

Optimise and benchmark UArray.replace

Open adinapoli opened this issue 8 years ago • 2 comments

Following up from #314, we should now:

  • Benchmark replace against text and bytestring to see how much we are lagging behind;
  • Optimise replace both 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 indices but rather using something like isInfixOfAt :: UArray -> UArray -> Offset -> Bool to check whether the first UArray contains 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)

adinapoli avatar Jun 13 '17 14:06 adinapoli

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.

ndmitchell avatar Jun 13 '17 20:06 ndmitchell

Agreed, thanks for the pointer!

adinapoli avatar Jun 14 '17 08:06 adinapoli