← Back to Pull requests

perf(grib): an open skips the data of a message's fields, and a remote dataset keeps no message it read for its nodata #1264

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 24, 2026 12:09 UTC
Author

Opening a GRIB file no longer reads the data of a message's fields, and a remote dataset no longer keeps the GRIB1 messages it reads whole to work out their nodata. In a GRIB2 message of several fields, each field's sections come after the data of the field before it: to reach them, the open read that data (48 of the 64 MiB of a message of four 16 MiB fields), and a remote dataset kept it for as long as it was open. The open now skips the bodies of the data, bitmap and local use sections by their lengths, on both readers. That message's remote open now fetches 0.02 MiB in 4 requests, where it fetched 48.00 MiB in 22; its local open reads 0.02 MiB, where it read 48.01 MiB. A GRIB1 message whose nodata depends on its values is still read whole at open to work out its nodata, and is then dropped: eight such messages of 4.2 MiB held 33.7 MiB after a remote open and now hold 0.1 MiB, and a read of one of their bands fetches its message again.

Fixes #1215.

Behaviour

  • The walk of a message's header. A GRIB2 message's header is the first octets of each section and its definitions (sections 1, 3, 4 and 5) whole. The walk asked for every byte up to the next section, so the bodies of the data, bitmap and local use sections before the last were read too. It now reads the parts the header needs, each section where the length of the one before puts it, and holds them as runs of the message's bytes. The sections are parsed from the runs as from the whole message: message_fields, which the in-memory reader and every decode use, parses a whole message as one run, with the same checks in the same order, so the bands, metadata, nodata and errors are those of the message read whole.

  • Local files: the open reads a page for each field after the first, through its buffer, instead of the data of the field before it. The header of a message is dropped before the next one is read, as before.

  • Remote files: each part is fetched with at least 512 bytes, so that a field's sections 4 to 7 come in one request (main asked for each section of a field after a skipped part separately). The extra bytes stop short of the message's end marker, and the next message's first request is sized from the bytes the header needed, as on main: a damaged end marker fails a remote open where it did before. The dataset keeps of each message the bytes read in a row from its start, which a read of its bands does not fetch again, as on main, but no longer the data of skipped fields, nor the zeros that stood for a skipped GRIB1 bitmap (2.0 MiB for the eight messages of the issue's script).

  • GRIB1 messages whose nodata depends on their values (a bitmap, and a packing whose range holds 9999): a remote open still reads each one whole, decodes it and works out its nodata, then drops it, as the local reader does; one is held at a time. A read of its band fetches it again, past the bytes the open read first. The open's steps keep their order (every header, then those messages, then the bands, then the dataset's header), so a damaged file fails with the error it failed with before.

  • Unchanged: GRIB1 headers (the sections before the bitmap, then the data section's first octets, the bitmap's body held as zeros while the open describes the message's band), what a read of a band decodes (its message, whole), values, nodata and metadata. A remote read fetches the part of the message the dataset does not keep, which is now all of it past its first bytes for a message of several fields (see Limitations).

  • Docs: docs/formats.md (GRIB: a request for each further field of a message, the GRIB1 messages read whole and dropped, a message of several fields fetched for each band read on its own, a local open skipping the fields' data) and docs/remote-data.md (what a remote open reads).
  • Before and after

    The issue's script, which serves the files from a loopback server and counts the bytes it sends. On main (e44e1126) it prints:

    terrio 0.9.0
    file                          size bands    nodata   the open: requests, MiB fetched held after the open   read of band 1, one pixel: MiB fetched
    fields.grib2              64.0 MiB     4      none         22 requests,  48.00 MiB          48.1 MiB    16.00
    messages.grib2            64.0 MiB     4      none          4 requests,   0.06 MiB           0.1 MiB    15.98
    nodata-from-values.grb    33.7 MiB     8 9.999e+20         24 requests,  33.68 MiB          33.7 MiB     0.00
    nodata-fixed.grb          33.7 MiB     8      9999         16 requests,   0.02 MiB           2.0 MiB     4.19
    

    On this branch:

    terrio 0.9.0
    file                          size bands    nodata   the open: requests, MiB fetched held after the open   read of band 1, one pixel: MiB fetched
    fields.grib2              64.0 MiB     4      none          4 requests,   0.02 MiB           0.1 MiB    63.98
    messages.grib2            64.0 MiB     4      none          4 requests,   0.06 MiB           0.1 MiB    15.98
    nodata-from-values.grb    33.7 MiB     8 9.999e+20         24 requests,  33.68 MiB           0.1 MiB     4.19
    nodata-fixed.grb          33.7 MiB     8      9999         16 requests,   0.02 MiB           0.1 MiB     4.19
    

    fields.grib2 is one message of four 16 MiB fields and messages.grib2 the same fields as four messages; the GRIB1 files hold eight messages with bitmaps, whose nodata depends on their values in nodata-from-values.grb. A read of band 1 of fields.grib2 now fetches the message past its first 16 KiB, so the open and that read fetch 64.00 MiB, as before; a read of band 1 of nodata-from-values.grb fetches its 4.2 MiB message again. Reads of the other bands of fields.grib2 fetch the message again too (see Limitations).

    Open times, medians of three rounds of 20 opens in one process (10 for the remote files and nodata-from-values.grb, 5 for remote many.grib2, six rounds of 60 for local many.grib2 and messages.grib2), the remote files served by the same loopback server:

    | File | main | This branch |
    | --- | --- | --- |
    | fields.grib2, local | 18.89 ms | 0.40 ms |
    | messages.grib2, local | 0.38 ms | 0.38 ms |
    | many.grib2 (696 messages of one 360 x 181 field), local | 24.72 ms | 24.34 ms |
    | nodata-fixed.grb, local | 0.45 ms | 0.46 ms |
    | nodata-from-values.grb, local | 212.97 ms | 214.20 ms |
    | fields.grib2, remote | 110.18 ms | 3.37 ms |
    | many.grib2, remote | 495.98 ms | 493.04 ms |
    | nodata-fixed.grb, remote | 11.77 ms | 12.03 ms |
    | nodata-from-values.grb, remote | 284.69 ms | 278.53 ms |

    An open, then a read of one pixel of each band, in a fresh process (medians of six runs): the MiB fetched by the open and by the reads, and their seconds.

    | Remote file, bands read | main | This branch |
    | --- | --- | --- |
    | fields.grib2, one at a time | 48.00 + 64.00 MiB, 0.193 + 0.921 s | 0.02 + 255.94 MiB, 0.004 + 1.220 s |
    | fields.grib2, together | 48.00 + 16.00 MiB, 0.167 + 0.569 s | 0.02 + 63.98 MiB, 0.004 + 0.767 s |
    | uv.grib2 (one message of two 1440 x 721 fields), one at a time | 1.98 + 3.96 MiB, 0.014 + 0.055 s | 0.02 + 7.89 MiB, 0.003 + 0.063 s |
    | uv.grib2, together | 1.98 + 1.98 MiB, 0.014 + 0.046 s | 0.02 + 3.94 MiB, 0.003 + 0.047 s |
    | nodata-from-values.grb, one at a time | 33.68 + 0 MiB, 0.295 + 0.233 s | 33.68 + 33.66 MiB, 0.292 + 0.292 s |
    | nodata-fixed.grb, one at a time | 0.02 + 33.66 MiB, 0.014 + 0.350 s | 0.02 + 33.66 MiB, 0.014 + 0.329 s |

    The memory of the open, in the same runs (MiB: the peak during the open, then what the dataset holds after it):

    | File | main | This branch |
    | --- | --- | --- |
    | fields.grib2, local | 63.2, 0.0 | 0.0, 0.0 |
    | fields.grib2, remote | 79.5, 48.1 | 0.1, 0.1 |
    | nodata-from-values.grb, remote | 56.0, 33.8 | 26.6, 0.2 |
    | nodata-fixed.grb, remote | 2.1, 2.1 | 2.1, 0.2 |

    Reads that each decode their band, in one process (TERRIO_CACHEMAX=0, the bands in turn; medians of three rounds):

    | File | main | This branch |
    | --- | --- | --- |
    | messages.grib2, local (a 16 MiB message per read) | 133.25 ms | 128.56 ms |
    | fields.grib2, local (the 64 MiB message per read) | 161.08 ms | 159.03 ms |
    | uv.grib2, local | 21.85 ms | 21.70 ms |
    | nodata-fixed.grb, local | 30.64 ms | 29.56 ms |
    | nodata-from-values.grb, local | 27.06 ms | 26.78 ms |
    | uv.grib2, remote | 24.20 ms | 26.12 ms |

    Local reads, whose code this branch changes only in parsing a whole message as one run, take no longer than on main. A remote read of a band of uv.grib2 fetches its whole 3.94 MiB message, where main fetched the 1.98 MiB past the field the open kept.

    Tests and mutations

  • a_message_of_several_fields_is_opened_without_their_data (local.rs): one message of four 512 x 256 Float32 fields is opened reading at most five pages, and its bands are those of the message read whole. With the data sections read whole, the open reads all 2,097,498 bytes of the message.

  • a_grib2_header_skips_the_bodies_of_its_sections (local.rs): the header of the two fields of two_bands_with_bitmap.grib2 as one message, the bodies of its local use, bitmap and data sections grown by 10,000 bytes each, is read without touching any of them (under 2,000 bytes), and describes the bands as the whole message does.

  • a_lazy_grib_open_holds_one_message_header_at_a_time (tests/declared_size.rs) also checks that the open allocates nothing the size of a field (fields of 64 KiB; with the data sections read whole, its largest allocation is 262,486 bytes).

  • test_remote_open_skips_the_data_of_a_message_s_fields (Python): four fields of 200,000 bytes in one remote message cost the first block and under 3 KiB more at open. On main it fails: 600,342 bytes.

  • test_remote_grib1_by_range now also checks that a read of a band whose message was read whole at open fetches it again (on main, [1] fails: the read fetches nothing, the message being kept); its ceiling and that of test_remote_grib1_headers_skip_bitmaps count those messages twice.
  • Each guarantee broken on purpose, then restored (the tree checked clean after each). Four of them (the data sections read whole, the walk by the bytes at hand, the remote GRIB1 message kept, and the read-ahead reaching the end marker) were run again on the branch rebased on main, with the same result, the read-ahead's 48 outcomes differing from main's:

    | Mutation | Caught by |
    | --- | --- |
    | A GRIB2 header needs the bodies of its data sections | a_grib2_header_skips_the_bodies_of_its_sections, a_message_of_several_fields_is_opened_without_their_data, a_local_file_is_read_by_message, and in declared_size.rs a_lazy_grib_open_allocates_nothing_the_size_of_its_file and a_lazy_grib_open_holds_one_message_header_at_a_time |
    | ... the bodies of its bitmap sections | a_grib2_header_skips_the_bodies_of_its_sections |
    | ... the bodies of its local use sections | a_grib2_header_skips_the_bodies_of_its_sections |
    | The skipped parts held as zeros in one run (nothing more read) | a_lazy_grib_open_holds_one_message_header_at_a_time |
    | The walk goes on past the bytes at hand of a cut section instead of by its length | a_grib2_header_skips_the_bodies_of_its_sections, a_file_is_read_by_position_as_in_memory, a_message_of_several_fields_is_opened_without_their_data, the_fields_of_one_message_read_together_read_it_once |
    | A local GRIB2 message's end marker left unchecked when its header does not reach it | a_file_is_read_by_position_as_in_memory |
    | A bitmap section's indicator octet not read with its header | a_grib2_header_skips_the_bodies_of_its_sections, a_message_of_several_fields_is_opened_without_their_data, a_file_is_read_by_position_as_in_memory, the_fields_of_one_message_read_together_read_it_once, and in declared_size.rs a_lazy_grib_open_holds_one_message_header_at_a_time |
    | A header's adjacent parts kept as separate runs, never joined | the same five, and a_remote_grib_without_a_grib_extension_is_read_by_range |
    | A remote GRIB1 message read whole for its nodata kept | test_remote_grib1_by_range[1] |
    | A remote GRIB1 header kept with the zeros of its bitmap | test_remote_grib1_by_range[4], test_remote_grib1_headers_skip_bitmaps[1] and [7] |
    | The remote read-ahead reaching the end marker | the remote differential check below: 48 of 19,720 outcomes differ |
    | The next remote message's first read sized from the first run, read-ahead included | the remote differential check: 16 outcomes differ |

    #1163's differential check, now on main (every byte of the small fixtures and of the GRIB1 file of four messages damaged four ways, and the file cut there, read by position and in memory), with four messages of several fields made of two-message fixtures added: 341,895 cases, 127,351 of them errors, the same by position and in memory in every case, on this branch and on main, and the in-memory outcomes of the two the same in every case. A remote one, temporary like it: damaged files (four messages of two fields whose first field's data is padded past a block, a GRIB1 file of four messages with bitmaps, and a file of a GRIB1 message, the padded message and a 15,501-byte message), served by range, opened and read band by band with main's build and this branch's: 19,720 cases, the same outcome (bands, metadata, values or error) in every case.

    Checks run

    On this branch's code (a0a0c5ac: two commits on main e44e1126), with Rust 1.95 unless said:

  • cargo fmt --all -- --check: passed.

  • cargo clippy -p terrio -p terrio-py --all-targets --no-deps -- -D warnings passes with Rust 1.99.1, and with Rust 1.95 with -A for the four lints that main has elsewhere in terrio (nonminimal_bool, collapsible_match, collapsible_if, drop_non_drop: 25 in 18 files, none of which this branch changes). cargo clippy --workspace --all-targets -- -D warnings passes with Rust 1.99.1.

  • cargo test -p terrio: 3,669 passed, 55 ignored; the GRIB and block cache unit tests (48) and tests/declared_size.rs (39) also on their own.

  • RUSTFLAGS="-D warnings" cargo hack check -p terrio --each-feature --no-dev-deps --exclude-features gdal: passed. cargo +1.90 check --workspace --all-targets --locked: passed. cargo build -p terrio --target wasm32-unknown-unknown: passed, with no warning in the GRIB driver. RUSTDOCFLAGS="-D warnings" cargo doc -p terrio -p terrio-crs --no-deps: passed.

  • python -m pytest tests/test_grib.py tests/test_grib_gdal.py tests/test_resource_limits.py tests/test_remote_reads.py, with the extension built from a0a0c5ac: 236 passed, 19 skipped, 3 xfailed. The whole Python suite (-n 6 --dist loadfile): 9,991 passed, 2,286 skipped, 4 xfailed, 2 xpassed.

  • ruff check and ruff format --check on the Python test file changed: passed. reuse lint: compliant. No Python package code changes.
  • Limitations

  • A remote message of several fields is fetched whole for each decode of its bands, as a local one is read for each: the open no longer keeps the data of its fields, which made later reads of that message cheaper. Reading the four bands of fields.grib2 one at a time fetches 255.9 MiB in 1.22 s, where main fetched 48.0 MiB at open and 64.0 MiB for the reads (1.11 s in all); for the two fields of uv.grib2, 7.9 MiB where main fetched 5.9 MiB. Bands read together are decoded from one fetch of their message: 64.0 MiB for fields.grib2, as many as main fetched in all (0.77 s, against 0.74 s). Keeping a fetched message of several fields for the next read of its bands, within the block cache's budget, would avoid the refetch; it is not part of this change.

  • A GRIB1 message read whole at open for its nodata is fetched again when its band is read remotely: for the issue's eight messages, an open and a read of every band fetch 33.68 + 33.66 MiB in 0.58 s, where main fetched 33.68 MiB in 0.53 s. Keeping the message would keep the file.

  • A read of a band reads its message whole, all the fields of a message of several fields included, as before.

  • A GRIB1 header holds zeros for the body of its bitmap while the open describes its band. The local open holds one message's header at a time and the remote open every header until the dataset's header is built, as before; neither keeps them after.
  • <!-- This is an auto-generated comment: release notes by coderabbit.ai -->

    <!-- coderabbit-ai-generated:v1 -->

    Summary by CodeRabbit

    Improvements
    GRIB datasets can now be opened when message headers are split across separate byte ranges or read in partial sections.
    Opening local GRIB2 files skips field data between section headers, reducing unnecessary data reads.
    Remote GRIB reads now fetch field data as needed, with clearer handling of multi-field messages and GRIB1 messages that require data to determine nodata values.

    Documentation
    Updated GRIB guidance to clarify remote request behavior and data reads during open and band access.

    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: github-actions[bot] Review duration: 4m
    All Vouch review confidence checks passed
    Residual risk 0.42 = Change Risk (0.52) × (1 − Review Confidence (0.19)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 26% · Reviewer familiarity: 35%
    19%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #1264"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: medium confidence: 19.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 19.5% · Lower = rushed or shallow review
    19.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 41.9% · Moderate risk
    41.9%