Optimize memory alloc and retained #2441
Merged
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 PR should improve memory allocation and retained.
Base code
This code
Real world app
Improve caching
lib/grape/namespace.rb
andlib/grape/path.rb
were already caching but it could be improved by addingGrape::Router.normalize_path
call directly in the cache instead.Stringify inheritable settings
prefix
,namespace
and other inheritable settings may be naturally declared with a symbol instead of a string and along compilation, they will be stringified and join create more complex strings. Saving them as string saves some memory allocation.delete_if
instead of-
delete_if
will remove elements in the array and returns it. Substracting two arrays will create a new one. Most cases were callingkeys
and substracting values from its.Replace
AttributesTranslator
byActiveSupport::OrderedOptions
AttributesTranslator
is basically a hash with dynamic accessor methods and it is used inlib/grape/router/route.rb
andlib/grape/router/greedy_route.rb
. These 2 also have anoptions
which is the same data included in theAttributesTranslator
. It didn't make sense to keep these two. So, I found that ActiveSupport::OrderedOptions is literally a hash with dynamic accessor methods and it could replace entirelyAttributesTranslator
. In conjunction with delegate_missing_to, I've successfully dropAttributesTranslator
and just add an aliasattributes
tooptions
.Introducing base_route.rb
lib/grape/router/greedy_route.rb
andlib/grape/router/route.rb
are vey close in terms of code so I've created abase_route
that both would inherit. It also encapsulated theregexification
. I've also added some cache for the regex captures.Some small refactors
I've refactored some small functions.
Fix #2440