dashbitco / nimble_parsec

A simple and fast library for text-based parser combinators

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[proposal] Improve integer parsing

dkuku opened this issue · comments

Currently to parse a year the function generated looks like this (copied from the example in readme):

  def nimble(<<x0, x1, x2, x3>>)
      when x0 >= 48 and x0 <= 57 and (x1 >= 48 and x1 <= 57) and
             (x2 >= 48 and x2 <= 57) and (x3 >= 48 and x3 <= 57) do
    (x3 - 48) * 1 + (x2 - 48) * 10 + (x1 - 48) * 100 + (x0 - 48) * 1000
  end
end

This can be improved by matching only on the part of the bit that stores the integer

  def parse_new(<<3::4, x0::4, 3::4, x1::4, 3::4, x2::4, 3::4, x3::4>>)
      when x0 < 10 and x1 < 10 and x2 < 10 and x3 < 10 do
    x3 + x2 * 10 + x1 * 100 + x0 * 1000
  end

With this example I have half the memory usage and 2x speedup in benchee.

Ok nvm - I think I used wrong functions in my benchmark - I had it wrong way around with recursive implementation - was late when I was testing it 😴 .
It's comparable. It may be few percent faster when we are sure these are integers and remove the guards.

livebook - I think this can still gain 5% improvement:
https://gist.github.com/dkuku/9004bbf92cb3b81e8ed6fe211b9c7fc6