Skip to content

Decode DEFLATE with zlib in the inflating reader - #2506

Draft
nikosavola wants to merge 4 commits into
KLayout:masterfrom
nikosavola:pr-inflate-zlib
Draft

nikosavola wants to merge 4 commits into
KLayout:masterfrom
nikosavola:pr-inflate-zlib

Conversation

@nikosavola

Copy link
Copy Markdown
Contributor

Draft, stacked on #2505: the first two commits are #2505's and drop out once it is merged and I rebase. New here are the last two commits.

  • InflateFilter now decodes with zlib raw inflate, the same way DeflateFilter already encodes with zlib, instead of its own Huffman decoder. BitStream and HuffmannDecoder are removed, about 400 lines less
  • zlib keeps its own window, so the output buffer is linear and the ring buffer with its rotation is gone
  • zlib takes the compressed input in chunks. The bytes behind the DEFLATE stream are pushed back through a new bypass_inflate flag on InputStream::unget, so the raw stream continues right after the block, and pos () does not count the bytes zlib has not decoded yet
  • this is the decoder for OASIS CBLOCKs and for .gz files
  • new tests for raw data behind a DEFLATE stream, pos () while inflating and truncated input. They pass with the old decoder too
  • second piece of the Inflate OASIS CBLOCKs in one go with zlib #2468 split
case before after speedup
OASIS read with CBLOCKs, 200k polygons (75 MB) 6.34 s 2.14 s 2.96x
OASIS read with CBLOCKs (vexriscv) 148.7 ms 87.6 ms 1.70x
OASIS read, 200k polygons (102 MB) 1.69 s 1.67 s 1.01x
OASIS read (vexriscv) 77.9 ms 78.7 ms 0.99x
GDS2 read (vexriscv) 144.2 ms 146.4 ms 0.99x

Xeon Gold 6248, gcc 13 -O2, before (#2505) and after alternated on one pinned core for 6 to 10 rounds, every round agrees. The uncompressed reads do not run the changed code, so their 1% either way is code layout.

- get was out-of-line, so byte-wise readers such as the OASIS varint decoder and the GDS2 record reader paid a call per get, two inside a CBLOCK
- the inline get serves bytes from the raw buffer and single bytes from the inflate ring buffer, refills and the end of the inflated data stay in get_slow
- the last ring slot is left to get_slow too: get rotates the ring there so the read pointer does not wrap, otherwise a following unget(1) asserts
- tlStream.h now needs the InflateFilter definition, so tlDeflate.h no longer includes tlStream.h and BitStream::get_byte moved to tlDeflate.cc

| case | before ms | after ms | speedup |
|---|---:|---:|---:|
| oasis_read_vexriscv_compressed | 145.0 | 132.4 | 1.09x |
| gds2_read_vexriscv | 135.9 | 127.8 | 1.06x |
| oasis_read_vexriscv | 71.5 | 69.9 | 1.02x |

Xeon Gold 6248, g++ 13 -O2, before and after alternated for 6 rounds on one pinned core, every round agrees.
- InflateFilter uses zlib raw inflate like DeflateFilter already uses zlib deflate, instead of its own Huffman decoder
- the output buffer is linear, zlib keeps its own window, so the ring buffer and its rotation are gone
- zlib reads the compressed input in chunks, the part behind the DEFLATE stream is pushed back with the new bypass_inflate flag of InputStream::unget so raw reading continues right after the block
- InputStream::pos does not count the chunk part zlib has not decoded yet, so error positions inside a CBLOCK stay close to where they were
- BitStream and HuffmannDecoder were only used by the old decoder and are removed
@stefanottili

stefanottili commented Nov 29, 2026 •

Copy link
Copy Markdown

Faster oas reading will be very much appreciated.

I found that writing oas with zlib compression set to 2 instead of the default 6 will create 5-10% larger oas, but both writing and reading can be up to 2x faster, a tradeoff well worth it.

Depending on the order of the reference tables and the cells written, different tools write the same oas with different files sizes anyways, so a slight file size increase due to zlib compression level 2 might even go unnoticed.
But the read/write speed up will be appreciated.

@chkkbim

chkkbim commented Dec 1, 2026

Copy link
Copy Markdown

Hi @nikosavola ,

[1] This PR seems to be an improvement over #2505, if I may phrase it that way.

  • What is the possibility you will submit yet another PR that improves upon this PR ?
  • let's remember each PR review comes out of the author's free time, time he will never get back

[2] If it takes, say 5 successive PRs to reach a state where no further improvement seems possible :

  • would it be possible for you to somehow reach that 5th PR, so that the author expends only 1 PR review ?
  • perhaps you could throw a bunch of AI models / agents that critique each other's solution/approach, and "prove" the solution is "final"

Hi @stefanottili ,

Would you be able to run @nikosavola 's solution on your various layout files (as many as possible) and tabulate the speedup ?

In the PR above, I think the sample size is only 2 layouts ?
We need more field data to evaluate the efficiency of the PR.

@stefanottili

stefanottili commented Dec 2, 2026 •

Copy link
Copy Markdown

Hi @klayoutmatthias

I've experimented with just one file, using Leo Mosers mpw gf180ws-run2/G802.oas:

you can find the latest reticle here: https://drive.proton.me/urls/3R8VCY9AK0#X0cfi4Qf8XFQ

It has 4M propnames, 503k duplicate klayout tile aref and I believe was written with klayout.
klayout tiling generating duplicate aref has recently been fixed and future mpw will not contain instance or net names as properties.

 file    type  struct top uref      sref  + aref   + path + polygon + text  = element  dbu/um
-------------------------------------------------------------------------------------------------------
   #1 a  oas    72348   1    0  39062950 9989396 13910582  63698978 450001  127111907  1000.0  reticle
  del               0                134  503477       32     37900      5     541548        
 prop               2            2510815     324  1368729    170102      0    4049970        

  sorted coordinates for 1 file: 38.6M coordinates, 15.9k buckets in 0.7s (cpu)

strm2oas -d=21 <input file> G802.oas

┌─────────────────────┬────────────────┬───────────────┬─────────────┐
│     input file      │   input size   │ read (master) │ read (zlib) │
├─────────────────────┼────────────────┼───────────────┼─────────────┤
│ G802.oas (original) │ 187.4/418.6 MB │ 24.0          │ 24.1        │
├─────────────────────┼────────────────┼───────────────┼─────────────┤
│ G802.2.oas          │ 181.3/490.4 MB │ 31.7          │ 31.3        │
├─────────────────────┼────────────────┼───────────────┼─────────────┤
│ G802.6.oas          │ 175.3/490.1 MB │ 31.5          │ 30.9        │
├─────────────────────┼────────────────┼───────────────┼─────────────┤
│ G802.oas strm2oas   │ 182.3/496.0 MB │ 31.5          │             │    
└─────────────────────┴────────────────┴───────────────┴─────────────┘

Using zlib for reading these files doesn't make a difference for strm2oas read times.

The unzipped file size does: 24 s for 418.6 MB, 31 s for 490 MB.
strm2oas uses various array repetition for coordinates.
My own program only uses rep 10/11.
This is 70 MB larger uncompressed, but leads to smaller overall file size.

Unlike strm2oas my own program reads its own output faster than strm2oas oas.
It also reads/writes zlib level 2 faster than level 6.

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.

3 participants