Skip to content

Implement cache optimization for regexp matching - #6486

Merged
mame merged 24 commits into
ruby:masterfrom
makenowjust:recache
Nov 9, 2022
Merged

Implement cache optimization for regexp matching#6486
mame merged 24 commits into
ruby:masterfrom
makenowjust:recache

Conversation

@makenowjust

Copy link
Copy Markdown
Contributor

No description provided.

@makenowjust
makenowjust marked this pull request as ready for review November 8, 2022 06:07
makenowjust and others added 7 commits November 8, 2022 15:13
```
regexec.c: In function ‘reset_match_cache’:
regexec.c:1259:56: warning: suggest parentheses around ‘-’ inside ‘<<’ [-Wparentheses]
 1259 |     match_cache[k1 >> 3] &= ((1 << (8 - (k2 & 7) - 1)) - 1 << ((k2 & 7) + 1)) | ((1 << (k1 & 7)) - 1);
      |                              ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
regexec.c:1269:60: warning: suggest parentheses around ‘-’ inside ‘<<’ [-Wparentheses]
 1269 |         match_cache[k2 >> 3] &= ((1 << (8 - (k2 & 7) - 1)) - 1 << ((k2 & 7) + 1));
      |                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
regexec.c: In function ‘find_cache_index_table’:
regexec.c:1192:11: warning: ‘m’ may be used uninitialized [-Wmaybe-uninitialized]
 1192 |   if (!(0 <= m && m < num_cache_table && table[m].addr == p)) {
      |         ~~^~~~
regexec.c: In function ‘match_at’:
regexec.c:1238:12: warning: ‘m1’ is used uninitialized [-Wuninitialized]
 1238 |   if (table[m1].addr < pbegin && m1 + 1 < num_cache_table) m1++;
      |            ^
regexec.c:1218:39: note: ‘m1’ was declared here
 1218 |   int l = 0, r = num_cache_table - 1, m1, m2;
      |                                       ^~
regexec.c:1239:12: warning: ‘m2’ is used uninitialized [-Wuninitialized]
 1239 |   if (table[m2].addr > pend && m2 - 1 > 0) m2--;
      |            ^
regexec.c:1218:43: note: ‘m2’ was declared here
 1218 |   int l = 0, r = num_cache_table - 1, m1, m2;
      |                                           ^~
```
Currently, the keys for CACHE_MATCH are handled as an `int` type. So we
should make sure the table size are smaller than the range of `int`.
@mame
mame merged commit 1dc4128 into ruby:master Nov 9, 2022
@casperisfine

Copy link
Copy Markdown
Contributor

Relaying here just in case, but I think this PR introduced a bug: https://bugs.ruby-lang.org/issues/19104#note-4

module Sass
  H        = /[0-9a-fA-F]/
  UNICODE  = /\\#{H}{1,6}[ \t\r\n\f]?/
  s = '\u{80}-\u{D7FF}\u{E000}-\u{FFFD}\u{10000}-\u{10FFFF}'
  NONASCII = /[#{s}]/
  ESCAPE   = /#{UNICODE}|\\[^0-9a-fA-F\r\n\f]/
  NMSTART  = /[_a-zA-Z]|#{NONASCII}|#{ESCAPE}/
  NMCHAR   = /[a-zA-Z0-9_-]|#{NONASCII}|#{ESCAPE}/
  VALID_UNIT = /#{NMSTART}#{NMCHAR}|%*/

  100_000.times do
    print '.'
    raise "WTF?" if "%" !~ VALID_UNIT
  end
end

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

module Sass
H = /[0-9a-fA-F]/
UNICODE = /\#{H}{1,6}[ \t\r\n\f]?/
s = '\u{80}-\u{D7FF}\u{E000}-\u{FFFD}\u{10000}-\u{10FFFF}'
NONASCII = /[#{s}]/
ESCAPE = /#{UNICODE}|\[^0-9a-fA-F\r\n\f]/
NMSTART = /[a-zA-Z]|#{NONASCII}|#{ESCAPE}/
NMCHAR = /[a-zA-Z0-9
-]|#{NONASCII}|#{ESCAPE}/
VALID_UNIT = /#{NMSTART}#{NMCHAR}|%*/

100_000.times do
print '.'
raise "WTF?" if "%" !~ VALID_UNIT
end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants