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
5 changes: 5 additions & 0 deletions .changeset/zero-rust-prepare-sorts-rows.md
Original file line number Diff line number Diff line change
@@ -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.
31 changes: 25 additions & 6 deletions rust/zero-client/src/client/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -29,6 +30,9 @@ pub(crate) struct GroupData {
pub(crate) cookie: Value,
pub(crate) last_mutation_ids: BTreeMap<String, f64>,
pub(crate) mutations: Vec<StoredMutation>,
/// The rows of `entries` sorted for the query sources, when
/// `Zero::prepare` read the group.
pub(crate) sources: Option<SortedRows>,
}

impl From<StoredGroup> for GroupData {
Expand All @@ -38,6 +42,7 @@ impl From<StoredGroup> for GroupData {
cookie: g.cookie,
last_mutation_ids: g.last_mutation_ids,
mutations: g.mutations,
sources: None,
}
}
}
Expand All @@ -46,13 +51,14 @@ type OnDelete = Rc<dyn Fn(&str)>;
type FailureListener = Rc<dyn Fn(&StorageFailure)>;

/// 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.
Expand Down Expand Up @@ -83,7 +89,7 @@ pub(crate) struct ReplicaDatabase {
/// The cookie of each client group's stored snapshot.
snapshot_cookies: RefCell<HashMap<String, Value>>,
/// The group `Zero::prepare` read, until a client joins it.
prepared_group: RefCell<Option<(String, StoredGroup)>>,
prepared_group: RefCell<Option<(String, GroupData)>>,
}

thread_local! {
Expand Down Expand Up @@ -246,11 +252,18 @@ impl ReplicaDatabase {
directory: Option<std::path::PathBuf>,
name: String,
mutator_names: &[String],
schema: &crate::schema::Schema,
) -> Option<PreparedDatabase> {
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();
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()),
Expand Down
6 changes: 5 additions & 1 deletion rust/zero-client/src/client/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,7 @@ impl Engine {
ok
})
.collect();
let sorted_sources = group.sources;
let store = ReplicaStore::new(
Snapshot {
map: group.entries,
Expand Down Expand Up @@ -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();
Expand Down
160 changes: 158 additions & 2 deletions rust/zero-client/src/client/ivm_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<Object>),
/// The keys of a source that isn't a table of the schema, for `load`.
Keys(Vec<String>),
}

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<Item = (&'a String, &'a Value)>,
) -> 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<Object> = run.map(|(_, v)| object(v)).collect();
let compare = comparator::<Object>(&MemorySource::primary_key_order(table), false);
rows.sort_by(&compare);
let mut deduped: Vec<Object> = 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)]
Expand Down Expand Up @@ -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<Item = Op<'a>>, written: &HashMap<String, Row>) {
Expand Down Expand Up @@ -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<String> = 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<String> = 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"]);
}
}
2 changes: 1 addition & 1 deletion rust/zero-client/src/client/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn read_in_background<C>(
mutate_url.as_deref(),
query_url.as_deref(),
);
ReplicaDatabase::prepare(directory, name, &names)
ReplicaDatabase::prepare(directory, name, &names, &schema)
})
.ok()
}
Expand Down
17 changes: 14 additions & 3 deletions rust/zero-client/src/ivm/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<Row>(order, reverse))
}

/// [`make_comparator`]'s order, of rows or of objects, which can be sorted
/// on another thread.
pub(crate) fn comparator<R: Borrow<Object>>(
order: &[OrderPart],
reverse: bool,
) -> impl Fn(&R, &R) -> Ordering + use<R> {
let parts: Vec<(String, bool, Cell<usize>)> = 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 {
Expand All @@ -61,5 +72,5 @@ pub fn make_comparator(order: &[OrderPart], reverse: bool) -> Comparator {
}
}
Ordering::Equal
})
}
}
Loading
Loading