Skip to content

perf(data): GeoPackage export uses 16 KiB pages where they pack rows far better - #3038

Merged
Mavrick-F merged 2 commits into
opengeos:mainfrom
Mavrick-F:perf/gpkg-wide-pages
Nov 26, 2026
Merged

Mavrick-F merged 2 commits into
opengeos:mainfrom
Mavrick-F:perf/gpkg-wide-pages

Conversation

@Mavrick-F

@Mavrick-F Mavrick-F commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Summary

Exporting 100,000 buffered points (polygons of about 2.1 KB a row) writes a 417 MB GeoPackage, nearly half of whose feature table is empty: only one such row fits in SQLite's default 4 KiB page. Writing and syncing that space is a good part of the export.

A new file is now made with 16 KiB pages when its rows pack much better in them.

  • The choice. Before the first write, the export lays out the rows of its first 1,000 features in both page sizes, as SQLite fills a table (overflow pages included). It takes 16 KiB when that, plus what the file's other tables grow by, needs under nine tenths of the space. Otherwise the file keeps 4 KiB pages.
  • Why not always 16 KiB. Which size wastes less depends on the row sizes. In SQLite with synthetic rows, 16 KiB pages gave a file 0.58× the size for 2.1 KB rows, but 1.9× for 8.3 KB rows, and 1.8× when three rows in ten were that big among 150-byte ones. The estimate keeps both of those at 4 KiB; modelled the same way outside the writer, it came within 5% of real file sizes from 900-byte rows up.
  • What changes. The two buffer cases below get 16 KiB pages. The benchmark's building polygons (about 170 bytes of geometry) and its zones (7.6 KB) stay at 4 KiB, and their files are byte for byte what main writes.
  • A table added to or replaced in an existing GeoPackage keeps that file's page size.
  • A streamed export decides from its first run (up to 32 MiB of features), so it chooses as a whole export does unless that run holds only part of the first 1,000 features (a definition query that leaves few of them, for one).
  • A feature's box was computed separately for its blob header, its index entry and the table's extent. It is now computed once.

Trade-off: in a 16 KiB file, a later edit rewrites 16 KiB for each page it changes instead of 4. Page size is part of the SQLite file format, and the browser's GeoPackage reader takes any size.

Safety: journal and sync settings are unchanged. A new file is still built beside the destination, synced, and moved into place without replacing anything.

Memory: the estimate holds 1,000 integers (8 KB) and copies no layer data. Peak memory is unchanged.

Not done: a larger page cache gained nothing when measured. Encoding rows on several threads would save little (geometry encoding is around 0.1 s of CPU here; the rest of the insert is SQLite on its one connection). Loading the R-tree, about 0.5 s on one thread, is the biggest part of the writer's own work left, and would be a separate change.

Surfaces: this is the one GeoPackage writer, so the desktop app, rustgis-cli, Python, the Assistant, MCP, plugins and the phone get it. The browser doesn't write GeoPackage. No command or parameter changes. Nothing new to undo: an export doesn't change the project.

Testing

Timings. Release rustgis-cli at e48daeb, before and after: layer.addData, gp.run buffer 5 km, share.exportLayer to a new .gpkg, the whole rustgis-cli run. Medians of 7 rounds after a warm-up, before and after alternating, through the host queue on a shared 8-thread machine.

Input Wall Fastest run CPU (user + sys) Peak RSS File
UTM points 3.84 → 3.10 s (−19%) 3.40 → 2.58 s 7.24 → 6.66 s 499 → 499 MB 416.7 → 240.1 MB
lon/lat points 4.46 → 3.29 s (−26%) 3.90 → 2.99 s 10.64 → 10.17 s 264 → 264 MB 416.7 → 240.1 MB

Where the export's time went (UTM, an instrumented build with each page size, medians of 5):

Step 4 KiB 16 KiB
Row inserts (one thread) 0.74 s 0.45 s
R-tree load (one thread) 0.57 s 0.48 s
Close and sync (disk) 0.85 s 0.50 s
Together 2.16 s 1.43 s

The rest of the 2.8 s run in that build is loading, buffering, and converting to longitude/latitude (the last two on several threads).

Reading the file back (5 rounds): adding it as a loaded layer takes 0.74 → 0.46 s. Adding it streamed and drawing a view takes 2.03 → 1.93 s, and the two PNGs are identical.

Same contents. With SOURCE_DATE_EPOCH set, sqlite3 .dump of the files written before and after is identical for both inputs, rows and R-tree nodes included; only the page layout differs. PRAGMA integrity_check and rtreecheck pass, an independent GeoPackage validator reports nothing, and spatial-filter counts match.

Test. geopackage_pages_follow_the_row_size checks the page size and the read-back for small, 2 KiB, large, mixed, text-only and empty layers, written whole and streamed, and that adding a table leaves an existing file's pages alone. It fails with the choice switched off (4096 where 16384 is expected).

The full CI run on the fork passed on this head.

UI evidence

No visible UI change.

Related issue

Part of #1227.

Summary by CodeRabbit

  • Improvements
    • GeoPackage exports now use consistent geometry bounds for feature data, layer extents, and spatial indexes in both streamed and whole-layer exports.
    • New GeoPackages can select a page size based on the data, which may reduce storage space. Existing GeoPackages retain their current page size.

…far better

A row of about 2 KiB takes a whole 4 KiB page of a new GeoPackage and leaves half
of it empty. The export now packs its first 1,000 rows into pages of both sizes and
makes the file with 16 KiB pages when they need under nine tenths of the space.
A feature's box is also computed once, not for its blob, the index and the table
separately.
@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

GeoPackage export estimates page size from initial features when creating a database. It computes geometry extents during insertion, reuses them in geometry blobs, and registers the accumulated layer extent.

Changes: GeoPackage export

Layer / File(s) Summary
Initial-feature page sizing
crates/data/src/wkb.rs, crates/data/src/source/gpkg.rs, crates/data/src/source/gpkg/add.rs, crates/data/src/source/tests.rs
Creation estimates row storage from up to 1,000 initial features. It selects 16 KiB pages when table space plus overhead is below 90% of the 4 KiB estimate; otherwise, SQLite’s default page size remains. Streamed exports pass the first nonempty batch to creation. Tests cover whole and streamed writes, empty and mixed-size rows, and adding a table to an existing file.
Inserted geometry extents
crates/data/src/source/gpkg.rs
Insertion computes each geometry extent and reuses it for the GeoPackage blob and accumulated layer extent. Whole-layer export registers the accumulated extent instead of ds.extent().

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Refactor

Possibly related PRs

  • opengeos/RustGIS#1403: Adds table add and replace modes for existing GeoPackages, which connect to the page-size preservation behavior.

Suggested reviewers: giswqs, flight1973

Merge Risk: Minimal · up to 06d7f

GeoPackage export now picks 16 KiB pages only when they pack rows much better, and the registered layer extent is unchanged. No actionable merge risk was found, and existing files keep their page size.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: GeoPackage export now selects 16 KiB SQLite pages when they pack rows much better, which matches the summary, the objectives, and the description. It follows the conve…
Description check Passed The description follows the repository template closely. The Summary explains what changes and why, with the page-size choice, the trade-offs, and the safety and memory notes. The Testing section give…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: GeoPackage export now selects 16 KiB SQLite pages when they pack rows much better, which matches the summary, the objectives, and the description. It follows the conventional-commit style and is specific, so a teammate scanning history would understand the primary change.

Full details: Description check

Explanation

The description follows the repository template closely. The Summary explains what changes and why, with the page-size choice, the trade-offs, and the safety and memory notes. The Testing section gives timings, file comparisons, integrity checks, and a named test. The UI evidence section correctly states 'No visible UI change.' The Related issue section gives 'Part of #1227.' The template's comment text was removed, which is expected. All required sections are filled in.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks the pages with care,
Small rows leave room to spare.
Each shape lends its bounds anew,
The blob and layer share them too.
I thump beside the export run,
Then nibble when the work is done.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 12c43aee-be7b-4239-846e-36906909005f
Commits

Reviewing files that changed from the base of the PR and between e7994c8 and 06d7fec.

Files selected for processing: 1

  • crates/data/src/source/gpkg.rs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 1 remain after this review.

Comment thread crates/data/src/source/gpkg.rs Resolved
@github-actions

Copy link
Copy Markdown
Contributor

Code review

Bugs: None found. I checked the following:

  • insert runs the redo checks before it unions the extent, and each restart in fill starts with a fresh extent.
  • Extent::union treats an empty extent as the identity, so the per-feature union matches the old ds.extent() and the old per-batch union.
  • table_bytes follows SQLite's leaf and overflow rules (M/X/K local payload sizes). The OTHER_PAGES term makes the 0.9 comparison match the real file-size difference.
  • The test expectations for small, middling, large and text agree with the estimate when I work them through by hand.

Security: None found. The page size is an integer from a fixed set and goes into a format! as a constant.

Performance:

  • The streamed export can make the page-size decision from an empty run, which silently keeps 4 KiB pages. Inline comment on crates/data/src/source/gpkg.rs:819. Medium-high confidence that the path is reachable, based on reading export.rs, not a running export.

Quality (not posted inline; for the release quality pass):

  • The doc comment on geopackage_pages_follow_the_row_size (tests.rs:250) reads awkwardly ("whose rows 16 KiB pages hold"). Low.
  • The estimate leaves out the R-tree's pages. Its size is roughly the same at either page size, so the omission is probably harmless. Low.

AGENTS.md: No breaks found.

  • Wasm: source/mod.rs:23 gates gpkg to non-wasm, so page_size and the pub(crate) wkb_size never reach the browser build.
  • Browser reader: sqlite.rs:67-70 accepts any power-of-two page size from 512 up, so 16 KiB files open there.
  • Surfaces: no command or parameter changes, so command-surface.txt and the ribbon are untouched. The PR body addresses the surfaces.
  • Memory: the change adds no layer copy. The PR body states peak memory and gives no layer multiple. Nothing new holds layer data, so this doesn't break the rule.
  • Cancel: the insert loop already calls progress::step, and the export already runs in the background.

Twins checked:

  • add.rs ignores first and keeps the existing file's page size, which is correct for add and replace.
  • write_table_in and write_features_in both reach create, so the new page choice covers both whole-layer and streamed writes.
  • gpkg_mem.rs is a reader only, so there is no second GeoPackage writer.

Previously raised concerns: None. prior-reviews.json has no threads, so I left none alone.

Change size

Compared with main (merge base e185e02 to PR head e7994c8). Four files were modified; none were added, removed or renamed.

Measure Value
Files changed 4 (added 0, modified 4, removed 0, renamed 0)
Size before 402.2 KB
Size after 408.4 KB
Total size change +6.2 KB
File Before After Change
crates/data/src/source/gpkg.rs 71.5 KB 74.8 KB +3.3 KB
crates/data/src/source/tests.rs 297.5 KB 300.4 KB +2.8 KB
crates/data/src/source/gpkg/add.rs 16.2 KB 16.2 KB +22 B
crates/data/src/wkb.rs 17.0 KB 17.0 KB +11 B

…h rows

A chunk a definition query emptied said nothing about the rows to come, but
the table was made from it: 4 KiB pages for the whole file, and no
multipoints seen. The table now waits for the first run with rows; an empty
layer's is still made at the end of the feed.
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

📖 Docs preview

Item Value
Preview https://preview.rustgis.app/pr-3038/
Web app https://preview.rustgis.app/pr-3038/app/
Commit 06d7fec

GitHub Pages takes a minute or two to publish a new commit. Newer commits from the fork aren't published until a maintainer adds the preview label (once per commit).

geometry.saturating_add(values.sum())
})
.collect();
let wide = table_bytes(&rows, WIDE_PAGE).saturating_add(OTHER_PAGES * (WIDE_PAGE - PAGE));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The page-size comparison is not the one the doc comment describes. The wide side adds the other tables' growth (OTHER_PAGES * (WIDE_PAGE - PAGE)), but the narrow side carries no baseline for those same pages. The doc asks for the whole file's space to be compared, which is wide_table + OTHER_PAGES * WIDE_PAGE against narrow_table + OTHER_PAGES * PAGE. The current form makes 16 KiB win about 6 KiB of table space too easily near the 90% line (roughly OTHER_PAGES * PAGE / 10 of the threshold). The test rows sit well away from that line, so the tests don't catch it.

If the current threshold was tuned on purpose, the doc should say which baseline it uses. Otherwise the fix is:

Suggested change
let wide = table_bytes(&rows, WIDE_PAGE).saturating_add(OTHER_PAGES * (WIDE_PAGE - PAGE));
let wide = table_bytes(&rows, WIDE_PAGE).saturating_add(OTHER_PAGES * WIDE_PAGE);
let narrow = table_bytes(&rows, PAGE).saturating_add(OTHER_PAGES * PAGE);
(wide.saturating_mul(10) < narrow.saturating_mul(9)).then_some(WIDE_PAGE)

I have not run the tests with this change. The calibration numbers in the PR description came from the current formula, so re-measure before changing it.

Category: quality

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This form is deliberate, and it is what the doc says: the feature table's rows at 16 KiB, "counting what the other tables' pages grow by", against the rows at 4 KiB, within nine tenths. It compares what the choice changes rather than the whole file, so the threshold doesn't loosen with the fixed baseline, and the calibration in the PR body was measured with it. If the doc should spell that out more plainly, that is left for the release quality pass.

@github-actions

Copy link
Copy Markdown
Contributor

Code review

The PR makes GeoPackage exports use 16 KiB pages when rows pack much better, and computes each feature's box once. I found no crash paths, no silent losses, and no unwrap/expect/unsafe in the non-test code. The fix for the earlier empty-run thread is in 06d7fec, and the code matches it.

Bugs

  • None found.

Security

  • None found.

Performance

  • None found. The page-size estimate samples at most 1,000 features and copies no layer data. The per-feature extent is now computed once instead of twice.

Quality

  • Page-size threshold compares unlike baselines (crates/data/src/source/gpkg.rs:1000, medium confidence). The 16 KiB side adds the other tables' growth, but the 4 KiB side has no baseline for those pages. This shifts the choice by about 6 KiB of table space near the 90% line. The tests sit well away from that line, so they don't catch it. I left the fix as a suggestion, since the PR's calibration numbers came from the current formula.

AGENTS.md

  • None found. Checked: no new long loop (so no Cancel gap), no new unwrap/expect, no available_parallelism or thread spawns, nothing cfg-gated that changed, and the browser reader accepts 16 KiB pages (sqlite.rs:67-71).

Twins checked

  • add.rs (add or replace table): its make ignores first, so an existing file keeps its page size, as the PR says.
  • build vs fill: both pass the features to create and use the same extent path. ds.extent() and the new per-insert union give the same box.
  • wkb::wkb_size vs encode: the existing test checks they match.
  • Reader: sqlite.rs and the desktop reader both handle 16 KiB pages.
  • Browser (rustgis-web): GeoPackage writing is desktop-only, so the browser isn't affected.

Already raised

  • I left one earlier concern alone. It is the empty-first-run page-size issue in gpkg.rs, which the author fixed in 06d7fec and which is still resolved.

Change size

Compared with main (merge base e185e02 to PR head 06d7fec).

Measure Value
Files changed 4 (0 added, 4 modified, 0 removed, 0 renamed)
Size before 402.2 KB
Size after 408.7 KB
Total size change +6.5 KB
File Before After Change
crates/data/src/source/gpkg.rs 71.5 KB 75.1 KB +3.6 KB
crates/data/src/source/tests.rs 297.5 KB 300.4 KB +2.8 KB
crates/data/src/source/gpkg/add.rs 16.2 KB 16.2 KB +22 B
crates/data/src/wkb.rs 17.0 KB 17.0 KB +11 B

@Mavrick-F
Mavrick-F enabled auto-merge November 26, 2026 18:49
@Mavrick-F
Mavrick-F added this pull request to the merge queue Nov 26, 2026
Merged via the queue into opengeos:main with commit 20c91ee Nov 26, 2026
30 checks passed
@Mavrick-F
Mavrick-F deleted the perf/gpkg-wide-pages branch November 26, 2026 20:15
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