Skip to content

Commit 1ff72ce

Browse files
committed
Optimize control char stripping
With the introduction of `Regexp.timeout`, which is set to 1s by default in Rails 8.0, we ran into the control char stripping `gsub` sometimes timing out. This can be done much more efficiently with `String#tr_s`, it's about 3x faster and has guaranteed linear performance.
1 parent f152a37 commit 1ff72ce

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

lib/mutations/string_filter.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ class StringFilter < AdditionalFilter
1414
:allow_control_characters => false # false removes control characters from the string
1515
}
1616

17+
CONTROL_CHARS = ((0..31).map(&:chr) - ["\r", "\n", "\t"]).join.freeze
18+
REPLACE = (" " * CONTROL_CHARS.bytesize).freeze
19+
1720
def filter(data)
1821
# Handle nil case
1922
if data.nil?
@@ -28,7 +31,7 @@ def filter(data)
2831
return [data, :string] unless data.is_a?(String)
2932

3033
# At this point, data is a string. Now remove control characters from the string:
31-
data = data.gsub(/((?=[[:cntrl:]])[^\t\r\n])+/, ' ') unless options[:allow_control_characters]
34+
data = data.tr_s(CONTROL_CHARS, REPLACE) unless options[:allow_control_characters]
3235

3336
# Transform it using strip:
3437
data = data.strip if options[:strip]

0 commit comments

Comments
 (0)