Repository navigation
Avoid quadratic backtracking in numeric prefix validation - #140
Conversation
|
@taketo1113 thanks for merging the recent PRs and keeping the gem moving — much appreciated. Could you take a look at this one when you get a chance? Locally: 43 tests, 657 assertions, 0 failures on Ruby 3.1.2 (the engine without regexp memoization); the new guard takes ~8 s on the old pattern and ~0.3 s with the fix. |
|
@imoshkinlocal Thanks. Could you run benchmarks before and after the change on recent Ruby versions, such as Ruby 4.0? |
Benchmarks: before vs afterThanks for the trust, @taketo1113 — here are the before/after numbers on 3.4.13 and
Setup
Invalid prefix (the quadratic case)
Fitted exponent of
Raw regexp and hot paths
For Verdict
If you'd rather merge without test code, I can remove Minimal reproducer# ruby -Ilib/path repro.rb (min of 3 runs, after a warm-up)
require "ipaddr"
def elapsed(n)
addr = "1.2.3.4/" + "1" * n + "x"
t = Process.clock_gettime(Process::CLOCK_MONOTONIC)
begin
IPAddr.new(addr)
rescue IPAddr::Error
end
Process.clock_gettime(Process::CLOCK_MONOTONIC) - t
end
elapsed(100) # warm-up
[1_000, 4_000, 16_000, 64_000].each do |n|
printf "n=%-6d %.6f s\n", n, 1.upto(3).map { elapsed(n) }.min
endOn this machine that prints: |
[1-9]+ and \d* overlap in the numeric-prefix branch of IPAddr#mask!, so an invalid prefix such as "1" * 32000 + "x" can be split between them in O(n) ways and the engine backtracks through all of them before giving up. Both IPAddr.new and IPAddr#prefix= go through mask!. [1-9]\d* accepts the same strings without giving the engine a choice of where to split.
b1a23eb to
31e1e6b
Compare
taketo1113
left a comment
There was a problem hiding this comment.
Thank you for checking.
There don't seem to be any negative impacts on Ruby 4.0 or other versions, so I will merge it.
[1-9]+ and \d* overlap in the numeric-prefix branch of
IPAddr#mask!, so aninvalid prefix such as
"1" * 32_000 + "x"can be split between the twoquantifiers in O(n) ways and the engine backtracks through all of them before
giving up. Both
IPAddr.new("1.2.3.4/...")andIPAddr#prefix=go throughmask!.On Ruby 3.1.2 (regexp engine without memoization):
[1-9]\d*accepts exactly the same language — the first character must be1-9, the rest digits — without leaving the engine a choice of where to split,
so there is nothing to backtrack into. The sibling
/\A\d+\z/used for theleading-zero check has no overlapping quantifiers and is left as is.
Test
test_mask_long_invalid_prefixusesassert_linear_performance(availablethrough
test/lib/helper.rbandtest-unit-ruby-core) over[5, 10, 20, 40, 80], i.e. prefixes of 2 500 to 40 000 digits. Measured onRuby 3.1.2:
Full suite: 43 tests, 657 assertions, 0 failures.
Note for reviewers
On Ruby >= 3.2 the regexp engine memoizes matches, so the difference is not
observable there and the timing guard passes trivially. The gem still declares
required_ruby_version >= 2.4, so the quadratic behaviour is shipped to Ruby2.4-3.1 users, including several LTS distributions; the test is a regression
guard for engines without memoization.