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

Use a constant empty tar header to avoid extra allocations #7484

Conversation

segiddins
Copy link
Member

No description provided.

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.

Nice.

@martinemde martinemde self-requested a review February 22, 2024 00:26
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.

I approved it before double checking, here's the additional I think it needs.

Comment on lines 175 to 188
EMPTY = new({ # :nodoc:
mode: 0,
name: "",
prefix: "",
size: 0,
empty: true,
}).freeze
Copy link
Member

Choose a reason for hiding this comment

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

To really be identical you need the following. The default values are not the same as what an empty header outputs.

Suggested change
EMPTY = new({ # :nodoc:
mode: 0,
name: "",
prefix: "",
size: 0,
empty: true,
}).freeze
EMPTY = new({ # :nodoc:
checksum: 0,
gname: "",
linkname: "",
magic: "",
mode: 0,
name: "",
prefix: "",
size: 0,
uname: "",
version: 0,
empty: true,
}).freeze

Here's my irb doublecheck:

irb(main):033> EMPTY = Gem::Package::TarHeader.new({ checksum: 0, gname: "", linkname: "", magic: "", mode: 0, name: "", prefix: "", size: 0, uname: "", version: 0, empty: true,  })
=> #<Gem::Package::TarHeader:0x00000001203e60e0 @checksum=0, @devmajor=0, @devminor=0, @empty=true, @gid=0, @gname="", @linkname="", @magic="", @mode=0, @mtime=0, @name="", @prefix="", @size=0, @typeflag="0", @uid=0, @uname="", @version=0>
irb(main):034> Gem::Package::TarHeader.from(StringIO.new(Gem::Package::TarHeader::EMPTY_HEADER))
=> #<Gem::Package::TarHeader:0x00000001204cde90 @checksum=0, @devmajor=0, @devminor=0, @empty=true, @gid=0, @gname="", @linkname="", @magic="", @mode=0, @mtime=0, @name="", @prefix="", @size=0, @typeflag="0", @uid=0, @uname="", @version=0>

We should also add a test.

@martinemde martinemde self-requested a review February 22, 2024 00:36
Copy link
Member

@simi simi left a comment

Choose a reason for hiding this comment

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

🤔 wondering what's the impact of this change

@martinemde
Copy link
Member

@simi are you thinking the tar header is expected to be mutable? That's worth considering...

@simi
Copy link
Member

simi commented Feb 22, 2024

@simi are you thinking the tar header is expected to be mutable? That's worth considering...

No, I was just wondering what's memory footprint impact.

Copy link
Member

@deivid-rodriguez deivid-rodriguez left a comment

Choose a reason for hiding this comment

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

I also wonder about impact. Do we instantiate an empty tar reader once per gem, and this would reuse it for gem install invocations that install many gems?

Anyways, code changes look good to me!

lib/rubygems/package/tar_header.rb Show resolved Hide resolved
@segiddins
Copy link
Member Author

There is one empty tar header per tar file read, in my experience. each gem requires reading 2 tar files.

@segiddins segiddins force-pushed the segiddins/use-a-constant-empty-tar-header-to-avoid-extra-allocations branch from 8584e46 to 88d38ac Compare April 30, 2024 21:15
@segiddins segiddins marked this pull request as ready for review April 30, 2024 21:15
@deivid-rodriguez
Copy link
Member

Should we squash into a single commit? Right now there's three commits, two of them with the same commit message, which seems a bit confusing! Other than that, seems great!

@deivid-rodriguez deivid-rodriguez force-pushed the segiddins/use-a-constant-empty-tar-header-to-avoid-extra-allocations branch from 88d38ac to 716666f Compare May 13, 2024 11:51
@deivid-rodriguez
Copy link
Member

@segiddins I squashed commits myself and will be merging this, hope it's ok.

@deivid-rodriguez deivid-rodriguez merged commit 90d0ceb into master May 13, 2024
75 checks passed
@deivid-rodriguez deivid-rodriguez deleted the segiddins/use-a-constant-empty-tar-header-to-avoid-extra-allocations branch May 13, 2024 13:01
deivid-rodriguez added a commit that referenced this pull request May 16, 2024
…-tar-header-to-avoid-extra-allocations

Use a constant empty tar header to avoid extra allocations

(cherry picked from commit 90d0ceb)
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

4 participants