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

Add Buffered<T> to add buffering to byte streams. #3405

Merged
merged 9 commits into from
Sep 6, 2020
Merged

Add Buffered<T> to add buffering to byte streams. #3405

merged 9 commits into from
Sep 6, 2020

Conversation

asynts
Copy link
Member

@asynts asynts commented Sep 5, 2020

This is more of a collection of loosely related stuff.

  • I added a Buffered<T> template that adds buffering to any class that inherits from InputStream or OutputStream.

  • I've tried to get the Gzip implementation to a state where it can correctly decompress usual files (as opposed to files constructed to test one specific behavior.) I already found and fixed a few bugs but there seems to be a bug buried in DeflateDecompressor::decode_distance still.

  • I also accumulated a few unit tests that I wrote to track down errors.

It started to become annoying to rebase so I thought I'd merge some of it.

We shouldn't assert that the input file is valid.
I suspected an error in CircularDuplexStream::read(Bytes, size_t). This
does not appear to be the case, this test case is useful regardless.

The following script was used to generate the test:

    import gzip

    uncompressed = []
    for _ in range(0x100):
        uncompressed.append(1)
    for _ in range(0x7e00):
        uncompressed.append(0)
    for _ in range(0x100):
        uncompressed.append(1)

    compressed = gzip.compress(bytes(uncompressed))
    compressed = ", ".join(f"0x{byte:02x}" for byte in compressed)

    print(f"""\
    TEST_CASE(gzip_decompress_repeat_around_buffer)
    {{
        const u8 compressed[] = {{
            {compressed}
        }};

        u8 uncompressed[0x8011];
        Bytes{{ uncompressed, sizeof(uncompressed) }}.fill(0);
        uncompressed[0x8000] = 1;

        const auto decompressed = Compress::GzipDecompressor::decompress_all({{ compressed, sizeof(compressed) }});

        EXPECT(compare({{ uncompressed, sizeof(uncompressed) }}, decompressed.bytes()));
    }}
    """, end="")
The streaming operator doesn't short-circuit, consider the following
snippet:

    void foo(InputStream& stream) {
        int a, b;
        stream >> a >> b;
    }

If the first read fails, the second is called regardless. It should be
well defined what happens in this case: nothing.
@awesomekling awesomekling merged commit 4c317a9 into SerenityOS:master Sep 6, 2020
@asynts asynts deleted the buffered branch September 6, 2020 11:12
nico added a commit to nico/serenity that referenced this pull request Sep 9, 2024
Symbols that need <= 8 bits hit a fast path as of SerenityOS#18075, but
the slow path has done a full binary search over all symbols
ever since this code was added in SerenityOS#2963. (SerenityOS#3405 even added a FIXME
for doing this, but SerenityOS#18075 removed it.)

Instead of doing a binary search over all codes for every single
bit read, this implements the Moffat-Turpin approach described at
https://www.hanshq.net/zip.html#huffdec, which only requires a
table read per bit.

    hyperfine 'Build/lagom/bin/unzip ~/Downloads/enwik8.zip'
    1.008 s ± 0.016 s  =>  957.7 ms ± 3.9 ms, 5% faster

Due to issue SerenityOS#25005, we can't peek the full 15 bits at once but
have to read them one-by-one. This makes the code look a bit
different than in the linked article.

I also tried not changing CanonicalCode::from_bytes() too much.
It does 16 passes over all symbols. I think it could do it in
a single pass instead. But that's for a future change.

No behavior change (other than slightly faster perf).
nico added a commit to nico/serenity that referenced this pull request Sep 10, 2024
Symbols that need <= 8 bits hit a fast path as of SerenityOS#18075, but
the slow path has done a full binary search over all symbols
ever since this code was added in SerenityOS#2963. (SerenityOS#3405 even added a FIXME
for doing this, but SerenityOS#18075 removed it.)

Instead of doing a binary search over all codes for every single
bit read, this implements the Moffat-Turpin approach described at
https://www.hanshq.net/zip.html#huffdec, which only requires a
table read per bit.

    hyperfine 'Build/lagom/bin/unzip ~/Downloads/enwik8.zip'
    1.008 s ± 0.016 s  =>  957.7 ms ± 3.9 ms, 5% faster

Due to issue SerenityOS#25005, we can't peek the full 15 bits at once but
have to read them one-by-one. This makes the code look a bit
different than in the linked article.

I also tried not changing CanonicalCode::from_bytes() too much.
It does 15 passes over all symbols. I think it could do it in
a single pass instead. But that's for a future change.

No behavior change (other than slightly faster perf).
timschumi pushed a commit that referenced this pull request Sep 14, 2024
Symbols that need <= 8 bits hit a fast path as of #18075, but
the slow path has done a full binary search over all symbols
ever since this code was added in #2963. (#3405 even added a FIXME
for doing this, but #18075 removed it.)

Instead of doing a binary search over all codes for every single
bit read, this implements the Moffat-Turpin approach described at
https://www.hanshq.net/zip.html#huffdec, which only requires a
table read per bit.

    hyperfine 'Build/lagom/bin/unzip ~/Downloads/enwik8.zip'
    1.008 s ± 0.016 s  =>  957.7 ms ± 3.9 ms, 5% faster

Due to issue #25005, we can't peek the full 15 bits at once but
have to read them one-by-one. This makes the code look a bit
different than in the linked article.

I also tried not changing CanonicalCode::from_bytes() too much.
It does 15 passes over all symbols. I think it could do it in
a single pass instead. But that's for a future change.

No behavior change (other than slightly faster perf).
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.

2 participants