Skip to content

Faster single-core tile compression - #12

Merged
sputier merged 7 commits into
mainfrom
perf/1-single-core-codecs
Nov 14, 2026
Merged

sputier merged 7 commits into
mainfrom
perf/1-single-core-codecs

Conversation

@sputier

@sputier sputier commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Refs #1.

What changes

Single-core tile compression was slower than cfitsio (docs/comparison.md). This branch makes the codecs faster without changing the files they write.

  • Rice encoder: a 64-bit accumulator written four bytes at a time into a pooled buffer, room reserved once per block, the unary code and the low bits written as one number, the sum of the differences in an integer.
  • Rice decoder: a 64-bit window refilled eight bytes at a time, a Rice code taken with one shift, the zigzag mapping without a branch. An FS code that no encoder writes is now refused with a FitsException (it used to give pixels that depended on a register overflow).
  • Dequantization works on runs of the dither sequence that do not wrap, without a test for each value.
  • BitWriter (also used by the HCOMPRESS encoder) works through a 64-bit accumulator; the signs of the coefficients and the sizes of the quadtree codes are computed without a branch.
  • CompressionOptions.GzipLevel (new, default -1: the data of before): the zlib level of GZIP_1 and GZIP_2. At the default level the files are about 6 percent smaller than cfitsio's and take about twice as long to write; from .NET 9, level 2 gives about cfitsio's size and time. Ignored for tables.

Measured

Old (main) and new code in the same process, called in turn 21 times (the drift of the machine hits both the same way), 3536 x 3536 16-bit frame, one core:

Operation Gain
Rice encode, 16-bit tiles 2.2x to 2.4x
Rice encode, 32-bit tiles 2.9x
Rice decode, 16-bit tiles 1.2x to 1.4x
Rice decode, 32-bit tiles 1.4x
Dequantize to float 4.1x
HCOMPRESS encode 1.2x to 1.4x

BenchmarkDotNet, main against the branch, alternating: Rice compression 138 ms to 75 ms, decompression of the quantized float image 139 ms to 91 ms.

Checks

  • The tests keep the first encoder, decoder, bit writer and dequantization as references (tests/Eleris.FitsNet.Tests/Reference*.cs, DequantizeTests, BitWriterTests, RiceEncoderTests, RiceDecoderTests) and compare bytes and bit patterns, including a differential fuzz on damaged data. 2003 tests pass on net8.0 and net10.0.
  • A code review found nothing critical or major; its minor findings are fixed in af48774.

Not in this PR

docs/comparison.md still shows the figures measured before these changes. The new figures against cfitsio need a machine with nothing else running (other work on the machine made the measures of the last attempt unusable), so #1 stays open until the page is refreshed.

The encoder writes through a 64-bit accumulator four bytes at a time into
a pooled buffer, reserves room once for each block, merges the unary part
with the low bits into one write and sums the differences in an integer.
The bytes are those of the previous encoder, which the tests keep as a
reference. One core, 3536 x 3536 16-bit frame: 138 ms to 66 ms (cfitsio:
68 ms).
…st for each value

The Rice decoder reads from a 64-bit window refilled eight bytes at a
time, takes a unary code and the low bits with one shift, and undoes the
zigzag mapping without a branch. A code that no encoder writes (an FS
code above the maximum for 4-byte pixels) is now refused with a
FitsException instead of giving pixels that depended on a register
overflow. The dequantization works on runs of the dither sequence, which
has no wrap inside: 60 ms to 13 ms for a 3536 x 3536 float frame, the
same values bit for bit (the first versions are kept in the tests).
The bit writer works through a 64-bit accumulator four bytes at a time.
The signs of the coefficients and the bits of the quadtree codes are
counted without a branch. CompressionOptions.GzipLevel (-1 by default,
the same bytes as before) chooses the zlib level of GZIP_1 and GZIP_2:
from .NET 9 level 2 gives files about as large as cfitsio's in about the
same time.
The Rice reader never fills its window to 64 bits (a shift by 64 is a
shift by 0). The unary quotient of the encoder is unsigned, the initial
capacity is computed without overflow, and the buffers are returned when
an allocation fails. New tests: an FS code that no encoder writes is
refused, short random buffers decode like the first decoder up to the
last bit, tiles compressed on all cores give the bytes of one core. The
documentation says that GzipLevel is ignored by tables.
@sputier
sputier force-pushed the perf/1-single-core-codecs branch from 69b49ad to bfdb208 Compare November 14, 2026 21:03
@sputier
sputier merged commit ab114ca into main Nov 14, 2026
5 checks passed
@sputier
sputier deleted the perf/1-single-core-codecs branch November 14, 2026 21:16
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.

1 participant