Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a method cache for faster dispatch and allowing super calls #849

Closed
wants to merge 1 commit into from

Conversation

byroot
Copy link

@byroot byroot commented Mar 19, 2015

The code is heavily borrowed from ActiveRecord.

It should make the attribute methods faster even though I didn't write any benchmark to back that claim.

The real reason I implemented this, is to be able to use super in attribute methods:

class FooSerializer < ActiveModel::Serializer
  attributes :name

  def name
    super.titleize
  end
end

Regards.

@byroot byroot force-pushed the method-cache branch 3 times, most recently from 6369b04 to 8524e40 Compare March 20, 2015 00:07
@kurko
Copy link
Member

kurko commented Mar 20, 2015

This is interesting. @guilleiguaran @joaomdmoura can you chime in?

@byroot
Copy link
Author

byroot commented Mar 20, 2015

Oh I forgot to mention. This technique only works for ruby 2.0+. ActiveRecord up to 4.2 had a fallback. I don't know if it's worth implementing it since 1.9.3 is EOL. But I can do it if it's important.

@bf4
Copy link
Member

bf4 commented Dec 23, 2015

@byroot We've dropping support for Ruby < 2.0 in master, so this is again viable. Would you be interested in rebasing this off of current master? Let me know if I can help.

@byroot
Copy link
Author

byroot commented Dec 23, 2015

I'll try to find some time.

@bf4
Copy link
Member

bf4 commented Dec 23, 2015

Awesome!

B mobile phone

On Dec 23, 2015, at 3:57 AM, Jean Boussier [email protected] wrote:

I'll try to find some time.


Reply to this email directly or view it on GitHub.

@byroot byroot force-pushed the method-cache branch 5 times, most recently from af84b2e to 5b0bf60 Compare December 23, 2015 15:12
@byroot
Copy link
Author

byroot commented Dec 23, 2015

So I looked into it, and given the way it's implemented now with AttributeReader etc, this optimization wouldn't help as much. I actually think it would be worse.

The idea was to generate attributes accessor dynamically via class_eval, but to share cache them in a module.

If we were doing that on AttributeReader, it would generate a singleton_class for every attributes, which is quite slow IIRC.

@byroot byroot closed this Dec 23, 2015
@byroot byroot deleted the method-cache branch December 23, 2015 15:14
@bf4
Copy link
Member

bf4 commented Dec 23, 2015

@byroot Oh, but this PR is great! the attributes internal api is still in flux. See e.g. #1370 I hope there's still something we get improve (would need a benchmark, of course, like #233

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants