-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡✅ Update all net-imap regexps to run in O(n)
The BIDI_FAILURE regexps all used `\g<name>` to define char classes and then re-use them. Unfortunately, ruby 3.2 can't compile that to run in linear time. The regexps could also be written using lookahead, but that also wouldn't run in linear time. The debug output gsub is simple to accomplish without negative lookahead by using a block with gsub and checking `$'.empty?`. `bin/check-regexps` was added for quick double-check, just in case the tests aren't catching everything.
- Loading branch information
Showing
6 changed files
with
43 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
# See also: test/net/imap/regexp_collector.rb | ||
# See also: test/net/imap/test_regexps.rb | ||
|
||
def traverse(m=Object, s=Set.new, &b) | ||
m.constants(false).map{m.const_get _1 rescue nil}.select{_1 in Module}.each do | ||
next if s.include?(_1); s << _1 | ||
b and b[_1] | ||
traverse(_1, s, &b) | ||
end | ||
end | ||
|
||
def collect_regexps = ObjectSpace | ||
.each_object(Regexp) | ||
.reject{Regexp.linear_time? _1} | ||
|
||
2.times{traverse} | ||
before = collect_regexps | ||
|
||
$LOAD_PATH.unshift "./lib" | ||
require 'net/imap' | ||
2.times{traverse} | ||
traverse(Net::IMAP) { puts _1.name } | ||
after = collect_regexps - before | ||
p before: before.count, count: after.count, after:; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.