Skip to content

fix(layout): look at Cancel while a table or chart reads its layer, and sort a table's text faster - #3497

Merged
jgregoriorincon merged 4 commits into
opengeos:mainfrom
jgregoriorincon:fix/3493-layout-read-cancel
Dec 1, 2026
Merged

jgregoriorincon merged 4 commits into
opengeos:mainfrom
jgregoriorincon:fix/3493-layout-read-cancel

Conversation

@jgregoriorincon

@jgregoriorincon jgregoriorincon commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Drawing a layout page reads every row of a layer for a sorted table and for a chart. Neither read looked at Cancel, so a cancel asked for during an export waited until the page was drawn. Timing the reads also showed that a sort by text lowercased both texts on every comparison, which took up to 45 s over two million rows.

  • TableListing::first (sorted) and chart_data_with look at Cancel every 4096 rows. A cancelled table read stops with the rows kept so far, and a cancelled chart read is no data. The page is then refused as cancelled (finish_legends), so nothing is written.
  • A row the table's sort keeps holds its text in lowercase, so later comparisons don't lower it again. A row that isn't kept is still compared as it is read, no further than the first difference, so long texts cost what they did before. The order is unchanged.
  • Memory: no copy of the layer. The lowercase text held is that of the kept rows only: at most 500 cells of the sort field (the most rows a table lists), each about as long as its text, whatever the layer's row count.

Surfaces: every layout and report drawing shares these two reads. Cancel reaches a drawing whose run installs one, as the desktop's background export does; where none is installed, as in the Layout view and the browser, the look does nothing and only the sort's speed changes. No command, parameter or saved format changes.

Not in this PR: the Layout view still draws its page on the interface thread, so a very large sorted table is read there; it is faster with this change, not moved off the thread.

Testing

Time of one sorted read, limit 500, 2,000,000 rows, a development test build with rustgis-map and rustgis-data at opt-level = 3 (not a release build; the timing test is not committed):

Values Before After
whole numbers, reverse order 0.34 s 0.26 s
short text, random order 0.40 s 0.21 s
text sharing a 40-character start, random order 1.6 s 0.57 s
the same, reverse order 24.6 s 1.8 s
non-ASCII text, reverse order 45.5 s 2.2 s

New tests, seen failing before the fix: a_tables_sort_looks_at_cancel_between_rows, a_charts_read_looks_at_cancel_between_rows. a_tables_sort_of_text_keeps_the_comparators_order pins the order against cmp_values. The cancellation sweeps' layout now holds a sorted table and a chart.

cargo fmt --check, cargo clippy with -D warnings, doctests, cargo xtask layers and schema --check pass. cargo nextest run: 11675 passed, 8 failed, 29 skipped, one DuckDB test left out. The 8 (seven rustgis-geolibre tests and line_strokes_editor_edits_one_class) also fail on main on this machine.

UI evidence

No visible UI change.

Related issue

Closes #3493

Summary by CodeRabbit

  • Bug Fixes
    • Table and chart data processing now checks for cancellation during large scans, allowing cancelled layout drawing to stop sooner.
    • Table sorting now applies consistent case-insensitive ordering, with original text and row order used to resolve ties.
    • When a sorted table scan is cancelled, it can return the best rows collected so far; cancelled chart scans return no chart data.

@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Sorted table and chart reads now check for cancellation during layout drawing. Cancelled sorted-table reads return the best rows collected so far, while cancelled chart reads return no data. Tests cover cancellation checks and text-sort ordering.

Changes: Layout export cancellation

Layer / File(s) Summary
Sorted table scan and ordering
crates/map/src/layout/elements.rs, crates/map/src/layout/tests.rs
Sorted table scans check for cancellation every 4,096 rows and return the best rows collected so far. Retained text values cache lowercase text for comparisons. Tests check cancellation and compare text ordering with cmp_values.
Chart cancellation and validation
crates/map/src/layout/elements.rs, crates/map/src/layout/tests.rs, crates/engine/src/tests/cancellation.rs, crates/render/src/layout.rs
Grouped and ungrouped chart scans check for cancellation every 4,096 features and return no data if cancelled. Tests cover chart reads. The cancellation fixture adds a sorted table and chart, and the render_pending documentation describes its cancellation limitation.

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

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

Possibly related PRs

  • opengeos/RustGIS#3479 — That PR defines layout table sorting; this PR updates the sorted-table scan and tests its ordering.

Suggested reviewers: elpidioassinaturas, m-hollis-1, choupeanut

Merge Risk: Minimal · up to 128aa

Sorted table and chart reads now stop promptly on cancellation during layout export. A cancelled export is refused before any output is written. No command, parameter or saved format changes, and the change is easy to revert, so merge readiness looks good.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the two main changes: cancellation checks during table and chart layer reads, and faster text sorting for tables. It is specific and follows the conventional-commit style. The changese…
Description check Passed The description follows the template. It has Summary, Testing, UI evidence, and Related issue sections. The UI section correctly says 'No visible UI change.' The Testing section gives detailed command…
Linked Issues check Passed [#3493] requires that the sorted-table read (TableListing::first) and the chart read (ElementKind::chart_data_with) check Cancel between rows, and that a cancelled export leaves nothing behind. Th…
Out of Scope Changes check Passed All changes are in the layout read path that #3493 names: crates/map/src/layout/elements.rs (table sort and chart scan), crates/map/src/layout/tests.rs (tests), `crates/engine/src/tests/cancellati…
Full details: Title check

Explanation

The title names the two main changes: cancellation checks during table and chart layer reads, and faster text sorting for tables. It is specific and follows the conventional-commit style. The changeset matches it: cancellation checks every 4096 rows in elements.rs and the lowercase-caching sort change, with tests in tests.rs and a doc note in render/layout.rs.

Full details: Description check

Explanation

The description follows the template. It has Summary, Testing, UI evidence, and Related issue sections. The UI section correctly says 'No visible UI change.' The Testing section gives detailed commands, timings, and new tests. The Related issue section links #3493 with 'Closes'. The description is complete and specific.

Full details: Linked Issues check

Explanation

[#3493] requires that the sorted-table read (TableListing::first) and the chart read (ElementKind::chart_data_with) check Cancel between rows, and that a cancelled export leaves nothing behind. The PR description and the AI-generated summary of crates/map/src/layout/elements.rs state that both reads check Cancel every 4096 rows. A cancelled table read returns the rows kept so far. A cancelled chart read returns no data. finish_legends then refuses the page, so nothing is written. crates/render/src/layout.rs documents render_pending as refusing cancelled drawing. The added tests a_tables_sort_looks_at_cancel_between_rows and a_charts_read_looks_at_cancel_between_rows cover the cancellation behavior, and the cancellation fixture in crates/engine/src/tests/cancellation.rs now includes a sorted table and a chart. Limits: the diff of crates/map/src/layout/elements.rs could not be read (changed blobs missing from the checkout), so this verdict relies on the PR description, the summary, and the test names, not on the reviewed hunks. The reported test results were not re-run.

Full details: Out of Scope Changes check

Explanation

All changes are in the layout read path that #3493 names: crates/map/src/layout/elements.rs (table sort and chart scan), crates/map/src/layout/tests.rs (tests), crates/engine/src/tests/cancellation.rs (cancellation fixture), and crates/render/src/layout.rs (doc comment only). The lowercase-text cache for sort comparisons is a speed change in the same sorted-table read that the cancellation check touches. The PR description states that the order is unchanged and a test pins it against cmp_values. It adds no new command, parameter, or saved format. This is a related change in the same function and is not an unrelated change. Limits: the code diff could not be read, so this verdict relies on the PR description and summary.

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 rows as they go,
And pauses when cancellation says so.
The chart scan stops before its finish,
The table keeps its best, without a skirmish.
Unicode sorts in order, neat and slow.
I nibble a carrot, then hop through the snow.

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: cb9143f4-288a-47bf-978f-92d9b9228a1b
Commits

Reviewing files that changed from the base of the PR and between 1fc44f9 and 128aad3.

Files selected for processing: 4

  • crates/engine/src/tests/cancellation.rs
  • crates/map/src/layout/elements.rs
  • crates/map/src/layout/tests.rs
  • crates/render/src/layout.rs

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

@github-actions

Copy link
Copy Markdown
Contributor

Code review

I read the diff, the base-branch AGENTS.md, and the surrounding code (the render table path, the chart callers, the export closure, progress.rs, and cmp_values). I could not compile or run the tests here, so everything below is from reading the code.

Bugs

  • None confirmed. I checked the new text ordering against cmp_values (crates/data/src/value.rs:435). Lowercasing character by character and then comparing the lowercase strings by bytes gives the same order as comparing the chars, because UTF-8 byte order matches code point order. The then_with(|| a.cmp(b)) tie-break matches too. Confidence: high.
  • Cancel and partial output: a cancelled table returns partial rows (elements.rs:589-620) and a cancelled chart returns None. Both are safe only because the export closure checks Cancel after drawing (crates/engine/src/cmd/layout/export.rs:560) and refuses the file. The Layout view and the chart-notice path (engine/.../chart.rs:473) have no probe, so they never see a cancel. Any future background caller of first or chart_data that skips that check would write a partial table without a notice. Confidence in the current code: high. Confidence that this is a risk for future callers: medium.

Security

  • None found.

Performance

  • Memory (low, confidence 40%): each kept text row holds a full lowercase String. The row count is capped at 500 by table_rows (crates/engine/src/cmd/layout/elements.rs:69), so the PR's "at most 500" claim holds. The cap is on rows, not text length, so 500 rows of long text (say 100 KB each) hold about 50 MB of lowercase copies. Consider lowering only a bounded prefix, or saying the cost is "500 × longest kept text" in the PR.
  • The Layout view still draws on the UI thread. The PR says so openly, so I did not raise it as a finding.

Quality

  • The chart test builds its element JSON by hand (tests.rs:2450-2452) when the file's chart(...) helper (tests.rs:134) does the same. Using the helper would shorten it.
  • Sorted::kept is a vague name for "lowercase this text once it is kept". Naming and wording are left for the release quality pass.

AGENTS.md

  • Progress is not counted (confidence 55%). The new loops look at Cancel but never call progress::begin or progress::step, though the row count is known. AGENTS.md asks for this where progress can be measured. Details are in the inline comment at elements.rs:494.

Checked and left alone

  • Twins: the table readers (render/src/layout/table.rs:172 and :88, where :88 only counts) and the chart readers (chart_data and chart_data_of, all routed through chart_data_with). No wasm-specific branch is touched, and progress is not cfg-gated.
  • Prior reviews: none on this PR, so none were left alone.
  • Not verified: the test and CI results in the PR body, since I could not run anything.

Change size

Compared with the base branch (main, merge base a44616f to PR head 4e516b1); this is the whole PR, not the latest push.

Measure Value
Files changed 3 (0 added, 3 modified, 0 removed, 0 renamed)
Size before 234.9 KB
Size after 241.8 KB
Total size change +6.9 KB
Path Before After Change
crates/map/src/layout/tests.rs 128.7 KB 133.0 KB +4.3 KB
crates/map/src/layout/elements.rs 62.3 KB 64.6 KB +2.3 KB
crates/engine/src/tests/cancellation.rs 43.9 KB 44.2 KB +301 B

@jgregoriorincon

Copy link
Copy Markdown
Contributor Author

Answers to the review of 4e516b1, as of 6b7c7a0:

  • Memory: right, the cap is on rows and not on text length. The PR text now says the held text is that of at most 500 cells of the sort field, each about as long as its text. The code stays as it is: the copies are of at most 500 cells the layer already holds, so they cost about what those cells do, not a share of the layer that grows with its rows.
  • Test helper: fixed, the chart test builds its element with chart().
  • Sorted::kept: renamed to with_lowered.
  • Progress: left as it is. A multi-page export and a report count their pages, and begin inside a page's read would reset that count; nothing else in drawing a page counts. The reads only look at Cancel.
  • A future caller writing a partial table: no change. A cancelled read happens only where a run installs a Cancel, and the drawings that run under one refuse the page (finish_legends); first's doc says so.

The build check of the docs preview failed outside this change: on 4e516b1 it ended with exit code 143 about ten minutes into trunk build --release, with no compile error, and on 6b7c7a0 the download of the Trunk release failed (curl: (22), error 618). The Web app (wasm32) check passed on 4e516b1.

Comment thread crates/map/src/layout/elements.rs Resolved
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Code review

I found no bugs, security issues, or AGENTS.md breaks in this PR. I could not compile or run the tests, so the review is by reading the code.

Bugs: none found. Confidence: high.

  • Cancel is refused on both export paths that draw tables and charts. render (via render_page, render/layout.rs:221-223) and render_drawing (render/layout.rs:272) call finish_legends, which checks progress::check(). Cancel is sticky, so a cancelled partial table or placeholder chart is never written. The svg.rs:39 path also goes through finish_legends.
  • The lowercase-cached comparison gives the same order as the old cmp_values path. It uses the same character-by-character lowercasing, and UTF-8 byte order matches code-point order. a_tables_sort_of_text_keeps_the_comparators_order pins this against cmp_values using a separate sort.
  • The heap's Ord stays a total order: both the cached and uncached branches end in the same raw-text tiebreak.

Security: none found.

Performance: no regression found. Confidence: medium-high.

  • Lowercase copies exist only for rows the heap keeps. The page-size cap on rows is in table_rows/TABLE_ROWS (engine), so the "at most 500" claim depends on that constant. I did not read its value.
  • progress::check() runs once per 4096 rows, which is negligible.

Quality:

  • Nit, posted inline on crates/map/src/layout/elements.rs:616: the Option<()> stop signal could be ControlFlow<()>.
  • Not in the diff, so not posted inline: render_pending (render/layout.rs:199-223) skips finish_legends because wait_shadows is false. A cancelled read there would return a partial page with no refusal. Only the Layout preview calls it, and that path installs no probe, so nothing is affected today. Confidence: high that it is unreachable now; the risk is a future caller that installs a probe. Worth a comment in that function.

AGENTS.md: no break found. Confidence: high.

  • The new code has no unwrap, expect, or indexing in non-test code.
  • No command, parameter, or saved format changes, so no snapshot or schema update is needed.
  • The cancellation sweep gains table and chart elements on existing fixtures. It adds no new long command, so no list entry is needed.
  • The PR body covers the surfaces (desktop, browser, UI-thread Layout view) and the exemption for UI-thread reads.

Twins checked:

  • Table: the render path (render/layout/table.rs:228) and the Layout view preview both reach TableListing::first.
  • Chart: chart_data and chart_data_of both reach chart_data_with. The colour-by-class path runs in the same loop, so it is covered. Other callers are engine/cmd/layout/chart.rs:473 (a notice path with no probe) and ui-egui/layoutview/format/chart.rs:229 (UI thread, no probe).
  • The UI-thread reads the PR names as out of scope are the known exemption.

Earlier concerns: none. prior-reviews.json has no threads, so I left 0 earlier concerns alone.

Change size

Compared with the base branch (merge base a44616f to PR head 6b7c7a0), the whole PR against main.

Measure Value
Files changed 3 (0 added, 3 modified, 0 removed, 0 renamed)
Size before 234.9 KB
Size after 241.7 KB
Total size change +6.8 KB
Path Before After Change
crates/map/src/layout/tests.rs 128.7 KB 132.9 KB +4.2 KB
crates/map/src/layout/elements.rs 62.3 KB 64.6 KB +2.4 KB
crates/engine/src/tests/cancellation.rs 43.9 KB 44.2 KB +301 B

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Code review

Verdict: no blocking findings. I read the diff, the base-branch AGENTS.md, the callers in render and engine, and the progress and cancel plumbing. I could not run the tests (no shell in this environment), so the test claims in the PR description are unverified. I posted no inline comments because I found no new issue that met the bar.

Bugs

None found. What I checked:

  • Sort order is unchanged. The lowercase comparison is char-by-char via flat_map(char::to_lowercase), the same as cmp_values (crates/data/src/value.rs:435), with the raw-text tiebreak. The mixed Some/None branches compare the same sequences, and the heap only ever holds Some rows, so its ordering is consistent. Confidence: high.
  • Cancel reaches the reads and a cancelled page is refused. stopped_at looks at rows 0, 4096, 8192 and so on. render, render_region and render_drawing all call finish_legends, which sees progress::check(), so a page built from a cancelled table or chart is not written. Confidence: high.
  • Test arithmetic. For 10,000 rows, a full read looks 3 times, and a cancel from look 2 keeps rows 4095/4094/4093. These match the loop. Confidence: medium, since I did not run them.

Security

None.

Performance

  • The lowercase copies are held only for the at most limit (500) kept rows. They scale with the kept text length, not the layer's row count, so this is not a second copy of the layer, as the PR says. Confidence: high.
  • The Layout view still reads the sorted table on the UI thread (crates/ui-egui/src/layoutview.rs:452). The PR discloses this as out of scope. I'm noting it, not raising it as a defect.
  • The chart's post-loop sort is not checked for Cancel. Its doc comment says so, which the rule allows. Confidence: high.

Quality

  • The Option<()> → ControlFlow nit from the earlier thread is already fixed in 128aad3. I left it alone (1 earlier concern, answered and fixed). The render_pending doc point was also addressed in the same commit.
  • No new quality points worth a comment.

AGENTS.md

No violations found in the changed files. Checked: no unwrap/expect in non-test code, the new cancellation tests live in the long_commands sweep, no command surface change, no saved-format change, and no new background raster or export path.

Notes (low confidence)

  • Cancel source. The new reads only check rustgis_data::progress::check(). Render also has its own flag (render::cancel::requested()), which finish_legends checks and the map loops in render/src/lib.rs check. If an export ever set only that flag, the table and chart would not stop early. The page would still be refused at the end, so no output is wrong. I found no export path that sets only that flag. Confidence this matters: about 30%.
  • Test wiring. The new layout.addElement table and chart calls in cancellation.rs rely on the frame default in frame_of. I did not confirm that default, so the sweep could fail on its first unwrap if it is wrong.

Change size

Compared with main (merge base a44616f to head 128aad3).

Files changed Added Modified Removed Renamed Size before Size after Total change
4 0 4 0 0 298.9 KB 305.9 KB +7.0 KB
Path Size before Size after Change
crates/map/src/layout/tests.rs 128.7 KB 132.9 KB +4.2 KB
crates/map/src/layout/elements.rs 62.3 KB 64.7 KB +2.4 KB
crates/engine/src/tests/cancellation.rs 43.9 KB 44.2 KB +301 B
crates/render/src/layout.rs 64.0 KB 64.1 KB +127 B

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

📖 Docs preview

Item Value
Preview https://preview.rustgis.app/pr-3497/
Web app https://preview.rustgis.app/pr-3497/app/
Commit 128aad3

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).

@jgregoriorincon
jgregoriorincon marked this pull request as ready for review December 1, 2026 10:34
Comment thread crates/render/src/layout.rs Resolved
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Code review

Bugs

  • None found in the changed code. I checked the new text order against cmp_values (crates/data/src/value.rs:434), including the case tie-break. The heap's cancel path returns the rows kept so far. Both chart row loops check Cancel, and the colour closure runs inside them. The unsorted and selected branches of first are bounded by limit. Confidence: high.

Security

  • None found.

Performance

  • The lowercase copy is held only for kept rows, and comparisons against unkept rows stop at the first difference, as the PR says. I found no further issue. Confidence: high.

Quality

  • Latent contract risk in render_pending (inline comment on crates/render/src/layout.rs:199). With a Cancel installed, the new early stop can return a truncated page as Ok, because finish_legends runs only on the export path. No current production caller does this. Confidence: medium.
  • Nits, not posted inline:
    • use std::ops::ControlFlow; sits mid-function in first. Moving it to the top of the file would be tidier.
    • (0..n).enumerate() pairs each row with an identical read index. (0..n).map(|r| (r, r)) says the same more plainly.
    • The memory bound "at most 500 cells" is not a constant. The row count comes from min(height / row_h) in crates/render/src/layout/table.rs:40. The PR should state the bound as a multiple of the layer, per AGENTS.md's "Size for real data".

AGENTS.md

  • No rule break found. There are no unwrap/expect calls in non-test code, the new tests use progress::check, and no assert_eq! compares identical tokens.
  • Known gap, disclosed in the PR: the Layout view still draws on the UI thread, so a very large sorted table is faster but not off the thread. This is not a finding against this change.

Prior reviews

  • One thread exists (elements.rs:617, the Option<()> versus ControlFlow nit). The author fixed it in 128aad3 and the thread is resolved. I left it alone. I also left one concern alone: the render_pending doc update the author already made.

Checked twins

  • chart_data and chart_data_of: both use chart_data_with, so both are covered.
  • Sorted and unsorted table reads in first: the unsorted paths are bounded and need no check.
  • count() and text_losses: not changed by this PR.

I could not run tests (no shell in this review). The timing and test-count figures in the PR are the author's claims.

Change size

Compared with main (merge base a44616f to PR head 128aad3). Files changed: 4 (0 added, 4 modified, 0 removed, 0 renamed). Size before 298.9 KB, after 305.9 KB, total change +7.0 KB.

Files changed Added Modified Removed Renamed Size before Size after Total size change
4 0 4 0 0 298.9 KB 305.9 KB +7.0 KB
Path Size before Size after Size change
crates/map/src/layout/tests.rs 128.7 KB 132.9 KB +4.2 KB
crates/map/src/layout/elements.rs 62.3 KB 64.7 KB +2.4 KB
crates/engine/src/tests/cancellation.rs 43.9 KB 44.2 KB +301 B
crates/render/src/layout.rs 64.0 KB 64.1 KB +127 B

@jgregoriorincon

Copy link
Copy Markdown
Contributor Author

On the other points of the review of 128aad3: the place of use std::ops::ControlFlow and enumerate() stay, as the behaviour is the same. The memory bound holds: a table lists at most 500 rows (layout.addElement, layout.update and loading a project all clamp it), and min(height / row_h) in table_fit only lowers that, so the kept text never grows with the layer's row count.

@jgregoriorincon
jgregoriorincon added this pull request to the merge queue Dec 1, 2026
Merged via the queue into opengeos:main with commit ac20e75 Dec 1, 2026
31 checks passed
@jgregoriorincon
jgregoriorincon deleted the fix/3493-layout-read-cancel branch December 1, 2026 12:11
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.

[Bug]: A layout export can't be cancelled while a table or chart reads its layer

1 participant