This post originated from an RSS feed registered with Ruby Buzz
by Daniel Berger.
Original Post: Stripping NULL
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
In our efforts to rewrite win32-eventlog I noticed that the EventLog#read method was rather slow compared to the C equivalent. Too slow, even for pure Ruby. Thanks to the profiler I noticed the primary culprit was this idiom I was using to strip a string of null characters:
string.split(0.chr).first
This was necessary, instead of String#rstrip or String#gsub, because the buffer could contain junk other than null characters at the end of the string. So, I proposed a String#nstrip (null strip) on the ruby-core list, and several people chimed in with some handy, and faster, solutions. The winner was Ara Howard with this solution:
string[ /^[^\0]*/ ]
That's about 4 times faster than the approach I was using, which had quite a significant impact. To give you an idea of just how significant, it took 6 minutes to read 12000 event log records using the old approach. Using Ara's approach reduced that to 2 minutes. :)