Actions
Patch #44734
closedUse String#scrub to replace invalid UTF-8 byte sequences in Redmine::CodesetUtil.replace_invalid_utf8
Description
The attached patch improves the performance of Redmine::CodesetUtil.replace_invalid_utf8 by reimplementing it with String#scrub.
The method currently replaces invalid byte sequences with "?" by transcoding the string to UTF-16LE and back to UTF-8. This is a workaround from the time before String#scrub was added in Ruby 2.1:
str = str.encode("UTF-16LE", :invalid => :replace,
:undef => :replace, :replace => '?').encode("UTF-8")
String#scrub does the same thing in a single pass:
str.dup.force_encoding('UTF-8').scrub('?')
On Ruby 3.3, both implementations returned the same result for 200,000 random strings containing various invalid byte sequences. String#scrub was about 7 times faster: 0.05 s compared with 0.36 s for 2,000 iterations on a 30KB string.
Files
Updated by Go MAEDA about 22 hours ago
- Status changed from New to Closed
- Assignee set to Go MAEDA
- Target version changed from Candidate for next major release to 7.1.0
Committed the patch in r25453.
Actions