text
stringlengths
0
2.67k
**Justin Garrison:** So you discovered Deflate, right, Jon?
**Jon Johnson:** Yeah, yeah. Back on track.
**Justin Garrison:** All the way back to -- we're giving up on Stargz, you had a kid, you discovered Deflate, and you're thinking of this stuff.
**Jon Johnson:** Wow. That's a great summary. \[laughter\]
**Justin Garrison:** That's why I'm here.
**Autumn Nash:** This is why we keep Justin around.
**Jon Johnson:** \[50:00\] So I want to try to describe Deflate to you... I think without a visual aid, it's basically impossible. But I've also on podcasts -- I think basically what we're describing... If I'm forced to try to visualize something, just from hearing someone describe it, I actually have a way better chan...
**Autumn Nash:** It's like when you read a book, and you have to imagine the scene, and it's not a movie.
**Jon Johnson:** Yeah, exactly. So let me try to explain this. And if it doesn't make sense, that's fine. So Deflate is essentially the body of Gzip. If you were to look at a container image layer, I would attribute maybe 16 bytes of it to Gzip, total, and then the rest of it is all Deflate.
And so when you talk about Gzip, what you're really saying is like "Deflate with a header that's Gzip, and a trailer that's Gzip." And so the Gzip parts of a Gzip archive are just a header that says like, optionally, the file name, definitely it says "Hey, I'm Gzip", and then there's some places to put metadata.
And then the trailer is "Hey, here's a checksum of all that data you just read, and the size that you expect it to be." But because Gzip \[unintelligible 00:51:13.22\] And so that's annoying, for a lot of reasons. But yeah, the Gzip part of Gzip is actually very small. The Deflate part is the actual like "Hey, here's h...
Deflate is made of a sequence of blocks. And it's kind of like a linked list, where you just keep going through it until you find a block where there's a header that says "Hey, I'm the last block." And so you read through a bunch of Deflate blocks and decode them individually, and then you stitch those contents togethe...
So the header is just three bits of a Deflate block. A Deflate block starts with BFINAL, which is a bit that's either zero or one. If it's one, it means "Hey, I'm the last block. When you finish reading this, you're done. Stop reading." And the next two bits are the type. So there are three types of Deflate blocks. It ...
So the first block is "Hey, I am uncompressible data. The encoder just gave up, because this data can't be compressed very easily. Or you didn't give me enough time to figure this out." And so that just starts off with a length that says "I have 100,000 bytes, and it's all binary data. So just copy the next 100k bytes ...
So that's BTYPE=00, the first one. The next block is fixed-compressed, which means that it's a compressed block, but the Huffman tables are hardcoded. So what does that mean? So the compressed blocks have a header themselves, which is like two Huffman trees. If you're not familiar with Huffman encoding, I'm not going t...
\[54:01\] And so what Huffman encoding does is it basically sorts all your data by frequency, and then it encodes them in a way where the most frequent things are small, and the less frequent things are large. And so it's just a way to basically make a bunch of stuff smaller, in a generic way, that's very clever.
**Autumn Nash:** That's like a complicated tree, basically.
**Jon Johnson:** It's trees. Yeah.
**Justin Garrison:** And roughly, if it's a text file or a book, I could say "Hey, I see the\_ a lot. So I'm gonna just say that's in here the most, and so I'm just gonna say that's number one." And then anytime you see a number one, it means this. Right?
**Jon Johnson:** Are we willing to be very pedantic? Because that's not quite right in the context of Deflate.
**Justin Garrison:** Yeah, why not?
**Jon Johnson:** Okay. So there are actually two trees that they themselves become Huffman-encoded. So the start of a compressed block is a tree, that contains two trees' worth of data. One of those trees is for literals. And so each byte is encoded as a Huffman-encoded literal. So you have 255 of these. Those are Huff...
So beyond just Huffman encoding, you also have -- I don't remember what the RFC calls it, but basically pointers. So it is a pair of bytes - maybe more than one byte, but it's two numbers, essentially. And it says "Hey, I'm a pointer. If you go back 12 bytes, and then copy the next four, that's it. That's all you need....
And so what was your example? You've got the\_, and that happens a lot; you're going to end up with this pair of numbers that represent the previous time you saw the\_ space. And so you just go back, you copy that, and then you keep going. And so it's those two things together that give Gzip really good compression rat...
The other cute thing that I didn't realize before staring at RFC 1951 for a long time is that with those pointers you can actually like predict the future. So let's say you have 1,000 zero characters, which happens at the end of a tar stream. So this is a real thing. You can say "Hey, go back one character (that's a ze...
I kind of lied a little bit. You can't do it 1,000. I think the maximum length is like 100-something, or 200-something. 263, or something. But it's a good thing.
So popping the stack a little bit... So the other type of blocks are compressed. So we have compressed blocks. With BTYPE=01 these are fixed trees, basically. So they're hardcoded in the spec, and you don't put them in the block themselves. They're out of band, and everyone knows them. This is useful for compressing re...
**Autumn Nash:** Why is it immutable, though? What makes that?
**Jon Johnson:** \[57:45\] It's just a little optimization -- so Deflate itself is a cute engineering trade-off of a lot of things that are aspects of like Lz77... But it's just an optimization where if you have a small amount of data to encode, it's better if we all just agree ahead of time on what those trees would b...
**Justin Garrison:** It's just hardcoded in the spec, right? You're like "Hey, we're gonna do this type of data, and if you're decoding this type of file, if I reference it, I don't need to add it myself."
**Jon Johnson:** Exactly. The last block type is compressed dynamic, where at the start of the Deflate block you have this inline encoding of the Huffman trees, where you have the literals encoded, and the lengths encoded. And that's it, actually. So that's all of Deflate. So writing an encoder - very complicated, and ...
**Justin Garrison:** And not seekable as what Stargz was doing with those blocks of metadata.
**Jon Johnson:** Exactly.
**Justin Garrison:** Now you can go right to the Deflate block, and pull out the smaller blocks in there, and know how to decode just those bits, right?
**Autumn Nash:** So are you then like almost indexing Deflate?
**Jon Johnson:** Yes. So I'll describe this algorithm... It's very cute. And then maybe we can do the --
**Autumn Nash:** You're the only person that makes things cute. I'm loving this. We just made compression cute. This is amazing, and I'm so here for it.
**Jon Johnson:** It's so, so clever. I can't believe it. So the last piece of information that you have to understand for this to make sense is - with those pointers, when you're seeking backwards to copy some data you already saw, there's a limit to how far back you can go. And that's very, very important for this. So...
What that means is that if I have that 32k around somewhere, I can give it to the decoder if it asks for it. So the trick of this seekable Deflate stuff. And if you want a good implementation of it, Amazon actually put out something called the SOCI Snapshotter, which is seekable OCI. So SOCI Snapshotter is this trick a...
**Autumn Nash:** So you're making your index as you Deflate it.
**Jon Johnson:** Yes. And notably, you don't have to change the input at all.
**Autumn Nash:** Does that also help if, say, your download got interrupted? You could then start at those same intervals?
**Jon Johnson:** Yes. You could also do that with -- well, so yes...
**Autumn Nash:** Would that help for like data recovery also? Say stuff got interrupted, and for when you need to learn when to start later, if you do get interrupted?
**Jon Johnson:** Yes... So it kind of depends on how you do that. If you download the whole thing first, and then g-unzip it, then yes, you don't need this. But let's say you're downloading the whole thing, and as you're downloading it, you're unzipping it and writing it somewhere, and you're not keeping track of any o...
\[01:02:10.09\] So the other thing you need for this to work is the table of contents, of like - okay, I have a tar file; I need to know where in the stream that offset is. And so you end up with this index of checkpoints, which tells you how far into the Gzip stream you can seek, both like the compressed and uncompres...
**Justin Garrison:** And at that point, for something like a container image, you don't have to package it a special way. You have to build an index of the package contents.
**Jon Johnson:** Exactly. And this gets into some OCI stuff, but - you know, "Where do you store those?" was kind of a question that people had for a long time. And there wasn't really a good way with OCI to associate one artifact with another without just rebuilding the thing. We kind of hacked around this... I was on...
So as a client, if you're like "Hey, I want to access this lazily", you can ask the registry, "Give me all the metadata that references like this image", and if one of those things happens to be a SOCI index, then ta-da, you can lazily access it without modifying it... Which is the whole trick, right? It's like, we wan...
**Justin Garrison:** Right. And you don't want to force people to rebuild everything with a special tool that isn't globally supported maybe, or they don't use it everywhere... So then you're like "Oh, I have this one version of the image that is compressed in a certain way, and another version that's just straight Doc...