Skip to content

perf(gzip): index BytesView directly in crc32_update - #388

Closed
mizchi wants to merge 1 commit into
moonbitlang:mainfrom
mizchi:pr-gzip-crc32-index-loop
Closed

perf(gzip): index BytesView directly in crc32_update#388
mizchi wants to merge 1 commit into
moonbitlang:mainfrom
mizchi:pr-gzip-crc32-index-loop

Conversation

@mizchi

@mizchi mizchi commented May 23, 2026

Copy link
Copy Markdown
Contributor

This proposal is superseded by #475, merged on July 3, 2026. That change replaced the byte-at-a-time CRC32 loop with a slicing-by-eight implementation in 007b3ab.

The original May proposal replaced iterator-based byte traversal with direct indexing. Current main already processes eight bytes per iteration and indexes the remaining tail directly, so porting the old loop would replace the newer algorithm. The original benchmark numbers no longer describe the implementation on main.

The current native release gzip and gzip-internal suites pass 39/39 with moonc v0.10.12+1634b282e (2026-09-07):

moon test src/internal/gzip_internal src/gzip --target native --release

Closing this obsolete proposal in favor of the implementation already merged in #475.

crc32_update iterates the input via 'for byte in chunk' where chunk
is a BytesView. That desugars through BytesView::iter + Iter::next,
which allocates an iterator closure and pays a virtual dispatch per
byte. A gzip_roundtrip callgrind profile attributes ~16% of total
instructions to that single loop (BytesView::iter 9.80% +
Iter::next 6.07%), even though the loop body is a couple of
arithmetic ops and a table lookup.

Pull out the backing Bytes + start offset + length once and index
the raw Bytes directly. Bytes[i] is intrinsic and inlined.

gzip_roundtrip bench (native, 3-run median):
  baseline: 178 ms
  patched : 162 ms  (-9.0%)
@Guest0x0

Copy link
Copy Markdown
Collaborator

For builtin, array-like data structure, for .. in has a clearer shape, and is hence easier to optimize. For Array, FixedArray etc., the compiler can already specialize for .. in into faster-than-hand-written (due to omitted bound check) loop. So writing the loop as for .. in would be the better approach in the long term IMO. If for .. in for BytesView has a performance problem, I would flag that as a missing compiler optimization

@Guest0x0

Copy link
Copy Markdown
Collaborator

It seems that for .. in (_ : BytesView) can now be optimized on nightly. Could you try your experiment again with nightly?

@mizchi mizchi closed this Sep 12, 2026
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