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

Memory allocations #184

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Memory allocations #184

wants to merge 2 commits into from

Conversation

ProGM
Copy link

@ProGM ProGM commented May 18, 2018

I've noticed a peak of memory allocations in this file by using derailed gem

       800  /Users/progm/.rbenv/versions/2.3.5/lib/ruby/gems/2.3.0/gems/deface-1.0.2/lib/deface/search.rb:13
       400  /Users/progm/.rbenv/versions/2.3.5/lib/ruby/gems/2.3.0/gems/deface-1.0.2/lib/deface/override.rb:189

That could be simply fixed using a .freeze.

  • Freezing empty strings
  • use compact! instead of compact

ProGM added 2 commits May 18, 2018 15:28
* Freezing empty string
* use `compact!` instead of compact
@@ -196,7 +196,7 @@ def digest
#
def self.digest(details)
overrides = self.find(details)
to_hash = overrides.inject('') { |digest, override| digest << override.digest }
to_hash = overrides.inject(''.freeze) { |digest, override| digest << override.digest }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to become a +=.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean overrides.inject('') { |digest, override| digest += override.digest } ?

Uhm maybe also overrides.map(&:digest).join?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ProGM I mean:

overrides.inject(''.freeze) { |digest, override| digest += override.digest }

<< will fail on a frozen string because it attempts to mutate the object, so you need to create a new string every time. I'm not sure if that will negate the performance benefits of using .freeze though.

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

Successfully merging this pull request may close these issues.

2 participants