プロジェクト

全般

プロフィール

Vote #79923

完了

Use #b shortcut instead of #force_encoding

Admin Redmine さんが約4年前に追加. 約4年前に更新.

ステータス:
Closed
優先度:
通常
担当者:
-
カテゴリ:
Code cleanup/refactoring_30
対象バージョン:
開始日:
2022/05/09
期日:
進捗率:

0%

予定工数:
category_id:
30
version_id:
127
issue_org_id:
31059
author_id:
123153
assigned_to_id:
332
comments:
6
status_id:
5
tracker_id:
3
plus1:
0
affected_version:
closed_on:
affected_version_id:
ステータス-->[Closed]

説明

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

journals

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

The attached patch:
<pre>
str = str.dup.b
</pre>

proposing change:
<pre>
str = str.b
</pre>

--------------------------------------------------------------------------------
fixed, thanks
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
Committed the patches. Thank you for improving Redmine.
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------


related_issues

relates,Closed,26561,Enable frozen string literals

Admin Redmine さんが約4年前に更新

  • カテゴリCode cleanup/refactoring_30 にセット
  • 対象バージョン4.1.0_127 にセット

他の形式にエクスポート: Atom PDF

いいね!0
いいね!0