Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions crates/map/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1695,9 +1695,10 @@ impl Layer {
}

/// The rows whose shape's box, in the layer's own coordinates ([`Dataset::coord_crs`]), may
/// meet `within`: every row whose box does, perhaps some whose box doesn't, never one without
/// a shape; each once, in order. Looked up in a grid kept until the rows change (#2032
/// C2f-6), so a search near an edit doesn't read every feature.
/// meet `within`: every row whose box does, perhaps some whose box doesn't (rows without a
/// shape only in a layer of more than 4 billion rows, which gives every row); each once, in
/// order. Looked up in a grid kept until the rows change (#2032 C2f-6), so a search near an
/// edit doesn't read every feature.
pub fn rows_touching(&self, within: &Extent) -> Vec<usize> {
self.row_boxes.touching(&self.dataset.features, within)
}
Expand Down
33 changes: 21 additions & 12 deletions crates/map/src/row_boxes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,24 @@ impl Clone for Cache {
}

impl Grid {
/// The grid of `rows`' boxes: one walk over every row's vertices, with a box a row (32 bytes)
/// kept only while the grid is made. Capped at 256 by 256 cells, so the cells' own lists stay
/// small (about 1.5 MB at most) whatever the layer's size; a row's box is listed in at most
/// [`MAX_CELLS_PER_ROW`] cells.
/// The grid of `rows`' boxes, made in two walks over every row's vertices: one for the box
/// they are all in, one to list each row's box, so no box is kept per row (a list of them
/// would take 32 bytes a row while the grid is made). Capped at 256 by 256 cells, so the
/// cells' headers (24 bytes each) take about 1.5 MB at most; each row is listed at 4 bytes a
/// cell it is in, in at most [`MAX_CELLS_PER_ROW`] cells (usually one to four), or once in
/// the wide list.
Comment on lines +46 to +49

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 memory comment says each row is listed "at 4 bytes a cell it is in", but the cell lists are Vec<u32>, and a growing Vec keeps spare capacity, so the real figure can be higher than 4 bytes per listing. "(usually one to four)" is also not measured anywhere in this PR. Either say "at least 4 bytes" or measure with cargo xtask memory and state the number. This matters because the comment is the place that records the grid's cost against the layer size.

Category: quality

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This merged before the fix was pushed. The next C2f PR changes the comment to "at least 4 bytes per listing, more where a cell's list keeps spare room" and drops the unmeasured "usually one to four".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Carried into #3018 (C2f-7 part 2) as promised.

fn new(rows: &Features) -> Grid {
// A box per row, empty for a row without a shape or a place (`Geometry::extent` leaves
// out a coordinate that isn't a number, so every other box is finite).
let boxes: Vec<Extent> = (0..rows.len()).map(|i| rows.geometry(i).map_or(Extent::EMPTY, |g| g.extent())).collect();
let total = boxes.iter().fold(Extent::EMPTY, |t, e| if e.is_empty() { t } else { t.union(e) });
let shaped = boxes.iter().filter(|e| !e.is_empty()).count();
// A row's box, empty for a row without a shape or a place (`Geometry::extent` leaves out a
// coordinate that isn't a number, so every other box is finite).
let box_of = |i: usize| rows.geometry(i).map_or(Extent::EMPTY, |g| g.extent());
let (mut total, mut shaped) = (Extent::EMPTY, 0usize);
for i in 0..rows.len() {
let e = box_of(i);
if !e.is_empty() {
total = total.union(&e);
shaped += 1;
}
}
Comment on lines +54 to +61

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.

This removes the 32-byte-per-row box list, but the grid is still built synchronously from held_places (crates/engine/src/cmd/design.rs:1474), which runs inside edit commands. Every row's geometry and extent are now computed twice on the first search after an edit, and the cell sizes need the total box before the second walk can start, so the second walk can't be avoided with this layout. The PR says so, and the trade-off is reasonable for a 10 million row layer. It would still be worth timing the first search after an edit on a large layer, before and after, so the cost is known rather than estimated. Confidence is medium-low, since the per-row extent is cheap and the real cost depends on vertex counts.

Category: performance

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not timed. The second walk does the same work as the first, one Geometry::extent per row, which is also what held_places did on every command before #2895. So the first search after an edit costs about two of the old per-command scans, and later searches cost nothing until the rows change. Timing it on a large layer is worth doing once the edited layer's grid is updated in place rather than rebuilt, which is noted for #2032.

// About one row a cell.
let cols = ((shaped as f64).sqrt().ceil() as usize).clamp(1, 256);
let mut grid = Grid {
Expand All @@ -63,7 +71,8 @@ impl Grid {
wide: Vec::new(),
beyond: false,
};
for (i, e) in boxes.iter().enumerate() {
for i in 0..rows.len() {
let e = &box_of(i);
let Ok(row) = u32::try_from(i) else {
// Past 4 billion rows a row can't be listed: every search looks at them all.
grid.beyond = true;
Expand Down Expand Up @@ -126,8 +135,8 @@ impl Grid {
}

impl Cache {
/// The rows of `rows` whose shape's box may meet `within` (a superset of those that do; never
/// one without a shape), each once, in order. The grid is made again only once the rows
/// The rows of `rows` whose shape's box may meet `within` (a superset of those that do; one
/// without a shape only past `u32::MAX` rows, when every row is given), each once, in order. The grid is made again only once the rows
/// change.
Comment on lines +138 to 140

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 reflowed doc comment has one line about 140 characters long, and the last line holds only "change.". Rewrapping it keeps the lines under the usual width:

Suggested change
/// The rows of `rows` whose shape's box may meet `within` (a superset of those that do; one
/// without a shape only past `u32::MAX` rows, when every row is given), each once, in order. The grid is made again only once the rows
/// change.
/// The rows of `rows` whose shape's box may meet `within` (a superset of those that do; one
/// without a shape only past `u32::MAX` rows, when every row is given), each once, in order.
/// The grid is made again only once the rows change.

Category: quality

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This merged before the fix was pushed; the rewrap goes into the next C2f PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Carried into #3018 (C2f-7 part 2) as promised.

pub(super) fn touching(&self, rows: &Features, within: &Extent) -> Vec<usize> {
// The grid is disposable; a poisoned lock does not invalidate the features themselves.
Expand Down
Loading