Quoting Xavier Noria <[email protected]>: > Hi gents, > > I am playing around with an idea to improve the performance of singularize > and pluralize for Rails 4.0. In my proof of concept I see some 5x boost, > but it relies an assumption that I'd like to consult with you all. Let me > explain. > > As you know, inflection rules have a lhs which is a string or regexp, and a > replacement string as rhs. > > The current implementation collects the rules in an array, and to apply > them to a particular word the array is iterated. The first pattern that > matches is the one applied. In particular, the most common rule (eg append > "s" to form a plural), is the *last* one because most specific rules come > first. By default we have +30 rules for singulars and +30 for plurals. > > My idea is to build a single regexp with an alternation, detect which > segment matches, and apply its replacement. That is, let the regexp engine > itself do the loop. Much faster. >
This is interesting. I tried a similar optimization parsing RSS feeds, replacing the list of strings in a when clause with a regex. It was 35% faster. Then I combined all the regex with a group with a unique character in it to determine which sub-pattern matched. No faster than the list of string approach. Interesting. There is something I don't understand about regex performance. Jeffrey -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
