Skip to content

Commit

Permalink
Changed to frozen_string_literal: true.
Browse files Browse the repository at this point in the history
## Why?
Because `s.check("a")` is slower than `s.check("a".freeze)`.

- benchmark/stringscan_2.yaml
```
loop_count: 100000
contexts:
  - name: No YJIT
    prelude: |
      $LOAD_PATH.unshift(File.expand_path("lib"))
      require 'rexml'

prelude: |
  require 'strscan'
  s = StringScanner.new('abcdefg hijklmn opqrstu vwxyz')
  ptn = "a"
benchmark:
  'check("a")'            : s.check("a")
  'check("a".freeze)'     : s.check("a".freeze)
  'ptn="a";s.check(ptn)'  : |
    ptn="a"
    s.check(ptn)
  'check(ptn)'            : s.check(ptn)
```

```
$benchmark-driver benchmark/stringscan_2.yaml
Comparison:
          check(ptn):  13524479.4 i/s
   check("a".freeze):  13433638.1 i/s - 1.01x  slower
          check("a"):  10231225.8 i/s - 1.32x  slower
ptn="a";s.check(ptn):  10013017.0 i/s - 1.35x  slower
```
  • Loading branch information
naitoh committed Feb 24, 2024
1 parent fb7ba27 commit fd8ef89
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions lib/rexml/parsers/baseparser.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: false
# frozen_string_literal: true
require_relative '../parseexception'
require_relative '../undefinednamespaceexception'
require_relative '../source'
Expand Down Expand Up @@ -462,8 +462,7 @@ def normalize( input, entities=nil, entity_filter=nil )

# Unescapes all possible entities
def unnormalize( string, entities=nil, filter=nil )
rv = string.clone
rv.gsub!( /\r\n?/, "\n" )
rv = string.gsub( /\r\n?/, "\n" )
matches = rv.scan( REFERENCE_RE )
return rv if matches.size == 0
rv.gsub!( /&#0*((?:\d+)|(?:x[a-fA-F0-9]+));/ ) {
Expand Down

0 comments on commit fd8ef89

Please sign in to comment.