Permalink
Browse files

tweak a few things

  • Loading branch information...
charliesome committed Dec 17, 2012
1 parent bf038c6 commit 129e1e626196b01015a5f5d2a5d5ca4d9e7673ca
Showing with 4 additions and 4 deletions.
  1. +4 −4 posts/why-do-singleton-methods-make-ruby-slow.md
@@ -32,14 +32,14 @@ So what gives? Why would defining a completely unrelated singleton method on an
## The Ruby VM
-MRI Ruby 1.9 and up uses a bytecode virtual machine internally. Before any Ruby code is executed, it is compiled into instructions for Ruby's virtual machine. Ruby allows us to peek behind the curtain and inspect the results of the compilation stage with the `RubyVM::InstructionSequence` class.
+MRI Ruby 1.9 and up uses a bytecode virtual machine internally. Before any Ruby code is executed, it is compiled into instructions for MRI's virtual machine. MRI Ruby allows us to peek behind the curtain and inspect the results of the compilation stage with the `RubyVM::InstructionSequence` class.
For example, take this simple looking line of Ruby:
\ruby
puts "Hello world".upcase
-Let's compile it:
+Let's compile it and dump the generated bytecode:
\ruby
puts RubyVM::InstructionSequence.compile(%q{
@@ -57,7 +57,7 @@ Using ruby-2.0.0-preview2, I get a bytecode listing that looks like this: (don't
0007 send <callinfo!mid:puts, argc:1, FCALL|ARGS_SKIP>
0009 leave
-Let's take a look at what's going on here. Ruby's VM is *stack based*, meaning that most instructions will pop their input values off the stack, do some computation, and the push their result values back on to the stack.
+MRI's VM is *stack based*, meaning that most instructions will pop their input values off the stack, do some computation, and the push their result values back on to the stack.
The `putself` instruction pushes the current value of `self` to the top of the stack. Then, the `putstring` instruction pushes its operand (`"Hello world"`) to the top of the stack. At this point, there are two objects on the stack - `"Hello world"` at the top and `self` in the next slot down.
@@ -73,7 +73,7 @@ Finally, the `leave` instruction pops the return value off the top of stack and
Everything in Ruby is a method call - even attribute access and arithmetic. When you call a method on an object, Ruby must look for that method in the object's class's method table. If the method is not found, Ruby tries to look for the method in each superclass until it reaches the root of the inheritance hierarchy - `BasicObject`. This is very slow.
-Ruby uses a few techniques to speed up this otherwise slow process. One of these techniques involves using specialized instructions for common method calls on core classes. At time of writing, these optimized methods are `+`, `-`, `*`, `/`, `%`, `==`, `!=`, `<`, `<=`, `>`, `>=`, `<<`, `[]`, `[]=`, `length`, `size`, `empty?`, `succ`, and `=~`.
+MRI Ruby uses a few techniques to speed up this otherwise slow process. One of these techniques involves using specialized instructions for common method calls on core classes. At time of writing, these optimized methods are `+`, `-`, `*`, `/`, `%`, `==`, `!=`, `<`, `<=`, `>`, `>=`, `<<`, `[]`, `[]=`, `length`, `size`, `empty?`, `succ`, and `=~`.
Here's the implementation of the `opt_aref` instruction - the optimized version of the `[]` method:

0 comments on commit 129e1e6

Please sign in to comment.