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

Avoid allocating empty hashes in Index #6962

Merged
merged 2 commits into from
Sep 15, 2023

Conversation

segiddins
Copy link
Member

Since the hashes have a default proc that returns a (new) empty hash, we
can avoid allocating those empty hashes when we are only doing lookups.

Test from running bundle update --bundler against a rails app I have
lying around:

==> memprof.after.txt <==
Total allocated: 9.71 MB (68282 objects)
Total retained:  4.87 MB (33791 objects)

==> memprof.before.txt <==
Total allocated: 10.83 MB (100596 objects)
Total retained:  5.02 MB (34721 objects)

What was the end-user or developer problem that led to this PR?

What is your fix for the problem, implemented in this PR?

Make sure the following tasks are checked

Since the hashes have a default proc that returns a (new) empty hash, we
can avoid allocating those empty hashes when we are only doing lookups.

Test from running `bundle update --bundler` against a rails app I have
lying around:

```
==> memprof.after.txt <==
Total allocated: 9.71 MB (68282 objects)
Total retained:  4.87 MB (33791 objects)

==> memprof.before.txt <==
Total allocated: 10.83 MB (100596 objects)
Total retained:  5.02 MB (34721 objects)
```
Copy link
Member

@martinemde martinemde left a comment

Choose a reason for hiding this comment

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

Great find, thanks @segiddins!

@martinemde martinemde force-pushed the segiddins/avoid-allocating-empty-hashes-in-index branch 2 times, most recently from 76da335 to f1ec56c Compare September 15, 2023 04:17
@@ -170,31 +160,41 @@ def add_source(index)

private

def search_by_query(query)
Copy link
Member

Choose a reason for hiding this comment

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

There were no callers except the one here. Moved to private and changed to return nil when nothing found so calling code can more easily reduce array usage.

bundler/lib/bundler/source/rubygems.rb Outdated Show resolved Hide resolved
@martinemde martinemde force-pushed the segiddins/avoid-allocating-empty-hashes-in-index branch from f1ec56c to e6f2146 Compare September 15, 2023 04:20
bundler/lib/bundler/index.rb Outdated Show resolved Hide resolved
bundler/lib/bundler/index.rb Outdated Show resolved Hide resolved
@martinemde martinemde force-pushed the segiddins/avoid-allocating-empty-hashes-in-index branch 7 times, most recently from 21aed21 to c8eb2f1 Compare September 15, 2023 20:21
Remove the default nested hash in Index entirely
Index#search_all now yields or returns enum since that's what caller
needs.
@martinemde martinemde force-pushed the segiddins/avoid-allocating-empty-hashes-in-index branch from c8eb2f1 to c45ea3b Compare September 15, 2023 20:25
@martinemde martinemde merged commit bf1c85d into master Sep 15, 2023
92 checks passed
@martinemde martinemde deleted the segiddins/avoid-allocating-empty-hashes-in-index branch September 15, 2023 22:01
deivid-rodriguez pushed a commit that referenced this pull request Sep 21, 2023
…ty-hashes-in-index

Avoid allocating empty hashes in Index

(cherry picked from commit bf1c85d)
deivid-rodriguez pushed a commit that referenced this pull request Sep 21, 2023
…ty-hashes-in-index

Avoid allocating empty hashes in Index

(cherry picked from commit bf1c85d)
deivid-rodriguez added a commit to dependabot/dependabot-core that referenced this pull request Nov 8, 2023
* `Bundler::Fetcher#fetchers` was made private at
  rubygems/rubygems#6919.
* `Bundler::Index#search_all` returns an enumerator since
  rubygems/rubygems#6962.
deivid-rodriguez added a commit to dependabot/dependabot-core that referenced this pull request Nov 9, 2023
* `Bundler::Fetcher#fetchers` was made private at
  rubygems/rubygems#6919.
* `Bundler::Index#search_all` returns an enumerator since
  rubygems/rubygems#6962.
deivid-rodriguez added a commit to dependabot/dependabot-core that referenced this pull request Nov 15, 2023
* `Bundler::Fetcher#fetchers` was made private at
  rubygems/rubygems#6919.
* `Bundler::Index#search_all` returns an enumerator since
  rubygems/rubygems#6962.
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.

None yet

3 participants