Skip to content

Commit

Permalink
Move type checks up front for common cases
Browse files Browse the repository at this point in the history
These cases are hit often in Rails apps (FFI due to Socket).
  • Loading branch information
dbussink committed Feb 27, 2013
1 parent 0b819a2 commit 6230693
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 3 additions & 1 deletion kernel/common/string.rb
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions kernel/platform/struct.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 6230693

Please sign in to comment.