On Saturday 11 July 2009 08:17:54 pm Roger Pack wrote:
> > if (a[0] == '3' && a[1] == NULL)
> >
> > I'm assuming C strings, Ruby strings might work differently, but you get
> > the
> > idea.
>
> That's a great idea.  Avoid ruby calls totally :) [NB that ruby2cext
> already does this for a few things, so...maybe I can just use that].

Maybe. The point was to do this at runtime, in the VM. I don't think all Ruby 
code can be easily converted to C, nor would I want to (I like having 'eval' 
work). But looking at the AST, or even the bytecode representation, this kind 
of thing should be possible to figure out.

Other ideas would be to do clever runtime optimizations, or to do 
optimizations that wouldn't be sane at compile time -- for instance, unroll 
this:

x = 0
foo = lambda { x += 5 }
5.times do
  foo.call
end

into something more like this:

x = 25

Of course, that optimization only makes sense if no one's making changes to 
the Proc class, or to the scope in which that's run -- or maybe we're actually 
storing 'foo' deep in some object, where it probably won't be touched, but it 
might.

All of these are dangerous assumptions to make at compile time, but at 
runtime, it might be reasonable to say "If any of these assumptions change, 
drop the optimized version and do it the slow way."

But I am in a bit over my head, here -- I only know in theory how compilers 
and VMs work; I've never actually contributed to one.