barsoom/traco — pull request #28 by nashbridges (2014-08-03 08:43:43+00:00)
Title: Performance

Hi!

This PR has two main goals: improve performance and make code more understandable.

The main concern about current implementation is that on each `Post#title` call we perform full scan of `Post` columns with grep & sort, which is very unefficient. The other concern is that we instantiate `LocalizedReader` and throw it away on each call, that adds more work to GC.

During refactoring I was able to decrease access overhead to a localized column from 210x to 10x. Can we make it better?

The approach to finding localized attributes was changed. Instead of grepping we check if class responds to a method.

`Traco::LocaleFallbacks` was introduced in order to decouple fallback calculating logic. I wanted it to be compatible with https://github.com/svenfuchs/i18n/blob/master/lib/i18n/locale/fallbacks.rb, thus `#[]` interface. IMHO, `:any` fallback looks a little weird, however, I didn't want to change any existing behaviour.

I also found that using

```ruby
MyModule = Class.new(Module)
include MyModule.new(...)
```

technique is more understandable than dealing with `INSTANCE_METHODS_MODULE_NAME`.