loro/crates/loro-internal/examples/encoding_refactored.rs
Zixuan Chen 09a004e365
refactor: Loro import function should return LoroEncodeError (#487)
* refactor: Loro import function should return LoroEncodeError

- Change return type
- Add support for handling potential errors:
  - Invalid or non-existent frontiers
  - Exporting old snapshot format from trimmed snapshot doc
- Improve compatibility with trimmed docs

* fix: add tests and fix a few places that need to return err
2024-10-02 11:11:08 +08:00

106 lines
3.2 KiB
Rust

use bench_utils::TextAction;
use criterion::black_box;
use loro_internal::LoroDoc;
fn main() {
log_size();
// bench_decode();
// bench_decode_updates();
}
#[allow(unused)]
fn log_size() {
let actions = bench_utils::get_automerge_actions();
{
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_with_txn(&mut txn, *pos, *del);
text.insert_with_txn(&mut txn, *pos, ins);
}
txn.commit().unwrap();
let snapshot = loro.export_snapshot().unwrap();
let updates = loro.export_from(&Default::default());
let json_updates =
serde_json::to_string(&loro.export_json_updates(&Default::default(), &loro.oplog_vv()))
.unwrap();
println!("\n");
println!("Snapshot size={}", snapshot.len());
println!("Updates size={}", updates.len());
println!("Json Updates size={}", json_updates.as_bytes().len());
println!("\n");
loro.diagnose_size();
}
println!("\n");
println!("\n");
println!("\n");
{
println!("One Transaction Per Action");
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_with_txn(&mut txn, *pos, *del);
text.insert_with_txn(&mut txn, *pos, ins);
txn.commit().unwrap();
}
let snapshot = loro.export_snapshot().unwrap();
let updates = loro.export_from(&Default::default());
println!("\n");
println!("Snapshot size={}", snapshot.len());
println!("Updates size={}", updates.len());
println!("\n");
loro.diagnose_size();
}
}
#[allow(unused)]
fn bench_decode() {
println!("Bench decode");
let actions = bench_utils::get_automerge_actions();
{
let mut loro = LoroDoc::default();
let text = loro.get_text("text");
loro.start_auto_commit();
for _ in 0..10 {
for TextAction { pos, ins, del } in actions.iter() {
text.delete(*pos, *del);
text.insert(*pos, ins);
}
}
let snapshot = loro.export_snapshot().unwrap();
// for _ in 0..100 {
// black_box(loro.export_snapshot());
// }
for _ in 0..100 {
let loro = LoroDoc::new();
loro.import(black_box(&snapshot)).unwrap();
}
}
}
#[allow(unused)]
fn bench_decode_updates() {
let actions = bench_utils::get_automerge_actions();
let loro = LoroDoc::default();
let text = loro.get_text("text");
#[allow(warnings)]
for TextAction { pos, ins, del } in actions.iter() {
let mut txn = loro.txn().unwrap();
text.delete_with_txn(&mut txn, *pos, *del);
text.insert_with_txn(&mut txn, *pos, ins);
txn.commit().unwrap();
}
let updates = loro.export_from(&Default::default());
for _ in 0..10 {
let loro = LoroDoc::new();
loro.import(black_box(&updates)).unwrap();
}
}