diff --git a/.changeset/zero-rust-prepare-sorts-rows.md b/.changeset/zero-rust-prepare-sorts-rows.md new file mode 100644 index 0000000000..a5a39991e7 --- /dev/null +++ b/.changeset/zero-rust-prepare-sorts-rows.md @@ -0,0 +1,5 @@ +--- +'zero-rust': patch +--- + +`Zero::from_prepared` takes less of the app's thread. On a Mac, it takes 2.1 ms instead of 3.8 ms for a replica of 8,000 rows, and 1.6 ms instead of 2.2 ms for 2,000; on Linux, 6.2 ms instead of 9.6 ms for 8,000. `Zero::prepare` now also sorts each table's rows for the query sources on its own thread, so the app's thread only puts them into the sources and hydrates the first views. diff --git a/rust/zero-client/src/client/database.rs b/rust/zero-client/src/client/database.rs index 4cab1e5b85..fc51b33dd3 100644 --- a/rust/zero-client/src/client/database.rs +++ b/rust/zero-client/src/client/database.rs @@ -8,6 +8,7 @@ //! `kvStore: 'mem'` for memory), and two clients of one group (two windows) //! share one open database. +use super::ivm_branch::SortedRows; use super::replica_store::{KvMap, compare_cookies}; pub(crate) use super::storage::{ AggregateWrites, ClientGroupRecord, ClientIdPair, ClientRecord, StorageOp, StoredMutation, @@ -29,6 +30,9 @@ pub(crate) struct GroupData { pub(crate) cookie: Value, pub(crate) last_mutation_ids: BTreeMap, pub(crate) mutations: Vec, + /// The rows of `entries` sorted for the query sources, when + /// `Zero::prepare` read the group. + pub(crate) sources: Option, } impl From for GroupData { @@ -38,6 +42,7 @@ impl From for GroupData { cookie: g.cookie, last_mutation_ids: g.last_mutation_ids, mutations: g.mutations, + sources: None, } } } @@ -46,13 +51,14 @@ type OnDelete = Rc; type FailureListener = Rc; /// A client's database opened and read by `Zero::prepare` on its own -/// thread: the clients, and the group a client with its mutators joins. +/// thread: the clients, and the group a client with its mutators joins, +/// with its replica's rows sorted for the query sources. #[cfg(feature = "sqlite")] pub(crate) struct PreparedDatabase { name: String, sqlite: super::sqlite::PreparedSqlite, clients: StoredClients, - group: Option<(String, StoredGroup)>, + group: Option<(String, StoredGroup, SortedRows)>, } /// Without SQLite there is nothing to prepare. @@ -83,7 +89,7 @@ pub(crate) struct ReplicaDatabase { /// The cookie of each client group's stored snapshot. snapshot_cookies: RefCell>, /// The group `Zero::prepare` read, until a client joins it. - prepared_group: RefCell>, + prepared_group: RefCell>, } thread_local! { @@ -246,11 +252,18 @@ impl ReplicaDatabase { directory: Option, name: String, mutator_names: &[String], + schema: &crate::schema::Schema, ) -> Option { let mut sqlite = super::sqlite::PreparedSqlite::open(directory, &name).ok()?; let clients = sqlite.database().load().ok()?; let group = match matching_group(&clients.groups, mutator_names) { - Some(g) => Some((g.id.clone(), sqlite.database().load_group(&g.id).ok()?)), + // Here, rather than on the client's thread: the replica's rows + // sorted as the query sources keep them. + Some(g) => { + let group = sqlite.database().load_group(&g.id).ok()?; + let sorted = SortedRows::new(schema, group.entries.iter()); + Some((g.id.clone(), group, sorted)) + } None => None, }; sqlite.mark_read(); @@ -281,7 +294,13 @@ impl ReplicaDatabase { Some(prepared.sqlite.into_parts()), ); db.set_clients(prepared.clients); - *db.prepared_group.borrow_mut() = prepared.group; + *db.prepared_group.borrow_mut() = prepared.group.map(|(id, group, sorted)| { + let group = GroupData { + sources: Some(sorted), + ..group.into() + }; + (id, group) + }); let db = Rc::new(db); OPEN.with(|o| o.borrow_mut().insert(key, db.clone())); Some(db) @@ -580,7 +599,7 @@ impl ReplicaDatabase { return Ok(GroupData::default()); }; if let Some((_, g)) = self.prepared_group.borrow_mut().take_if(|(g, _)| g == id) { - return Ok(g.into()); + return Ok(g); } match store.load_group(id) { Ok(g) => Ok(g.into()), diff --git a/rust/zero-client/src/client/engine.rs b/rust/zero-client/src/client/engine.rs index 6a0251f685..58c324af99 100644 --- a/rust/zero-client/src/client/engine.rs +++ b/rust/zero-client/src/client/engine.rs @@ -814,6 +814,7 @@ impl Engine { ok }) .collect(); + let sorted_sources = group.sources; let store = ReplicaStore::new( Snapshot { map: group.entries, @@ -1001,7 +1002,10 @@ impl Engine { // mutations on it, with the synced aggregate rows they wrote when // they were last persisted, as JS's stored commits have them. let snapshot = e.store.borrow().snapshot.map.clone(); - e.ivm_main.load(&snapshot); + match sorted_sources { + Some(sorted) => e.ivm_main.load_sorted(&snapshot, sorted), + None => e.ivm_main.load(&snapshot), + } let pending = e.store.borrow().pending.clone(); if !pending.is_empty() { let snapshot_map = e.store.borrow().snapshot.map.clone(); diff --git a/rust/zero-client/src/client/ivm_branch.rs b/rust/zero-client/src/client/ivm_branch.rs index 0916a1ec2a..18110596d8 100644 --- a/rust/zero-client/src/client/ivm_branch.rs +++ b/rust/zero-client/src/client/ivm_branch.rs @@ -3,7 +3,7 @@ use super::replica_store::{KvDiff, KvDiffOp, KvMap}; use crate::builder::OptimisticAggregate; -use crate::ivm::{MemorySource, Row, Source, SourceChange, has_failed}; +use crate::ivm::{MemorySource, Row, Source, SourceChange, comparator, has_failed}; use crate::json::{Object, Value}; use crate::keys::{ AGGREGATE_TABLE_PREFIX, AGGREGATES_PREFIX, ENTITIES_PREFIX, ROW_VERSION_COLUMN, @@ -42,6 +42,63 @@ impl Layouts { } } +/// The rows of a replica's tables as [`IvmBranch::load`] loads them, each +/// table's in the order of its primary key: sorted on `Zero::prepare`'s +/// thread, so that the client's thread only makes their sources' trees. +#[derive(Clone, Debug, Default)] +pub(crate) struct SortedRows(Vec<(String, Run)>); + +/// A run of one source's keys in a [`SortedRows`]. +#[derive(Clone, Debug)] +enum Run { + /// A table's rows, sorted. + Sorted(Vec), + /// The keys of a source that isn't a table of the schema, for `load`. + Keys(Vec), +} + +impl SortedRows { + /// The rows of the tables of `schema` among `entries`, a replica's in + /// key order, under `e/`, by table; equal rows of a table keep the last, + /// as `load` does. A source that isn't a table of `schema` is left to + /// `load`, by its keys. + pub(crate) fn new<'a>( + schema: &Schema, + entries: impl Iterator, + ) -> Self { + let mut tables = Vec::new(); + let object = |v: &Value| v.as_object().cloned().unwrap_or_default(); + let mut entries = entries + .skip_while(|(k, _)| !k.starts_with(ENTITIES_PREFIX)) + .take_while(|(k, _)| k.starts_with(ENTITIES_PREFIX)) + .peekable(); + while let Some((key, v)) = entries.next() { + // A run of one source's keys, as `apply` pushes them together. + let name = source_name(key); + let prefix = format!("{ENTITIES_PREFIX}{name}/"); + let run = std::iter::once((key, v)).chain(std::iter::from_fn(|| { + entries.next_if(|(k, _)| k.starts_with(&prefix)) + })); + let Some(table) = schema.tables.get(&name) else { + tables.push((name, Run::Keys(run.map(|(k, _)| k.clone()).collect()))); + continue; + }; + let mut rows: Vec = run.map(|(_, v)| object(v)).collect(); + let compare = comparator::(&MemorySource::primary_key_order(table), false); + rows.sort_by(&compare); + let mut deduped: Vec = Vec::with_capacity(rows.len()); + for row in rows { + match deduped.last_mut() { + Some(last) if compare(last, &row) == std::cmp::Ordering::Equal => *last = row, + _ => deduped.push(row), + } + } + tables.push((name, Run::Sorted(deduped))); + } + SortedRows(tables) + } +} + /// A change of a diff, by its key, borrowed: a [`KvDiffOp`], or an entry /// of a map read as its add. #[derive(Clone, Copy)] @@ -221,7 +278,32 @@ impl IvmBranch { /// Loads the rows of `map` into empty sources: `apply` of the diff /// that adds each of its entries, without making that diff. pub(crate) fn load(&self, map: &KvMap) { - self.apply_ops(map.iter().map(|(k, v)| Op::Add(k, v)), &HashMap::new()); + self.load_sorted(map, SortedRows::new(&self.schema, map.iter())); + } + + /// [`load`](Self::load) of `map` with its tables' rows sorted already, + /// as `Zero::prepare` sorts them on its own thread: `sorted` is + /// `SortedRows::new` of `map`'s entries. + pub(crate) fn load_sorted(&self, map: &KvMap, sorted: SortedRows) { + // The synced aggregates' rows, which come first in key order. + let aggregates = map.scan(AGGREGATES_PREFIX).map(|(k, v)| Op::Add(k, v)); + self.apply_ops(aggregates, &HashMap::new()); + for (name, rows) in sorted.0 { + if has_failed() { + return; + } + match rows { + Run::Sorted(rows) => { + if let Some(source) = self.source(&name) { + source.push_sorted_adds(rows.into_iter().map(Rc::new).collect()); + } + } + Run::Keys(keys) => { + let ops = keys.iter().filter_map(|k| Some(Op::Add(k, map.get(k)?))); + self.apply_ops(ops, &HashMap::new()); + } + } + } } fn apply_ops<'a>(&self, ops: impl Iterator>, written: &HashMap) { @@ -353,3 +435,77 @@ fn value_type(v: &Value) -> ValueType { Value::Array(_) | Value::Object(_) => ValueType::Json, } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::client::replica_store::adds; + use crate::json; + + /// `load`, which sorts each table's rows first, as `Zero::prepare` does + /// on its thread, loads the sources `apply` of the adds of every entry + /// does: each table's rows in key order, a table the schema doesn't + /// have left out, and the synced aggregates' rows. + #[test] + fn load_loads_what_applying_adds_loads() { + let schema = json::parse( + r#"{"tables": { + "issue": {"name": "issue", "primaryKey": ["n"], + "columns": {"id": {"type": "string"}, "n": {"type": "number"}}}, + "issueLabel": {"name": "issueLabel", "primaryKey": ["labelID", "issueID"], + "columns": {"issueID": {"type": "string"}, "labelID": {"type": "string"}}}}}"#, + ) + .unwrap(); + let schema = Arc::new(Schema::from_json(&schema).unwrap()); + let mut map = KvMap::default(); + let row = |text: &str| json::parse(text).unwrap(); + // Keys whose order isn't the primary key's: "10" before "9". + for i in [3, 10, 9, 1] { + map.set( + format!("e/issue/{i}"), + row(&format!(r#"{{"id": "i{i}", "n": {i}}}"#)), + ); + } + for (label, issue) in [("l2", "i1"), ("l1", "i3"), ("l1", "i1")] { + map.set( + format!("e/issueLabel/{label}{issue}"), + row(&format!( + r#"{{"issueID": "{issue}", "labelID": "{label}"}}"# + )), + ); + } + map.set("e/gone/x", row(r#"{"id": "x"}"#)); + map.set( + "a/q1/k", + row(r#"{"_0_key": "k", "_0_value": 2, "_0_count": 2}"#), + ); + map.set("m/c/1", row("{}")); + + let applied = IvmBranch::new(schema.clone()); + applied.apply(&adds(map.iter()), &HashMap::new()); + let loaded = IvmBranch::new(schema.clone()); + loaded.load(&map); + let names = |b: &IvmBranch| { + let mut names: Vec = b.sources.borrow().keys().cloned().collect(); + names.sort(); + names + }; + assert_eq!(names(&loaded), names(&applied)); + assert_eq!( + names(&loaded), + ["aggregate:q1", "gone", "issue", "issueLabel"] + ); + for name in names(&loaded) { + let rows = |b: &IvmBranch| b.source(&name).map(|s| (*s.data()).clone()); + assert_eq!(rows(&loaded), rows(&applied), "{name}"); + } + let ids: Vec = loaded + .source("issue") + .unwrap() + .data() + .iter() + .map(|r| r.get("id").unwrap().as_str().unwrap().to_owned()) + .collect(); + assert_eq!(ids, ["i1", "i3", "i9", "i10"]); + } +} diff --git a/rust/zero-client/src/client/prepare.rs b/rust/zero-client/src/client/prepare.rs index 086e57ebf8..03e1d1739a 100644 --- a/rust/zero-client/src/client/prepare.rs +++ b/rust/zero-client/src/client/prepare.rs @@ -120,7 +120,7 @@ fn read_in_background( mutate_url.as_deref(), query_url.as_deref(), ); - ReplicaDatabase::prepare(directory, name, &names) + ReplicaDatabase::prepare(directory, name, &names, &schema) }) .ok() } diff --git a/rust/zero-client/src/ivm/data.rs b/rust/zero-client/src/ivm/data.rs index c56805bcd9..d0a24d2404 100644 --- a/rust/zero-client/src/ivm/data.rs +++ b/rust/zero-client/src/ivm/data.rs @@ -4,7 +4,8 @@ use super::Row; use crate::ast::{Direction, OrderPart}; use crate::compare; -use crate::json::Value; +use crate::json::{Object, Value}; +use std::borrow::Borrow; use std::cell::Cell; use std::cmp::Ordering; use std::rc::Rc; @@ -48,11 +49,21 @@ pub fn identical(a: Option<&Value>, b: Option<&Value>) -> bool { /// `makeComparator(order, reverse)`. pub fn make_comparator(order: &[OrderPart], reverse: bool) -> Comparator { + Rc::new(comparator::(order, reverse)) +} + +/// [`make_comparator`]'s order, of rows or of objects, which can be sorted +/// on another thread. +pub(crate) fn comparator>( + order: &[OrderPart], + reverse: bool, +) -> impl Fn(&R, &R) -> Ordering + use { let parts: Vec<(String, bool, Cell)> = order .iter() .map(|p| (p.field.clone(), p.direction == Direction::Asc, Cell::new(0))) .collect(); - Rc::new(move |a: &Row, b: &Row| { + move |a: &R, b: &R| { + let (a, b) = (a.borrow(), b.borrow()); for (field, asc, hint) in &parts { let c = compare_values(a.get_hinted(field, hint), b.get_hinted(field, hint)); if c != Ordering::Equal { @@ -61,5 +72,5 @@ pub fn make_comparator(order: &[OrderPart], reverse: bool) -> Comparator { } } Ordering::Equal - }) + } } diff --git a/rust/zero-client/src/ivm/memory_source.rs b/rust/zero-client/src/ivm/memory_source.rs index 040672ecaa..b3eb8d319a 100644 --- a/rust/zero-client/src/ivm/memory_source.rs +++ b/rust/zero-client/src/ivm/memory_source.rs @@ -144,14 +144,7 @@ fn index_key(sort: &[OrderPart]) -> String { impl MemorySource { pub fn new(table: Table) -> Rc { - let primary_index_sort: Vec = table - .primary_key - .iter() - .map(|k| OrderPart { - field: k.clone(), - direction: Direction::Asc, - }) - .collect(); + let primary_index_sort = Self::primary_key_order(&table); let primary_index_key = index_key(&primary_index_sort); let mut state = State::default(); state.indexes.insert( @@ -423,16 +416,46 @@ impl MemorySource { Box::new(merged.filter(move |n| rest.iter().all(|mc| mc.iter().any(|c| c.matches(&n.row))))) } + /// Whether nothing is connected and the source is empty, so that adds + /// can build its primary index in one pass. + fn can_bulk_load(&self) -> bool { + let state = self.state.borrow(); + state.connections.is_empty() + && state.indexes.len() == 1 + && state.indexes[&self.primary_index_key].data.is_empty() + } + + /// `push_adds` of rows already in the order of the primary key with no + /// two equal, as `Zero::prepare` sorts a stored replica's. + pub(crate) fn push_sorted_adds(&self, rows: Vec) { + if !self.can_bulk_load() { + return self.push_adds(rows); + } + let mut state = self.state.borrow_mut(); + let index = state + .indexes + .get_mut(&self.primary_index_key) + .expect("primary"); + index.data = Tree::from_sorted(rows); + } + + /// The order of the primary key, of objects, for sorting rows on + /// another thread for [`push_sorted_adds`](Self::push_sorted_adds). + pub(crate) fn primary_key_order(table: &Table) -> Vec { + table + .primary_key + .iter() + .map(|k| OrderPart { + field: k.clone(), + direction: Direction::Asc, + }) + .collect() + } + /// Pushes an add for each of `rows`. When nothing is connected and the /// source is empty, builds the primary index in one pass instead. pub fn push_adds(&self, rows: Vec) { - let can_bulk_load = { - let state = self.state.borrow(); - state.connections.is_empty() - && state.indexes.len() == 1 - && state.indexes[&self.primary_index_key].data.is_empty() - }; - if !can_bulk_load { + if !self.can_bulk_load() { for row in rows { if has_failed() { return;