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

Reduce allocations by freezing paths #311

Merged
merged 2 commits into from Jun 29, 2020
Merged

Reduce allocations by freezing paths #311

merged 2 commits into from Jun 29, 2020

Conversation

casperisfine
Copy link
Contributor

Base on #310 for CI purposes.

This patch is based on an implementation detail of MRI. Pretty much all File methods call FilePathStringValue() on the passed path. This method will always dup the string unless it's already frozen. As a result freezing paths before calling `File methods will save one string allocation the vast majority of the time.

s = __FILE__
alloc = GC.stat(:total_allocated_objects)
File.exist?(s)
p GC.stat(:total_allocated_objects) - alloc # => 1

s = __FILE__.freeze
alloc = GC.stat(:total_allocated_objects)
real = File.exist?(s)
p GC.stat(:total_allocated_objects) - alloc # => 0

Of course it only really matter it loops, but Bootsnap have a few of these.

@casperisfine casperisfine requested a review from burke as a code owner June 28, 2020 21:25
@casperisfine casperisfine merged commit fc3acd5 into master Jun 29, 2020
@casperisfine casperisfine deleted the file-allocs branch June 29, 2020 17:22
@XrXr XrXr temporarily deployed to rubygems October 24, 2020 22:20 Inactive
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.

None yet

4 participants