Performance issue in String#codepoints
jabamaus opened this issue · comments
Hi,
I've written an app that runs with mruby or cruby and I noticed that the frame rate was massively degraded when running with mruby. After investigation I narrowed it down to calls to String#codepoints which where happening multiple times per frame. Of course I can, and probably will improve my code to use a cache or eliminate it entirely but I think the method itself can be hugely optimized, after all the way its currently written works fine in cruby.
The implementation in https://github.com/mruby/mruby/blob/master/mrbgems/mruby-string-ext/mrblib/string.rb is:
self.split('').map{|x| x.ord}
I think this is made worse by the fact that the onig_regexp gem replaces String#split with its own version which probably adds a further performance penalty.
Presumably the GC is getting hammered. I guess doing it in C would be the best?
I'm not desperate for this but I thought I'd flag it.
Cheers!
Are you passing a block ? Maybe it is more efficient to use yield
instead.
And I think split
could be avoided with each_char
.
It is a shot in the dark though.
def codepoints
if block_given?
each_char { |c| yield(c.ord) }
self
else
each_char.map { |c| c.ord }
end
end
We implemented part of codepoints
in C. Now it's even faster than CRuby (in cases we tested).