Skip to content

Commit 121633d

Browse files
authored
Hoist entity name regexp in Text.check (#391)
`Text.check` compiled the regexp `/\A#{Entity::NAME}\z/um` for each named entity reference. Now the regexp is a constant. The regexp is the same, so the output does not change. | Input | Ruby 4.0 | 4.0 + YJIT | Ruby 2.6 | JRuby 10.1 | |---|---:|---:|---:|---:| | Text with predefined entities | -86% | -89% | -77% | -64% | | Attribute values with entities | -74% | -82% | -58% | -47% | Documents without named entity references do not use the changed line. I measured no slowdown on them. I used 20 interleaved rounds and a Mann-Whitney test (Apple M5). Checks: - `ruby test/run.rb` passes on Ruby 2.6, Ruby 4.0 and JRuby. - Old and new outputs matched on 1,000,000 random documents.
1 parent 5305ed3 commit 121633d

1 file changed

Lines changed: 2 additions & 1 deletion

File tree

‎lib/rexml/text.rb‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Text < Child
2121
attr_accessor :raw
2222

2323
NEEDS_A_SECOND_CHECK = /(<|&((#{Entity::NAME});|(#0*((?:\d+)|(?:x[a-fA-F0-9]+)));)?)/um
24+
ENTITY_NAME_PATTERN = /\A#{Entity::NAME}\z/um
2425
NUMERICENTITY = /&#0*((?:\d+)|(?:x[a-fA-F0-9]+));/
2526
VALID_CHAR = [
2627
0x9, 0xA, 0xD,
@@ -157,7 +158,7 @@ def Text.check string, pattern, doctype = nil
157158
else
158159
raise "Illegal character #{string.inspect} in raw string #{string.inspect}"
159160
end
160-
elsif !(/\A#{Entity::NAME}\z/um.match?(value))
161+
elsif !(ENTITY_NAME_PATTERN.match?(value))
161162
raise "Illegal character \"#{string[index]}\" in raw string #{string.inspect}"
162163
end
163164

0 commit comments

Comments
 (0)