fix: opt offset [lamport]

This commit is contained in:
leeeon233 2023-02-17 09:52:14 +08:00 committed by Leonzhao
parent 033916c532
commit 7c8aa72969
3 changed files with 35 additions and 23 deletions

View file

@ -135,12 +135,7 @@ mod run {
mod import { mod import {
use criterion::Criterion; use criterion::Criterion;
use loro_internal::{ use loro_internal::{change::ChangeMergeCfg, configure::Configure, LoroCore};
change::ChangeMergeCfg,
configure::Configure,
log_store::{EncodeConfig, EncodeMode},
LoroCore,
};
pub fn causal_iter(c: &mut Criterion) { pub fn causal_iter(c: &mut Criterion) {
let mut b = c.benchmark_group("causal_iter"); let mut b = c.benchmark_group("causal_iter");

View file

@ -0,0 +1,27 @@
use bench_utils::{get_automerge_actions, TextAction};
use loro_internal::{log_store::EncodeConfig, LoroCore, VersionVector};
fn main() {
let actions = get_automerge_actions();
let mut loro = LoroCore::default();
let mut loro_b = LoroCore::default();
let mut i = 0;
for TextAction { pos, ins, del } in actions.iter() {
i += 1;
if i > 1000 {
break;
}
let pos = *pos;
let del = *del;
let mut text = loro.get_text("text");
text.delete(&loro, pos, del).unwrap();
text.insert(&loro, pos, ins).unwrap();
let mut text = loro_b.get_text("text");
text.delete(&loro_b, pos, del).unwrap();
text.insert(&loro_b, pos, ins).unwrap();
loro_b.import(loro.export(loro_b.vv_cloned()));
loro.import(loro_b.export(loro.vv_cloned()));
}
let encoded = loro.encode_with_cfg(EncodeConfig::rle_update(VersionVector::new()));
println!("parallel doc size {} bytes", encoded.len());
}

View file

@ -114,7 +114,6 @@ pub(super) fn encode_changes(store: &LogStore, vv: &VersionVector) -> Result<Vec
idx idx
}); });
start_counter.push(changes.first().unwrap().id.counter); start_counter.push(changes.first().unwrap().id.counter);
diff_changes.extend(changes); diff_changes.extend(changes);
} }
@ -341,19 +340,10 @@ pub(super) fn decode_changes_to_inner_format(
match get_lamport_by_deps(&change.deps, &lamport_map, Some(store)) { match get_lamport_by_deps(&change.deps, &lamport_map, Some(store)) {
Ok(lamport) => { Ok(lamport) => {
change.lamport = lamport; change.lamport = lamport;
let entry = lamport_map.entry(client_id).or_insert_with(Vec::new); lamport_map.entry(client_id).or_insert_with(Vec::new).push((
match entry.binary_search_by_key(&change.id.counter, |(r, _)| r.start) { change.id.counter..change.id.counter + change.content_len() as Counter,
Err(pos) => entry.insert( lamport,
pos, ));
(
change.id.counter
..change.id.counter + change.content_len() as Counter,
lamport + change.content_len() as u32 - 1,
),
),
// already exist?
Ok(_) => unreachable!(),
}
changes_ans changes_ans
.entry(client_id) .entry(client_id)
.or_insert_with(Vec::new) .or_insert_with(Vec::new)
@ -398,7 +388,7 @@ pub(super) fn get_lamport_by_deps(
} }
} else if let Some(v) = lamport_map.get(&id.client_id) { } else if let Some(v) = lamport_map.get(&id.client_id) {
if let Some((lamport, offset)) = get_value_from_range_map(v, id.counter) { if let Some((lamport, offset)) = get_value_from_range_map(v, id.counter) {
ans.push(lamport - offset as u32); ans.push(lamport + offset);
} else { } else {
return Err(id.client_id); return Err(id.client_id);
} }
@ -412,7 +402,7 @@ pub(super) fn get_lamport_by_deps(
fn get_value_from_range_map( fn get_value_from_range_map(
v: &[(Range<Counter>, Lamport)], v: &[(Range<Counter>, Lamport)],
key: Counter, key: Counter,
) -> Option<(Lamport, i32)> { ) -> Option<(Lamport, u32)> {
let index = match v.binary_search_by_key(&key, |&(ref range, _)| range.start) { let index = match v.binary_search_by_key(&key, |&(ref range, _)| range.start) {
Ok(index) => Some(index), Ok(index) => Some(index),
@ -425,7 +415,7 @@ fn get_value_from_range_map(
if let Some(index) = index { if let Some(index) = index {
let (ref range, value) = v[index]; let (ref range, value) = v[index];
if key < range.end { if key < range.end {
return Some((value, range.end - key - 1)); return Some((value, (key - range.start) as u32));
} }
} }
None None