Use a generic getter with __callee__ #53
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This modifies the getter definition to use a single method that uses
__callee__
to access the hash.On versions of Ruby 2.0+ and JRuby 9.4.2+
__callee__
will return the aliased name, which in this case is the key for the table.This eliminates one of the Proc-based methods per field and also performs better on CRuby:
Before:
After:
It also uses less memory; creating 100k OpenStruct in the same way as the benchmark uses 205.2MB before and 164MB after.
Note that a similar memory savings is possible by using the following setter:
But it is quite a bit slower than the Proc version. I think it would be worth exploring other ways we can simplify the setters too.