なるようになるブログ

読書感想文かrailsについてかrubyについてか

rails commit log流し読み(2016/02/09)

2016/02/09分のコミットです。

CHANGELOGへの追加はありませんでした。


Update rails-html-sanitizer version to v1.0.3

actionpack/actionpack.gemspecの修正です。

rails-html-sanitizerのバージョン指定を1.0.2以上から1.0.3以上に修正しています。

1.0.2脆弱性があるバージョンの為更新した、との事だったのですが、バージョン指定はRailsアプリ側で行えばよく、Railsが依存するバージョンを更新する必要性は無い(1.0.2でもRailsとしては正しく動作する為)為、後ほどrevertしています。


Revert "Merge pull request #23562 from Azzurrio/patch-1"

直前のコミットをrevertしています。理由は上記参照。


Fix a typo

rails guideのGetting Started with Railsの修正です。

"an Article"を"a Article"にタイポしている箇所があったのを修正しています。


Merge pull request #23534 from bronson/fix-redefined-warning

actionpack/lib/action_controller/test_case.rbの修正です。

ActionController::Liveテスト用のmoduleで、Rubyのワーニング(warning: method redefined; discarding old new_controller_thread)が出ていたので、メソッドを定義する前にremove_methodメソッドで既に定義済みのメソッドを削除するよう修正しています。


[ci skip] Fix grammar

rails guideのRuby on Rails Security Guideの修正です。

Examples from the Undergroundの項のグラマーの修正を行っています。


speed up string xor operation and reduce object allocations

actionpack/lib/action_controller/metal/request_forgery_protection.rbの修正です。

stringのxorをとる為のxor_byte_stringsメソッドリファクタリング(高速化及びメモリ使用量軽減)を行っています。

-        s1.bytes.zip(s2.bytes).map { |(c1,c2)| c1 ^ c2 }.pack('c*')
+        s2_bytes = s2.bytes
+        s1.bytes.map.with_index { |c1, i| c1 ^ s2_bytes[i] }.pack('c*')

実際のベンチマークは下記の通り(コミットログより)。

[aaron@TC rails (master)]$ cat xor.rb
a = "\x14b\"\xB4P8\x05\x8D\xC74\xC3\xEC}\xFDf\x8E!h\xCF^\xBF\xA5%\xC6\xF0\xA9\xF9x\x04\xFA\xF1\x82"
b = "O.\xF7\x01\xA9D\xA3\xE1D\x7FU\x85\xFC\x8Ak\e\x04\x8A\x97\x91\xD01\x02\xA4G\x1EIf:Y\x0F@"

def xor_byte_strings(s1, s2)
  s1.bytes.zip(s2.bytes).map { |(c1,c2)| c1 ^ c2 }.pack('c*')
end

def xor_byte_strings2(s1, s2)
  s2_bytes = s2.bytes
  s1.bytes.map.with_index { |c1, i| c1 ^ s2_bytes[i] }.pack('c*')
end

require 'benchmark/ips'
require 'allocation_tracer'

Benchmark.ips do |x|
  x.report 'xor_byte_strings' do
    xor_byte_strings a, b
  end

  x.report 'xor_byte_strings2' do
    xor_byte_strings2 a, b
  end
end

ObjectSpace::AllocationTracer.setup(%i{type})
result = ObjectSpace::AllocationTracer.trace do
  xor_byte_strings a, b
end
p :xor_byte_strings => result
ObjectSpace::AllocationTracer.clear
result = ObjectSpace::AllocationTracer.trace do
  xor_byte_strings2 a, b
end
p :xor_byte_strings2 => result
[aaron@TC rails (master)]$ ruby -I~/git/allocation_tracer/lib xor.rb
Calculating -------------------------------------
    xor_byte_strings    10.087k i/100ms
   xor_byte_strings2    11.339k i/100ms
-------------------------------------------------
    xor_byte_strings    108.386k (± 5.8%) i/s -    544.698k
   xor_byte_strings2    122.239k (± 3.0%) i/s -    612.306k
{:xor_byte_strings=>{[:T_ARRAY]=>[38, 0, 0, 0, 0, 0], [:T_STRING]=>[2, 0, 0, 0, 0, 0]}}
{:xor_byte_strings2=>{[:T_ARRAY]=>[3, 0, 0, 0, 0, 0], [:T_DATA]=>[1, 0, 0, 0, 0, 0], [:T_IMEMO]=>[2, 0, 0, 0, 0, 0], [:T_STRING]=>[2, 0, 0, 0, 0, 0]}}

drop array allocations on html_safe

activesupport/lib/active_support/core_ext/string/output_safety.rbの修正です。

ActiveSupport::SafeBuffer.newメソッドの引数をArrayからStringに変更しています。

-    def initialize(*)
+    def initialize(str = '')

不要なArrayのallocationを発生しないようにする為、との事です。


Fix typo [ci skip]

actioncable/README.mdの修正です。

"an AppearancesChannel"を"a AppearancesChannel"にタイポしている箇所があったのを修正しています。