From: Robert Klemme Date: 2011-04-27T21:08:47+09:00 Subject: Re: Fast way to move C array to ruby (rb_ary_new4)? On Wed, Apr 27, 2011 at 12:46 PM, Ville Sipola wrote: > Good day > > I have a number of large arrays in a C module, and I'd need to move them > to ruby as quickly as possible. > > Currently I use the following type of an approach: > > instance_array_in_ruby = rb_iv_get(calling_class, "@x"); > double c_array[1000000]; > > for(i=0;i<1000000;i++) > { >   rb_ary_store(instance_array_in_ruby, i, rb_float_new(c_array[i])); > } > > But that's slow, apparently due to the large number of rb_float_new:s. > > There is a function called rb_ary_new4 that would seem to fit > the purpose of quickly moving an entire C array to ruby. Only I'm unable > to get it work, as it, too requires VALUE:s. > I've tried many things including mumbo-jumbo like > rb_iv_set(calling_class,"@x",rb_ary_new4(1000000, (VALUE)c_array));, but > I'm only managing to get segfaults or solutions that still need a > million rb_something_new:s. > > Would you know an efficient way of moving a large data from C array to > ruby array? What about leaving it in C, i.e. wrapping in a specific type which presents an Array (or Enumerable) like API and uses your C array internally? A similar approach is taken by ENV (you can find the code in hash.c). Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/