[Ruby] splatting arguments in a C extension

Ryan Davis ryand-ruby at zenspider.com
Sun Feb 3 00:22:24 PST 2008


On Feb 2, 2008, at 12:49 , Aaron Patterson wrote:

>> VALUE rb_funcall_splat(VALUE receiver, ID method, VALUE argv)
>> {
>>  return rb_funcall2(receiver, method, RARRAY_LEN(argv),  
>> RARRAY_PTR(argv));
>> }
>
> Ah.  That seems to work.  This should be gc friendly as well.  Right?

Yeah. seems to pan out:

#!/usr/bin/ruby -w

require 'rubygems'
require 'inline'

class X
   def m a, b, c
     [a, b, c]
   end

   inline do |builder|
     builder.add_type_converter("ID", '', '')

     builder.c_raw '
       VALUE splat_funcall(VALUE recv, ID mid, VALUE args) {
         return rb_funcall2(recv, mid, RARRAY_LEN(args),  
RARRAY_PTR(args));
       }
     '

     builder.c '
       VALUE x(VALUE ary) {
         return splat_funcall(self, rb_intern("m"), ary);
       }
     '
   end
end

x = X.new

p x.x([1, 2, 3])

100_000_000.times do
   GC.start
   x.x([1, 2, 3])
end



More information about the Ruby mailing list