Skip to content

perf(ui,analysis): avoid table Dataset Arc clones and cull route_on snapping - #2934

Merged
giswqs merged 2 commits into
mainfrom
perf/avoid-table-arc-copy
Nov 29, 2026
Merged

giswqs merged 2 commits into
mainfrom
perf/avoid-table-arc-copy

Conversation

@giswqs

@giswqs giswqs commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Summary

Avoid two unnecessary hot-path costs in the Attribute Table and network routing:

  1. Drop Dataset / selection clones from the Attribute Table and Date Conversion Dialog (crates/ui-egui/src/table.rs, crates/ui-egui/src/table/dates.rs, crates/ui-egui/src/readjob.rs):

    • table::show previously cloned the displayed layer's or standalone table's Dataset and selection vector up front and held them across the entire frame, including edit.delete, edit.addFeature, edit.convertField, edit.deleteField, and edit.setValue. That bumped Arc::strong_count on features.rows across every mutating command (pinning the pre-edit rows until the end of the frame and causing toolbar edits to render a stale snapshot for one frame) and copied the entire selection vector on every repaint.
    • table::show now reads lightweight header metadata via a short borrow before drawing the toolbar, then borrows (&Dataset, &[usize]) only while drawing the grid and resolving field names/types, ending the borrow before any command runs. Only opening the Date Conversion Dialog clones the Dataset for its background scan.
    • table::dates::show now moves params (and dataset when reads_off_frame is enabled on desktop) out of Dialog and drops Dialog before running edit.convertField inline (wasm32 and desktop sync paths). readjob::run_presented also drops any unused preview snapshot before inline execution.
  2. Enable bounding-box culling in network::route_on (crates/analysis/src/network.rs):

    • route_on passed None as the within bound to nearest_segment, scanning and projecting every segment in the network with haversine_m on every endpoint snap, even though Graph::place already passes Some(snap_m).
    • route_on now checks segs.is_empty() up front (preserving "the layer has no lines"), passes Some(snap_m) to nearest_segment, and maps a culled None to the existing "a route end is more than ... from the network" error.

Measurements (before vs after)

Measured on this machine before and after the changes:

  • Network routing (network::route_on), 50,244-edge road grid (159 x 159), 20 stops (19 legs, 38 endpoint snaps), snap_m = 50.0, release build:

    • Total route_on (19 legs): 124.014 ms -> 37.085 ms (3.34x faster, 70.1% reduction), with bit-for-bit identical route outputs (total_len = 50275.815092, total_rows = 497).
    • Endpoint snapping (nearest_segment, 20 stops, 5-run mean): 50.284 ms -> 5.172 ms (9.7x faster).
  • Attribute Table & Date Conversion Dialog, starting from strong_count = 1 (expected count inside a mutating command with an Undo snapshot is 2: live dataset + Undo snapshot):

    • edit.setValue (cell edit): strong_count 3 -> 2 (0 extra UI Arc refs)
    • edit.delete (toolbar delete): strong_count 3 -> 2 (0 extra UI Arc refs)
    • edit.addFeature (standalone table Add row): strong_count 3 -> 2 (0 extra UI Arc refs)
    • edit.convertField (date dialog inline/wasm path): strong_count 4 -> 2 (eliminated 2 extra Dataset clones)
    • 200 Attribute Table repaints on a 50,000-row layer with 50,000 selected rows (debug test build): 66.121 ms -> 59.753 ms, eliminating per-frame Dataset and 400 KB selection vector cloning.

Surfaces & memory

  • Reaches desktop (rustgis), browser (rustgis-web, verified with wasm32-unknown-unknown -D warnings), CLI (rustgis-cli), Python (rustgis-python), and phone (ui-touch) for network::route_on, and desktop + browser for the egui Attribute Table and Date Conversion Dialog. Undo/redo and error messages remain unchanged.
  • Memory impact: strictly reduces retained references and per-frame allocations (0 extra layer/table row copies added).

Update after merging main

  • main already passes Some(snap_m) to nearest_segment in route_on (perf: eliminate hot-path allocations in query, routing, k-means, and publish #2926, which also replaced the cloned node and adjacency vectors with an overlay), so the routing code change here is now main's; this PR keeps its regression test route_on_culls_by_snap_window_with_identical_results_and_errors, which checks culled and unculled snapping match bit for bit and the empty-network and too-far errors stay distinct.
  • The Attribute Table change was re-applied onto main's reworked table::show (keyboard cell cursor, row menu): the header reads the source through a short borrow, the grid borrows (&Dataset, &[usize]), and every command the frame runs (statistics, convert, delete field, select rows, cell commits) is resolved to owned field names first, so none runs while the borrow or a copy holds the rows.

Testing

  • Added regression tests:
    • network::tests::route_on_culls_by_snap_window_with_identical_results_and_errors
    • table::tests::attribute_table_drops_dataset_borrow_before_mutating_commands
    • table::dates::tests::date_dialog_drops_dataset_before_inline_convert
  • Verified locally:
    • cargo fmt --check
    • cargo clippy --locked -p rustgis-analysis -p rustgis-ui-egui --all-targets -- -D warnings
    • cargo test --locked -p rustgis-analysis -- network
    • cargo test --locked -p rustgis-ui-egui -- table
    • cargo test --locked -p rustgis-ui-egui --test command_ids
    • cargo xtask test-hygiene && cargo xtask layers && cargo xtask rows && cargo xtask attribution
    • RUSTFLAGS='-D warnings' cargo build --locked --target wasm32-unknown-unknown -p rustgis-web

UI evidence

No visible UI change.

Related issue

Part of codebase performance audit.

Note: is_layer in table.rs now checks matches!(key, ViewKey::Layer(_)) rather than layer_id.is_some(), so if table_layer is stale while a standalone table is shown, the panel treats it as a standalone table rather than aiming layer actions at a missing layer id.

Summary by CodeRabbit

  • Improvements
    • Table edits, row changes, and field deletion now work with the current data, while undo continues to preserve the expected prior state.
    • Date conversion remains available during inline table workflows.
  • Bug Fixes
    • Improved handling of table data during changes to help ensure operations apply consistently.

Borrow Dataset and selection in the Attribute Table instead of cloning them across toolbar and cell-edit commands, and drop the date conversion dialog's Dataset before running edit.convertField inline. Also pass Some(snap_m) to nearest_segment in network::route_on while preserving empty-network and distance error messages.
@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The changes add route snap-window test coverage and adjust dataset borrowing and release timing in the egui table and date-conversion command paths. Tests check route results, error cases, and dataset reference counts during edits and conversions.

Changes: Route snap-window validation

Layer / File(s) Summary
Validate snap-window results and errors
crates/analysis/src/network.rs
A grid-based test compares nearest-segment results with and without a 50 m snap window. It also checks sampled routes and errors for distant endpoints, negative snap distance, and an empty network.

Table dataset reference lifetimes

Layer / File(s) Summary
Borrow table data and prepare commands
crates/ui-egui/src/table.rs
The table reads dataset and selection data by reference, prepares command inputs before mutations, and uses captured field definitions for commits. Tests check row-copy reference counts for edits and row or field operations.
Release datasets before inline commands
crates/ui-egui/src/readjob.rs, crates/ui-egui/src/table/dates.rs
Inline execution releases the preview or dialog dataset before conversion. A test checks the reference count during conversion and verifies the converted value.

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

Change: Refactor

Possibly related issues

  • Issue 2922 — The route test covers equivalence with a snap-window filter, which is related to the issue’s snap-culling objective. The supplied change summaries describe test coverage, not the culling implementation.
  • Issue 2640 — The route test covers nearest-segment snap-window behavior, one of the network items listed in the issue. The issue also lists other work that these changes do not cover.
  • Issue 2431 — The table changes and tests address the shared-Arc reference count during edits, which is the cause described in the issue.

Suggested reviewers: mavrick-f, deniial00, pick2510

Merge Risk: Minimal · up to c7b03

This change reduces dataset clones in the Attribute Table and date-conversion paths and culls route snapping. No concrete defect was identified, and tests cover the changed behavior. Merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names both halves of the changeset: removing Dataset Arc clones from the Attribute Table and Date Conversion Dialog, and culling route_on snapping. It uses the conventional commit prefix and…
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.
Description check Passed The description is thorough and covers the Summary (what changed and why, with measurements), a Testing section listing new regression tests and verification commands, and a Related issue entry ("Part…
Full details: Title check

Explanation

The title names both halves of the changeset: removing Dataset Arc clones from the Attribute Table and Date Conversion Dialog, and culling route_on snapping. It uses the conventional commit prefix and stays within the expected length. It is slightly dense, but it clearly identifies the main changes.

Full details: Description check

Explanation

The description is thorough and covers the Summary (what changed and why, with measurements), a Testing section listing new regression tests and verification commands, and a Related issue entry ("Part of codebase performance audit"). The template's UI evidence section is also addressed with "No visible UI change.", which matches the template's instruction for PRs without visible UI changes. The description is substantially complete. Minor point: the Related issue line does not reference a specific issue number, and the template placeholder asks for one, but "Part of" a performance audit is an acceptable non-critical gap.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • 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 routes at dawn
The grid points hop from line to line
A table lends its data lightly
Then drops its copy before edits
Dates turn while the rows stay near
The rabbit marks each test as green

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: 738092ee-a664-4529-8359-01532cdde798
Commits

Reviewing files that changed from the base of the PR and between 0286882 and c7b0373.

Files selected for processing: 4

  • crates/analysis/src/network.rs
  • crates/ui-egui/src/readjob.rs
  • crates/ui-egui/src/table.rs
  • crates/ui-egui/src/table/dates.rs

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

Comment thread crates/ui-egui/src/readjob.rs Resolved
Comment thread crates/ui-egui/src/table.rs Resolved
@github-actions

Copy link
Copy Markdown
Contributor

Code review

I read the full diff, the staged PR metadata, and the surrounding code for nearest_segment, snap_window, route_on, the table header, and the date dialog. I could not build or run tests (no shell here), so the compile and test claims in the PR body are unverified. There are no earlier review threads on this PR, so no concerns were skipped as already raised.

Bugs: None found. I checked the window math in snap_window: it is a superset of the real search radius, including latitude and longitude slack, and it returns None near the poles and the antimeridian. Culled and unculled results should match, and the negative and empty-network error paths are preserved. Confidence: medium-high. Compile-level borrow issues in the large table.rs restructure are not verified.

Security: None found.

Performance: The culling and the removal of the Dataset and selection clones are sound. I did not reproduce the benchmark numbers. Confidence: medium.

Quality:

  • readjob.rs:184: the drop(preview) hunk looks unreachable. run_date_preview now only receives a preview when reads_off_frame is true, so this path never holds one. Confidence: high. (Inline comment posted.)
  • table.rs:405: is_layer changed from layer_id.is_some() to a ViewKey match. This is a behavior change the PR does not mention, and no test covers it. Confidence: high that the change happens, medium that it matters. (Inline comment posted.)
  • network.rs:401-404: when nearest_segment returns None for a non-empty network, the error is now "a route end is more than …" instead of "the layer has no lines". That is the intended result for culling. It also changes the message for a graph whose segments all reference missing nodes, which Network::build probably never produces. Confidence: low.
  • table.rs:513: the shown fallback to "all fields" when the view is missing is unreachable, since block 1 always inserts the view. Sharing the value computed in block 1 would be clearer. Confidence: high, minor.
  • Tests: the network test covers only mid-latitude. It does not check the antimeridian or polar cases, where snap_window returns None and the culled-equals-unculled property matters most. Confidence: medium.

AGENTS.md: No hard rule break found. Low-confidence note: the PR says it adds "0 extra layer/table row copies", but the rule asks for a multiple of the layer for changes that store or copy layer data. The date dialog still holds one shared Arc, so the claim should be stated in the PR. Confidence: low.

Change size

Compared: the whole PR against main (merge base 1994173 to PR head 7786e80).

Measure Value
Files changed 4 (added 0, modified 4, removed 0, renamed 0)
Size before 346.0 KB (354,310 B)
Size after 363.1 KB (371,825 B)
Total size change +17.1 KB (+17,515 B)
Path Before After Change
crates/ui-egui/src/table.rs 86.9 KB 98.0 KB +11.1 KB
crates/analysis/src/network.rs 94.5 KB 97.5 KB +3.0 KB
crates/ui-egui/src/table/dates.rs 14.8 KB 17.6 KB +2.9 KB
crates/ui-egui/src/readjob.rs 149.9 KB 150.0 KB +121 B

# Conflicts:
#	crates/analysis/src/network.rs
#	crates/ui-egui/src/table.rs
Comment thread crates/ui-egui/src/table/dates.rs Resolved
@github-actions

Copy link
Copy Markdown
Contributor

Code review

No bugs, security issues or AGENTS.md breaks found in the changed lines. One performance note is posted inline on dates.rs:221. It is pre-existing and this PR does not change its behaviour.

Bugs: none found. What I checked:

  • Culling in route_on: the snap window is conservative. A segment whose bounding box misses the window cannot lie within snap_m, so culled and unculled snapping pick the same segment, and the "too far" and "no lines" errors are unchanged. The network.rs diff is test-only because the routing change is already on main (perf: eliminate hot-path allocations in query, routing, k-means, and publish #2926).
  • table.rs borrow restructuring: the header, grid and command phases use disjoint field borrows, and each command's inputs are resolved before the command runs. Statistics, convert, delete-field, select-rows and cell-commit behaviour matches the old code. The one intended change is is_layer, now matches!(key, ViewKey::Layer(_)). Statistics is only offered when is_layer, so the layer_id-gated statistics path cannot aim at a stale layer.
  • dates.rs desktop and wasm branches: the desktop None arm matches the wasm app.run("edit.convertField") call. The preview cfg guards are consistent.

Security: none.

Performance (1, inline): dates.rs:221, the None arm runs edit.convertField on the UI thread on desktop in the normal case. It is unchanged from before the PR. Confidence about 60%; I did not run the app.

Quality (2):

  • The PR description still presents the route_on culling and its measurements as this PR's change. The diff only adds a test for network.rs, and the "Update after merging main" section says the routing change now belongs to main. Confidence high.
  • Two earlier concerns were left alone because the author answered them: the unreachable drop(preview) in readjob.rs, and the is_layer description and edge-case test. The author updated the description and deferred the test to the release quality pass.

AGENTS.md: none. The recv_timeout in the dates test is a deadline wait, which test-hygiene allows.

Twins checked: every nearest_segment caller in network.rs now passes Some (place and route_on). The overlay tools in tools.rs reach these paths through route_on, so they are covered. The edit.* commands the table runs go through the same frame code. The wasm/desktop dates.rs branches are consistent.

Not verified: I could not compile or run the tests, since there is no shell here. The borrow-check and test claims rest on reading the code.

Change size

Compared with the base branch (merge base 0286882 to PR head c7b0373).

Measure Value
Files changed 4 (0 added, 4 modified, 0 removed, 0 renamed)
Size before 396.7 KB
Size after 411.6 KB
Total size change +14.8 KB
Path Before After Change
crates/ui-egui/src/table.rs 122.3 KB 131.2 KB +8.9 KB
crates/analysis/src/network.rs 98.6 KB 101.6 KB +3.0 KB
crates/ui-egui/src/table/dates.rs 14.8 KB 17.6 KB +2.9 KB
crates/ui-egui/src/readjob.rs 161.1 KB 161.2 KB +121 B

@giswqs
giswqs added this pull request to the merge queue Nov 29, 2026
@github-actions

Copy link
Copy Markdown
Contributor

📖 Docs preview

Item Value
Preview https://preview.rustgis.app/pr-2934/
Web app https://preview.rustgis.app/pr-2934/app/
Commit c7b0373

GitHub Pages takes a minute or two to publish a new commit.

Merged via the queue into main with commit cbd2f1b Nov 29, 2026
30 checks passed
@giswqs
giswqs deleted the perf/avoid-table-arc-copy branch November 29, 2026 01:32
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