Repository navigation
perf(render): keep a streamed Shapefile view with the geometry drawn from it #2779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
61219c2
perf(render): keep a streamed layer's view with the geometry drawn fr…
Mavrick-F e0e6753
fix(map): trim a kept view after the interaction rows are read (#1065)
Mavrick-F 8b178ba
test(map): build the kept view test's source info with the notes type…
Mavrick-F c929a12
fix(render): draw from a kept view only while its Shapefile is as it …
Mavrick-F e11a115
fix(render): keep a view only from a Shapefile as it was opened, stam…
Mavrick-F 80c8384
fix(map): keep a view read with nothing in it, so its frames read not…
Mavrick-F File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| //! The features a streamed layer's last view read and the geometry its drawing made of them | ||
| //! (projected, and thinned for display), kept so that a frame of a view inside that one draws | ||
| //! without reading, parsing or projecting them again (#1065). Shared by render snapshots, like | ||
| //! the thinning decisions ([`super::render_lod`]), and bounded by [`KEPT_VIEW_BYTES`] and the | ||
| //! room the source's share of the map's view memory leaves. | ||
| use rustgis_data::source::FeatureSource; | ||
| use rustgis_geom::Extent; | ||
| use std::sync::{Arc, Weak}; | ||
|
|
||
| use crate::Layer; | ||
|
|
||
| /// The most a kept view holds: its rows and the geometry drawn from them | ||
| /// ([`Layer::view_cache_bytes`]). A view with more, or with more than the source's share of the | ||
| /// map's view memory leaves beside its interaction rows, is drawn without being kept. | ||
| pub const KEPT_VIEW_BYTES: usize = crate::lod::CACHE_BYTES; | ||
|
|
||
| #[derive(Default)] | ||
| pub(super) struct KeptView { | ||
| source: Option<Weak<dyn FeatureSource>>, | ||
| /// What its geometry was drawn for: the projection ([`crate::Projection::code`]) and the | ||
| /// datum grids there were then, as the renderer names them. | ||
| key: String, | ||
| /// The longitude/latitude box it was read for: every feature whose box meets it. | ||
| read: Extent, | ||
| /// The chunks drawn from it in turn, in the source's order. | ||
| chunks: Arc<Vec<Layer>>, | ||
| /// What the chunks hold ([`Layer::view_cache_bytes`]). | ||
| bytes: usize, | ||
| /// The coarsest resolution (map units a pixel) a view's read was found too large to keep at, | ||
| /// for this key: views as coarse or coarser read only what they draw, and keep nothing. | ||
| too_large_at: Option<f64>, | ||
| } | ||
|
|
||
| impl KeptView { | ||
| /// Forgets everything kept for another source or key. | ||
| fn follow(&mut self, source: &Arc<dyn FeatureSource>, key: &str) { | ||
| let same = self.source.as_ref().and_then(Weak::upgrade).is_some_and(|old| Arc::ptr_eq(&old, source)); | ||
| if !same || self.key != key { | ||
| *self = KeptView { source: Some(Arc::downgrade(source)), key: key.into(), ..KeptView::default() }; | ||
| } | ||
| } | ||
|
|
||
| /// Forgets the chunks, keeping what was learnt of sizes. | ||
| fn drop_chunks(&mut self) { | ||
| self.read = Extent::EMPTY; | ||
| self.chunks = Arc::default(); | ||
| self.bytes = 0; | ||
| } | ||
|
|
||
| /// The kept chunks, when they were read for a box holding `near` and hold at most `room` | ||
| /// bytes; ones holding more (the source's share shrank) are let go. | ||
| pub(super) fn get(&mut self, source: &Arc<dyn FeatureSource>, key: &str, near: &Extent, room: usize) -> Option<Arc<Vec<Layer>>> { | ||
| self.follow(source, key); | ||
| if self.bytes > room { | ||
| self.drop_chunks(); | ||
| } | ||
| let r = &self.read; | ||
| // A view read with nothing in it is kept too (no chunks): its frames draw nothing. With | ||
| // nothing kept, `read` is empty and holds no box. | ||
| let holds = r.min_x <= near.min_x && r.min_y <= near.min_y && r.max_x >= near.max_x && r.max_y >= near.max_y; | ||
| holds.then(|| Arc::clone(&self.chunks)) | ||
| } | ||
|
|
||
| /// Lets the chunks go when they hold more than `room` bytes. | ||
| pub(super) fn trim(&mut self, room: usize) { | ||
| if self.bytes > room { | ||
| self.drop_chunks(); | ||
| } | ||
| } | ||
|
|
||
| /// Whether a view at resolution `res` is worth reading wider than it draws to keep: not | ||
| /// when one as coarse or coarser was already too large to keep. | ||
| pub(super) fn worth_keeping(&mut self, source: &Arc<dyn FeatureSource>, key: &str, res: f64) -> bool { | ||
| self.follow(source, key); | ||
| self.too_large_at.is_none_or(|at| res < at) | ||
| } | ||
|
|
||
| /// Keeps `chunks` of `bytes`, read for `read`, in place of what was kept; `None` when they | ||
| /// came to more than may be kept at resolution `res`, which is remembered. | ||
| pub(super) fn put(&mut self, source: &Arc<dyn FeatureSource>, key: &str, read: Extent, res: f64, chunks: Option<(Vec<Layer>, usize)>) { | ||
| self.follow(source, key); | ||
| match chunks { | ||
| Some((chunks, bytes)) => { | ||
| self.read = read; | ||
| self.chunks = Arc::new(chunks); | ||
| self.bytes = bytes; | ||
| } | ||
| None => { | ||
| self.drop_chunks(); | ||
| self.too_large_at = Some(self.too_large_at.map_or(res, |at| at.min(res))); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Puts `chunks` of `bytes` (the kept ones drawn again, with the geometry that drawing made, | ||
| /// a finer level of detail say) in place of `drawn`, when those are still what is kept; | ||
| /// another source or key kept since is left as it is. | ||
| pub(super) fn refresh( | ||
| &mut self, | ||
| source: &Arc<dyn FeatureSource>, | ||
| key: &str, | ||
| drawn: &Arc<Vec<Layer>>, | ||
| chunks: Vec<Layer>, | ||
| bytes: usize, | ||
| ) { | ||
| let same = self.source.as_ref().and_then(Weak::upgrade).is_some_and(|old| Arc::ptr_eq(&old, source)); | ||
| if same && self.key == key && Arc::ptr_eq(&self.chunks, drawn) { | ||
| self.chunks = Arc::new(chunks); | ||
| self.bytes = bytes; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use rustgis_data::source::{Batch, Request, SourceInfo}; | ||
|
|
||
| struct Empty(SourceInfo); | ||
| impl FeatureSource for Empty { | ||
| fn info(&self) -> &SourceInfo { | ||
| &self.0 | ||
| } | ||
| fn read(&self, _: &Request) -> rustgis_data::Result<Batch> { | ||
| Ok(Batch::default()) | ||
| } | ||
| } | ||
|
|
||
| /// A view too large to keep stops wider reads at its resolution and coarser ones, not at | ||
| /// finer ones; another projection or source starts afresh. | ||
| #[test] | ||
| fn a_view_too_large_to_keep_stops_wide_reads_as_coarse_or_coarser() { | ||
| let info = SourceInfo { | ||
| name: "s".into(), | ||
| format: "test", | ||
| geometry_type: rustgis_data::GeometryType::Point, | ||
| fields: Vec::new(), | ||
| extent: Extent::EMPTY, | ||
| count: Some(0), | ||
| crs: "EPSG:4326".into(), | ||
| indexed: true, | ||
| notes: Default::default(), | ||
| zm_dropped: false, | ||
| }; | ||
| let source: Arc<dyn FeatureSource> = Arc::new(Empty(info.clone())); | ||
| let mut kept = KeptView::default(); | ||
| assert!(kept.worth_keeping(&source, "3857", 100.0)); | ||
| kept.put(&source, "3857", Extent::new(0.0, 0.0, 1.0, 1.0), 100.0, None); | ||
| assert!(!kept.worth_keeping(&source, "3857", 100.0)); | ||
| assert!(!kept.worth_keeping(&source, "3857", 200.0)); | ||
| assert!(kept.worth_keeping(&source, "3857", 50.0)); | ||
| assert!(kept.get(&source, "3857", &Extent::new(0.2, 0.2, 0.4, 0.4), usize::MAX).is_none(), "nothing kept"); | ||
| assert!(kept.worth_keeping(&source, "robinson", 100.0), "another projection"); | ||
| let other: Arc<dyn FeatureSource> = Arc::new(Empty(info)); | ||
| assert!(kept.worth_keeping(&other, "3857", 100.0), "another source"); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This same-size case assumes the
.dbf's change time moves between opening and this rewrite.set_modifiedrestores only the modification time, so if both writes land in one kernel clock tick,same_as_openedstaystrueand this assertion fails. I haven't seen it fail:Stamp::ofreads the change time at opening, and kernels with multigrain timestamps stamp the next write finely after that, so I rate the flake risk low. If it ever shows up, wait for the clock to step before the rewrite (with a// test-wait:reason, as test-hygiene requires) rather than relaxing the assertion.Category: quality
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left for the release quality pass: the change time is taken at opening, and the rewrite comes after reading both files, so on current kernels it moves. If it ever flakes, the fix is a wait for the clock to step, as you say.