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
base: master
Are you sure you want to change the base?
Use a constant empty tar header to avoid extra allocations #7484
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice.
There was a problem hiding this 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.
lib/rubygems/package/tar_header.rb
Outdated
EMPTY = new({ # :nodoc: | ||
mode: 0, | ||
name: "", | ||
prefix: "", | ||
size: 0, | ||
empty: true, | ||
}).freeze |
There was a problem hiding this comment.
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.
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.
There was a problem hiding this 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
@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. |
There was a problem hiding this 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!
@@ -241,7 +257,7 @@ def header(checksum = @checksum) | |||
|
|||
header = header.pack PACK_FORMAT | |||
|
|||
header << ("\0" * ((512 - header.size) % 512)) | |||
header.ljust 512, "\0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a neat change regardless of the rest :)
There is one empty tar header per tar file read, in my experience. each gem requires reading 2 tar files. |
8584e46
to
88d38ac
Compare
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! |
No description provided.