From: "Eric I." Date: 2007-12-11T01:15:00+09:00 Subject: Re: Zero-copy String Handling On Dec 10, 10:52 am, Curt Sampson wrote: > It appears to me that this means I don't really have any need to > copy the data; I ought to just be able to set up a bunch of (likely > frozen) String objects and then tweak the ptr and len on them and pass > them around, avoiding any allocations or data copies. From a bit of > experimentation, I can see that dropping several calls to rb_str_new for > each row results in an enormous speed increase--about ten-fold--in how > fast I can scan through the file. > > Does anybody have any suggestions on a reasonably safe way to do this? I've read (although I've never verified this by looking at the source code) that Ruby's strings are copy-on-write. So if you have code like this: s1 = "This is some string." s2 = s1[5, 7] # => "is some" s3 = s1[5..11] # => "is some" no copies are made and data is shared ... until you modify the data, in which case the copy is made. If you wanted to enforce the read-only aspect, perhaps you could create a subclass of String where all the methods and operators that modify the character data are re-implemented to raise exceptions. Eric ==== Interested in hands-on, on-site Ruby training? See http://LearnRuby.com for information about a well-reviewed class.