From: Wincent Colaiuta Date: 2008-02-07T03:09:56+09:00 Subject: Re: Avoiding memcpy() during String creation in a C extensio On 6 feb, 18:46, Tim Hunter wrote: > Wincent Colaiuta wrote: > > > Basically what I would like is a String instantiation method which > > could take a pointer to a buffer and basically take ownership of that > > buffer instead of calling memcpy() on it. > > > I've looked in string.c to see if such a function exists and it > > appears that it does not. > > Instead of putting your data into a buffer and then asking Ruby to make > a String out of it, could you use rb_str_buf_new() to create a String > with a specific capacity and then put your data into the String's > buffer? Thanks, Tim. That would indeed work I think. Didn't know about that function but it will come in very handy I think. And if the buffer needs to grow as I append output to it then rb_str_cat should be relatively efficient (I certainly noticed a huge speed-up when I started using rb_str_cat with raw buffers rather than creating new Ruby Strings and using rb_str_append). Cheers, Wincent