forked from ruby-i18n/i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stop using throw / catch for exceptions
This cleans up our code, and makes it faster. Every call to `translate` required an `is_a` check to make sure that the return value wasn't a translation error. This behavior punished method calls when the lookup was successful. Successful lookup should *hopefully* be the majority of cases, so we should make that code path as fast as possible. Benchmark: ```ruby require 'i18n' require 'benchmark/ips' require 'set' I18n.backend = I18n::Backend::Simple.new I18n.backend.store_translations('en', :foo => 'bar') I18n.config.enforce_available_locales = true Benchmark.ips do |x| options = { :locale => 'en' } x.report 'tr' do I18n.translate(:foo, options) end end ``` Before: ``` [aaron@higgins i18n (rm_throw)]$ ruby -I lib test.rb Calculating ------------------------------------- tr 7661 i/100ms ------------------------------------------------- tr 89120.2 (±5.0%) i/s - 451999 in 5.085290s ``` After: ``` [aaron@higgins i18n (rm_throw)]$ ruby -I lib test.rb Calculating ------------------------------------- tr 8207 i/100ms ------------------------------------------------- tr 93770.9 (±5.1%) i/s - 467799 in 5.002582s ```
- Loading branch information
1 parent
750c3c3
commit acf0546
Showing
5 changed files
with
37 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
acf0546
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tenderlove you are probably unaware, but the whole
throw/catch
thing is being used because a typical Rails use-case generates a lot of missing translation lookups and generating exceptions on JRuby is somewhat expensive.Here what I get with your (slightly modified) benchmark
test.rb
:When running on JRuby, on
master
:When running JRuby on
rm_throw
:As it happens, I'm also quite unhappy with
i18n
's performance and have a pending "performace" PR, which also killsthrow/catch
(replacing it withnil
checks). Running the bench with my PR merged (on JRuby):acf0546
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thedarkone awesome. I just checked your PR, I think it's great. We should make the CI green, and I'll try to get someone to merge it.