Patch #31059

Use #b shortcut instead of #force_encoding

Added by Pavel Rosický about 1 month ago. Updated about 1 month ago.

Status:ClosedStart date:
Priority:NormalDue date:
Assignee:Go MAEDA% Done:

0%

Category:Code cleanup/refactoring
Target version:4.1.0

Description

follow up for #26561

this patch replaces all .force_encoding('ASCII-8BIT') or .force_encoding('BINARY') to .b

String#b is available since Ruby 2.0
https://www.rubydoc.info/stdlib/core/2.0.0/String%3ab

#b returns unfrozen string so + can be omited, this way the code can be much shorter and it's even faster

# frozen-string-literal: true

require 'benchmark/ips'

def test_b
  'looooonghsalfhfslakfsalhsfalhfsalhfsalhfsa'.b
end

def test_force
  (+"looooonghsalfhfslakfsalhsfalhfsalhfsalhfsa").force_encoding('ASCII-8BIT')
end

Benchmark.ips do |x|
  x.report('b', 'test_b')
  x.report('force', 'test_force')
end
Warming up --------------------------------------
                   b   278.357k i/100ms
               force   191.903k i/100ms
Calculating -------------------------------------
                   b      8.628M (± 6.3%) i/s -     43.145M in   5.023695s
               force      3.888M (± 7.9%) i/s -     19.382M in   5.021761s

core.patch Magnifier (6.54 KB) Pavel Rosický, 2019-03-19 18:39

tests.patch Magnifier (7.77 KB) Pavel Rosický, 2019-03-19 18:39

core.patch Magnifier - v2 (6.51 KB) Pavel Rosický, 2019-03-19 23:45

tests.patch Magnifier - v2 (7.77 KB) Pavel Rosický, 2019-03-19 23:45


Related issues

Related to Redmine - Feature #26561: Enable frozen string literals Closed

Associated revisions

Revision 17992
Added by Go MAEDA about 1 month ago

Use #b shortcut instead of #force_encoding (#31059).

Patch by Pavel Rosický.

History

#1 Updated by Go MAEDA about 1 month ago

I think you can remove #dup placed before #b because #b returns copied string.

The attached patch:

str = str.dup.b

proposing change:

str = str.b

#2 Updated by Pavel Rosický about 1 month ago

fixed, thanks

#3 Updated by Go MAEDA about 1 month ago

  • Target version set to 4.1.0

#4 Updated by Go MAEDA about 1 month ago

#5 Updated by Go MAEDA about 1 month ago

  • Status changed from New to Closed
  • Assignee set to Go MAEDA

Committed the patches. Thank you for improving Redmine.

#6 Updated by Go MAEDA about 1 month ago

  • Subject changed from use #b shortcut instead of #force_encoding to Use #b shortcut instead of #force_encoding

Also available in: Atom PDF