loro/crates/loro-internal/benches/text_r.rs
Zixuan Chen d942e3d7a2
Feat: Peritext-like rich text support (#123)
* feat: richtext wip

* feat: add insert to style range map wip

* feat: richtext state

* fix: fix style state inserting and style map

* fix: tiny vec merge err

* fix: comment err

* refactor: use new generic-btree & refine impl

* feat: fugue tracker

* feat: tracker

* feat: tracker

* fix: fix a few err in impl

* feat: init richtext content state

* feat: refactor arena

* feat: extract anchor_type info out of style flag

* refactor: state apply op more efficiently
we can now reuse the repr in state and op

* fix: new clippy errors

* refactor: use state chunk as delta item

* refactor: use two op to insert style start and style end

* feat: diff calc

* feat: handler

* fix: tracker checkout err

* fix: pass basic richtext handler tests

* fix: pass handler basic marking tests

* fix: pass all peritext criteria

* feat: snapshot encoding for richtext init

* refactor: replace Text with Richtext

* refacotr: rm text code

* fix: richtext checkout err

* refactor: diff of text and map

* refactor: del span

* refactor: event

* fix: fuzz err

* fix: pass all tests

* fix: fuzz err

* fix: list child cache err

* chore: rm debug code

* fix: encode enhanced err

* fix: encode enchanced

* fix: fix several richtext issue

* fix: richtext anchor err

* chore: rm debug code

* fix: richtext fuzz err

* feat: speedup text snapshot decode

* perf: optimize snapshot encoding

* perf: speed up decode & insert

* fix: fugue span merge err

* perf: speedup delete & id cursor map

* fix: fugue merge err

* chore: update utils

* perf: speedup text insert / del

* fix: cursor cache

* perf: reduce conversion by introducing InsertText

* perf: speed up by refined cursor cache

* chore: update gbtree dep

* refactor(wasm): use quill delta format

* chore: fix warnings
2023-10-29 14:02:13 +08:00

361 lines
13 KiB
Rust

use criterion::{criterion_group, criterion_main, Criterion};
#[cfg(feature = "test_utils")]
mod run {
use std::sync::Arc;
use super::*;
use bench_utils::TextAction;
use criterion::black_box;
use loro_common::LoroValue;
use loro_internal::loro::LoroDoc;
pub fn b4(c: &mut Criterion) {
let actions = bench_utils::get_automerge_actions();
let mut b = c.benchmark_group("refactored direct_apply");
b.sample_size(10);
b.bench_function("B4", |b| {
b.iter(|| {
let loro = LoroDoc::default();
let text = loro.get_text("text");
let mut txn = loro.txn().unwrap();
for TextAction { pos, ins, del } in actions.iter() {
text.delete(&mut txn, *pos, *del).unwrap();
text.insert(&mut txn, *pos, ins).unwrap();
}
})
});
b.bench_function("B4 with 100K actors history", |b| {
let store = LoroDoc::default();
for i in 0..100_000 {
store.set_peer_id(i);
let list = store.get_list("list");
let value: LoroValue = i.to_string().into();
let mut txn = store.txn().unwrap();
list.insert(&mut txn, 0, value).unwrap();
txn.commit().unwrap();
}
let update = store.export_snapshot();
drop(store);
b.iter_batched(
|| {
let loro = LoroDoc::default();
loro.import(&update).unwrap();
loro
},
|loro| {
let text = loro.get_text("text");
let mut txn = loro.txn().unwrap();
for TextAction { pos, ins, del } in actions.iter() {
text.delete(&mut txn, *pos, *del).unwrap();
text.insert(&mut txn, *pos, ins).unwrap();
}
},
criterion::BatchSize::SmallInput,
)
});
b.bench_function("B4 Obs", |b| {
b.iter(|| {
let loro = LoroDoc::default();
let text = loro.get_text("text");
loro.subscribe_deep(Arc::new(move |event| {
black_box(event);
}));
let mut txn = loro.txn().unwrap();
for TextAction { pos, ins, del } in actions.iter() {
text.delete(&mut txn, *pos, *del).unwrap();
text.insert(&mut txn, *pos, ins).unwrap();
}
})
});
b.bench_function("B4 encode snapshot", |b| {
let loro = LoroDoc::default();
let text = loro.get_text("text");
let mut n = 0;
let mut txn = loro.txn().unwrap();
for TextAction { pos, ins, del } in actions.iter() {
if n == 10 {
n = 0;
drop(txn);
txn = loro.txn().unwrap();
}
n += 1;
text.delete(&mut txn, *pos, *del).unwrap();
text.insert(&mut txn, *pos, ins).unwrap();
}
txn.commit().unwrap();
b.iter(|| {
loro.export_snapshot();
});
});
b.bench_function("B4 encode updates", |b| {
let loro = LoroDoc::default();
let text = loro.get_text("text");
let mut n = 0;
let mut txn = loro.txn().unwrap();
for TextAction { pos, ins, del } in actions.iter() {
if n == 10 {
n = 0;
drop(txn);
txn = loro.txn().unwrap();
}
n += 1;
text.delete(&mut txn, *pos, *del).unwrap();
text.insert(&mut txn, *pos, ins).unwrap();
}
txn.commit().unwrap();
b.iter(|| {
loro.export_from(&Default::default());
});
});
b.bench_function("B4 decode snapshot", |b| {
let loro = LoroDoc::default();
let text = loro.get_text("text");
let mut n = 0;
let mut txn = loro.txn().unwrap();
for TextAction { pos, ins, del } in actions.iter() {
if n == 10 {
n = 0;
drop(txn);
txn = loro.txn().unwrap();
}
n += 1;
text.delete(&mut txn, *pos, *del).unwrap();
text.insert(&mut txn, *pos, ins).unwrap();
}
txn.commit().unwrap();
let data = loro.export_snapshot();
b.iter(|| {
let l = LoroDoc::new();
l.import(&data).unwrap();
});
});
b.bench_function("B4 import updates", |b| {
let loro = LoroDoc::default();
let text = loro.get_text("text");
let mut n = 0;
let mut txn = loro.txn().unwrap();
for TextAction { pos, ins, del } in actions.iter() {
if n == 10 {
n = 0;
drop(txn);
txn = loro.txn().unwrap();
}
n += 1;
text.delete(&mut txn, *pos, *del).unwrap();
text.insert(&mut txn, *pos, ins).unwrap();
}
txn.commit().unwrap();
let data = loro.export_from(&Default::default());
b.iter(|| {
let l = LoroDoc::new();
l.import(&data).unwrap();
});
});
// b.bench_function("B4 utf16", |b| {
// b.iter(|| {
// let loro = LoroDoc::new();
// let text = loro.get_text("text");
// let mut txn = loro.txn().unwrap();
// for TextAction { pos, ins, del } in actions.iter() {
// text.delete_utf16(&mut txn, *pos, *del).unwrap();
// text.insert_utf16(&mut txn, *pos, ins).unwrap();
// }
// })
// });
b.bench_function("B4_Per100_Txn", |b| {
b.iter(|| {
let loro = LoroDoc::default();
let text = loro.get_text("text");
let mut n = 0;
let mut txn = loro.txn().unwrap();
for TextAction { pos, ins, del } in actions.iter() {
if n == 100 {
n = 0;
drop(txn);
txn = loro.txn().unwrap();
}
n += 1;
text.delete(&mut txn, *pos, *del).unwrap();
text.insert(&mut txn, *pos, ins).unwrap();
}
})
});
b.bench_function("B4 One Op One Txn", |b| {
b.iter(|| {
let loro = LoroDoc::default();
let text = loro.get_text("text");
{
for TextAction { pos, ins, del } in actions.iter() {
let mut txn = loro.txn().unwrap();
text.delete(&mut txn, *pos, *del).unwrap();
text.insert(&mut txn, *pos, ins).unwrap();
txn.commit().unwrap();
}
}
})
});
b.bench_function("B4 One Op One Txn Obs", |b| {
b.iter(|| {
let loro = LoroDoc::default();
let text = loro.get_text("text");
loro.subscribe_deep(Arc::new(move |event| {
black_box(event);
}));
{
for TextAction { pos, ins, del } in actions.iter() {
let mut txn = loro.txn().unwrap();
text.delete(&mut txn, *pos, *del).unwrap();
text.insert(&mut txn, *pos, ins).unwrap();
txn.commit().unwrap();
}
}
})
});
b.bench_function("B4DirectSync", |b| {
b.iter(|| {
let loro = LoroDoc::default();
let loro_b = LoroDoc::default();
let text = loro.get_text("text");
for TextAction { pos, ins, del } in actions.iter() {
{
let mut txn = loro.txn().unwrap();
text.delete(&mut txn, *pos, *del).unwrap();
text.insert(&mut txn, *pos, ins).unwrap();
}
loro_b
.import(&loro.export_from(&loro_b.oplog_vv()))
.unwrap();
}
})
});
drop(b);
let mut b = c.benchmark_group("refactored-sync");
b.bench_function("B4Parallel", |b| {
b.iter(|| {
let loro = LoroDoc::default();
let loro_b = LoroDoc::default();
let text = loro.get_text("text");
let text2 = loro_b.get_text("text");
let mut i = 0;
for TextAction { pos, ins, del } in actions.iter() {
let pos = *pos;
let del = *del;
i += 1;
if i > 1000 {
break;
}
{
let mut txn = loro.txn().unwrap();
text.delete(&mut txn, pos, del).unwrap();
text.insert(&mut txn, pos, ins).unwrap();
}
{
let mut txn = loro_b.txn().unwrap();
text2.delete(&mut txn, pos, del).unwrap();
text2.insert(&mut txn, pos, ins).unwrap();
}
loro_b
.import(&loro.export_from(&loro_b.oplog_vv()))
.unwrap();
loro.import(&loro_b.export_from(&loro.oplog_vv())).unwrap();
}
})
});
let b = b.sample_size(10);
b.bench_function("DecodeUpdates B4Parallel", |b| {
let loro = LoroDoc::default();
let loro_b = LoroDoc::default();
let text = loro.get_text("text");
let text2 = loro_b.get_text("text");
for TextAction { pos, ins, del } in actions.iter() {
let pos = *pos;
let del = *del;
{
let mut txn = loro.txn().unwrap();
text.delete(&mut txn, pos, del).unwrap();
text.insert(&mut txn, pos, ins).unwrap();
}
{
let mut txn = loro_b.txn().unwrap();
text2.delete(&mut txn, pos, del).unwrap();
text2.insert(&mut txn, pos, ins).unwrap();
}
loro_b
.import(&loro.export_from(&loro_b.oplog_vv()))
.unwrap();
loro.import(&loro_b.export_from(&loro.oplog_vv())).unwrap();
}
let data = loro.export_from(&Default::default());
b.iter(|| {
let loro = LoroDoc::default();
loro.import(&data).unwrap();
})
});
b.bench_function("DecodeSnapshot B4Parallel", |b| {
let loro = LoroDoc::default();
let loro_b = LoroDoc::default();
let text = loro.get_text("text");
let text2 = loro_b.get_text("text");
for TextAction { pos, ins, del } in actions.iter() {
let pos = *pos;
let del = *del;
{
let mut txn = loro.txn().unwrap();
text.delete(&mut txn, pos, del).unwrap();
text.insert(&mut txn, pos, ins).unwrap();
}
{
let mut txn = loro_b.txn().unwrap();
text2.delete(&mut txn, pos, del).unwrap();
text2.insert(&mut txn, pos, ins).unwrap();
}
loro_b
.import(&loro.export_from(&loro_b.oplog_vv()))
.unwrap();
loro.import(&loro_b.export_from(&loro.oplog_vv())).unwrap();
}
let data = loro.export_snapshot();
b.iter(|| {
let loro = LoroDoc::default();
loro.import(&data).unwrap();
})
});
}
}
pub fn dumb(_c: &mut Criterion) {}
#[cfg(feature = "test_utils")]
criterion_group!(benches, run::b4);
#[cfg(not(feature = "test_utils"))]
criterion_group!(benches, dumb);
criterion_main!(benches);