Permalink
Browse files

Move type checks up front for common cases

These cases are hit often in Rails apps (FFI due to Socket).
  • Loading branch information...
1 parent 0b819a2 commit 623069323856982e3a6b13ac6feb2256910165ef @dbussink dbussink committed Feb 27, 2013
Showing with 7 additions and 1 deletion.
  1. +3 −1 kernel/common/string.rb
  2. +4 −0 kernel/platform/struct.rb
@@ -121,7 +121,9 @@ def [](index, other = undefined)
Rubinius.primitive :string_aref
unless other.equal?(undefined)
- if index.kind_of? Regexp
+ if index.kind_of?(Fixnum) && other.kind_of?(Fixnum)
+ return substring(index, other)
+ elsif index.kind_of? Regexp
match, str = subpattern(index, other)
Regexp.last_match = match
return str
@@ -258,6 +258,8 @@ def []=(field, val)
raise "Unknown field #{field}" unless offset
case type
+ when Fixnum
+ @pointer.set_at_offset(offset, type, val)
when FFI::Type::Array
if type.implementation == InlineCharArray
(@pointer + offset).write_string StringValue(val), type.size
@@ -281,6 +283,8 @@ def [](field)
case type
when FFI::TYPE_CHARARR
(@pointer + offset).read_string
+ when Fixnum
+ @pointer.get_at_offset(offset, type)
when FFI::Type::Array
type.implementation.new(type, @pointer + offset)
when FFI::Type::StructByValue

0 comments on commit 6230693

Please sign in to comment.