7 comments

  • stgn2 hours ago
    &gt; so i wrote a gzip decompressor from scratch<p>After skimming through the author&#x27;s Rust code, it appears to be a fairly straightforward port of puff.c (included in the zlib source): <a href="https:&#x2F;&#x2F;github.com&#x2F;madler&#x2F;zlib&#x2F;blob&#x2F;develop&#x2F;contrib&#x2F;puff&#x2F;puff.c" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;madler&#x2F;zlib&#x2F;blob&#x2F;develop&#x2F;contrib&#x2F;puff&#x2F;puf...</a>
    • dymk55 minutes ago
      This feels like it should have been mentioned in the article.<p>It makes me wonder if there was some LLM help, based on how similar the fn structure and identifier names are.
      • f1shy37 minutes ago
        &gt; It makes me wonder if there was some LLM help<p>I would bet there was
  • Lerc1 hour ago
    The function<p><pre><code> fn bits(&amp;mut self, need: i32) -&gt; i32 { .... </code></pre> Put me in mind of one of my early experiments in Rust. It would be interesting to compare a iterator based form that just called .take(need)<p>I haven&#x27;t written a lot of Rust, but one thing I did was to write an iterator that took an iterator of bytes as input and provided bits as output. Then used an iterator that gave bytes from a block of memory.<p>It was mostly as a test to see how much high level abstraction left an imprint on the compiled code.<p>The dissasembly showed it pulling in 32 bits at a time and shifting out the bits pretty much the same way I would have written in ASM.<p>I was quite impressed. Although I tested it was working by counting the bits and someone critizised it for not using popcount, so I guess you can&#x27;t have everything.
    • kibwen53 minutes ago
      <i>&gt; I tested it was working by counting the bits and someone critizised it for not using popcount</i><p>PSA: Rust exposes the popcnt intrinsic via the `count_ones` method on integer types: <a href="https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;primitive.u32.html#method.count_ones" rel="nofollow">https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;primitive.u32.html#method.coun...</a>
      • Lerc30 minutes ago
        Looks like that was added about 3 years after I wrote my test code.
    • evmar27 minutes ago
      I had a similar experience implementing simd instructions in my emulator, where I needed to break apart a 64-bit value into four eight-bit values, do an operation on each value, then pack it back together. My first implementation did it with all the bit shifts you’d expect, but my second one used two helpers to unpack into an array, map on the array to a second array, and pack the array again. The optimized output was basically the same.
  • nayuki2 hours ago
    Just like that author, many years ago, I went through the process of understanding the DEFLATE compression standard and producing a short and concise decompressor for gzip+DEFLATE. Here are the resources I published as a result of that exploration:<p>* <a href="https:&#x2F;&#x2F;www.nayuki.io&#x2F;page&#x2F;deflate-specification-v1-3-html" rel="nofollow">https:&#x2F;&#x2F;www.nayuki.io&#x2F;page&#x2F;deflate-specification-v1-3-html</a><p>* <a href="https:&#x2F;&#x2F;www.nayuki.io&#x2F;page&#x2F;simple-deflate-decompressor" rel="nofollow">https:&#x2F;&#x2F;www.nayuki.io&#x2F;page&#x2F;simple-deflate-decompressor</a><p>* <a href="https:&#x2F;&#x2F;github.com&#x2F;nayuki&#x2F;Simple-DEFLATE-decompressor" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;nayuki&#x2F;Simple-DEFLATE-decompressor</a>
  • carlos25635 minutes ago
    &gt;the only flag we care about is FNAME The specification does not define an encoding for the file name. Different file systems may impose restrictions on certain names, so FNAME should not be used.
  • MisterTea2 hours ago
    &gt; twenty five thousand lines of pure C not counting CMake files. ...<p>Keep in mind this is also 31 years of cruft and lord knows what.<p>Plan 9 gzip is 738 lines total:<p><pre><code> gzip.c 217 lines gzip.h 40 lines zip.c 398 lines zip.h 83 lines </code></pre> Even the zipfs file server that mounts zip files as file systems is 391 lines.<p>edit - post a link to said code: <a href="https:&#x2F;&#x2F;github.com&#x2F;9front&#x2F;9front&#x2F;tree&#x2F;front&#x2F;sys&#x2F;src&#x2F;cmd&#x2F;gzip" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;9front&#x2F;9front&#x2F;tree&#x2F;front&#x2F;sys&#x2F;src&#x2F;cmd&#x2F;gzip</a><p>&gt; ... (and whenever working with C always keep in mind that C stands for CVE).<p>Sigh.
    • bboozzoo1 hour ago
      You forgot to include <a href="https:&#x2F;&#x2F;github.com&#x2F;9front&#x2F;9front&#x2F;tree&#x2F;front&#x2F;sys&#x2F;src&#x2F;libflate" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;9front&#x2F;9front&#x2F;tree&#x2F;front&#x2F;sys&#x2F;src&#x2F;libflate</a> which gzip is built around, which brings it closer to 10k lines.
      • carlos25632 minutes ago
        Interesting, the decompressor in Jdeflate is around 4k LoC. <a href="https:&#x2F;&#x2F;github.com&#x2F;Jpn666&#x2F;jdeflate" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Jpn666&#x2F;jdeflate</a>
    • tyingq2 hours ago
      His also omits CRC, which is part of the 25k lines, no --fast&#x2F;--best&#x2F;etc, missing some output formats, and so on. I&#x27;m sure the 25k includes a lot of bloat, but the comparison is odd. Comparing to your list would make much more sense.
      • kibwen2 hours ago
        I would expect a CRC to add a negligible number of lines of code. The reason that production-grade decompressors are tens of thousands of LOC is likely attributable to extreme manual optimization. For example, I wouldn&#x27;t be surprised if a measurable fraction of those lines are actually inline assembly.
        • nayuki1 hour ago
          True. A most basic CRC implementation is about 7 lines of code: (presented in Java to avoid some C&#x2F;C++ footguns)<p><pre><code> int crc32(byte[] data) { int crc = ~0; for (byte b : data) { crc ^= b &amp; 0xFF; for (int i = 0; i &lt; 8; i++) crc = (crc &gt;&gt;&gt; 1) ^ ((crc &amp; 1) * 0xEDB88320); } return ~crc; } </code></pre> Or smooshed down slightly (with caveats):<p><pre><code> int crc32(byte[] data) { int crc = ~0; for (int i = 0; i &lt; data.length * 8; i++) { crc ^= (data[i &#x2F; 8] &gt;&gt; (i % 8)) &amp; 1; crc = (crc &gt;&gt;&gt; 1) ^ ((crc &amp; 1) * 0xEDB88320); } return ~crc; } </code></pre> But one reason that many CRC implementations are large is because they include a pre-computed table of 256× 32-bit constants so that one byte can processed at a time. For example: <a href="https:&#x2F;&#x2F;github.com&#x2F;madler&#x2F;zlib&#x2F;blob&#x2F;7cdaaa09095e9266dee21314599a9258db53685e&#x2F;crc32.h" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;madler&#x2F;zlib&#x2F;blob&#x2F;7cdaaa09095e9266dee21314...</a>
          • xxs1 hour ago
            That&#x27;s java code, though... bit weird, esp. i % 8 (which is just i &amp; 7). The compiler should be able to optimize it since &#x27;i&#x27; is guaranteed to be non-negative, still awkward.<p>Java CRC32 nowadays uses intrinsics and avx128 for crc32.
        • ack_complete1 hour ago
          Doesn&#x27;t need to be inline assembly, just pre-encoded lookup tables and intrinsics-based vectorized CRC alone will add quite a lot of code. Most multi-platform CRC algorithms tend to have at least a few paths for byte&#x2F;word&#x2F;dword at a time, hardware CRC, and hardware GF(2) multiply. It&#x27;s not really extreme optimization, just better algorithms to match better hardware capabilities.<p>The Huffman decoding implementation is also bigger in production implementations for both speed and error checking. Two Huffman trees need to be exactly complete <i>except</i> in the special case of a single code, and in most cases they are flattened to two-level tables for speed (though the latest desktop CPUs have enough L1 cache to use single-level).<p>Finally, the LZ copy typically has special cases added for using wider than byte copies for non-overlapping, non-wrapping runs. This is a significant decoding speed optimization.
        • tyingq2 hours ago
          Yes, there&#x27;s subdirs with language bindings for many non-C langs, an examples folder with example C code, win32 specific C code, test code, etc.<p>More reasons it&#x27;s an odd comparison.
      • fullstop2 hours ago
        gzip also contains a significant amount of compatibility code for different platforms.
      • xxs1 hour ago
        Crc32 can be written in handful lines of code. Although it&#x27;d be better to use the vector instruction set - e.g. AVX when available.
  • jeffrallen2 hours ago
    But probably without any error checking.<p>Feels like Rust culture inherited &quot;throw and forget&quot; as an error handling &quot;strategy&quot; from Java<p>Sigh.
    • dmit1 hour ago
      Hehe, why &quot;probably&quot;? It says &quot;250 lines&quot; right there in the subject. Surely one can skim the single file of code (<a href="https:&#x2F;&#x2F;github.com&#x2F;ieviev&#x2F;mini-gzip&#x2F;blob&#x2F;main&#x2F;src&#x2F;main.rs" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ieviev&#x2F;mini-gzip&#x2F;blob&#x2F;main&#x2F;src&#x2F;main.rs</a>) and offer criticism that isn&#x27;t based on hypotheticals?<p>Anyway, I skimmed the file for you this time, and basically you&#x27;re either correct or wrong, depending on your definition of &quot;error checking.&quot; The code handles error conditions by aborting the process. Seeing as it&#x27;s a standalone CLI program and not a library meant for reuse, safely shutting down with a meaningful message sounds like fair game to me.
    • dymk2 hours ago
      This is an educational project. Not something for production. The article even says so!<p>You can leave the snide comments about “Rust culture” (whatever that is) out next time.
    • throwaway274482 hours ago
      Why people ascribe error handling practices to languages is baffling. What language <i>doesn&#x27;t</i> allow punting error handling until later? Even Haskell has &quot;panic&quot; functionality that fudges the type constraints to allow this.
      • pdpi21 minutes ago
        EAFP is an explicit part of what makes code &quot;pythonic&quot;, and the Zen of Python (`import this`) has the lines &quot;Errors should never pass silently. Unless explicitly silenced.&quot; Java has checked and unchecked exceptions. Rust has panics and Result&lt;T, E&gt;, and the ? operator.<p>The way a language&#x27;s community handles errors and how the language itself handles errors are different things, sure, but they&#x27;re not independent of each other.<p>That said, OP&#x27;s snark against Rust is completely unmerited, and they can take my `impl From&lt;OtherErr&gt; for MyErr` from my cold dead hands.
  • up2isomorphism2 hours ago
    Another dev who doesn’t show respect to what has been done and expect a particular language will do wonders for him. Also I don’t see this is much better in term of readability.
    • maverwa1 hour ago
      Where do you see the lack of respect? The author wanted to learn how gzip works and chose to implement it in a language they like to do so. As a learning tool, not because the world needs another gzip decompressor.
    • hybrid_study1 hour ago
      he does mention <a href="https:&#x2F;&#x2F;github.com&#x2F;trifectatechfoundation&#x2F;zlib-rs" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;trifectatechfoundation&#x2F;zlib-rs</a> not just <a href="https:&#x2F;&#x2F;github.com&#x2F;madler&#x2F;zlib" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;madler&#x2F;zlib</a>, but it would be interesting to hear from those developers also