From 9ce364b079efdb3bcef504a84e2460a9b2b4b9ad Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 6 Jul 2010 10:33:01 +0000 Subject: byte_slice: String#[range] is faster than String#slice!(range) rb_str_slice_bang() allocates a new string internally and calls rb_str_aref_m() AND rb_str_aset_m(), too. String#[] just calls rb_str_aref_m() directly, so it's a much quicker code path. Also, "[]" methods dispatch faster under 1.9, too. --- lib/rainbows/byte_slice.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rainbows/byte_slice.rb b/lib/rainbows/byte_slice.rb index 09d1188..2d5586c 100644 --- a/lib/rainbows/byte_slice.rb +++ b/lib/rainbows/byte_slice.rb @@ -3,7 +3,7 @@ module Rainbows::ByteSlice if String.method_defined?(:encoding) def byte_slice(buf, range) if buf.encoding != Encoding::BINARY - buf.dup.force_encoding(Encoding::BINARY).slice!(range) + buf.dup.force_encoding(Encoding::BINARY)[range] else buf[range] end -- cgit v1.2.3-24-ge0c7