SciRuby / rubex

rubex - A Ruby-like language for writing Ruby C extensions.
BSD 2-Clause "Simplified" License
451 stars 21 forks source link

Specify types of Ruby objects for faster method calls and C level optimizations #10

Closed v0dro closed 6 years ago

v0dro commented 7 years ago

The C API provides several features for optimizing method calls and things like directly accessing the length of a string via macro RSTRING_LEN() instead of going through the overhead of a Ruby method lookup.

Therefore, if the user can specify the type of a Ruby object using data types str, array and hsh. These Ruby objects will then be of that type only and assigning them to another type of Ruby object should raise an error (via calls to TYPE() etc.)

Sample code:

def foo
  str a = "hello world"
  return a.size
end

Since the compiler knows that a is a String, it can directly call RSTRING_LEN() to generate code like so:

// header code....
static VALUE foo(...)
{
  VALUE a = rb_str_new2("hello world");
   return INT2FIX(RSTRING_LEN(a));
}
// footer code....

Various such examples exist for each data type, all of which should be implemented.