diff --git a/benches/repo_cache_bench.rs b/benches/repo_cache_bench.rs index 3d13a1a..8b44a4f 100644 --- a/benches/repo_cache_bench.rs +++ b/benches/repo_cache_bench.rs @@ -1,8 +1,8 @@ -use criterion::{criterion_group, criterion_main, Criterion, BenchmarkId}; -use std::collections::{BTreeMap, HashMap}; -use std::hint::black_box; +use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; use hashbrown::HashMap as HashbrownMap; use rustc_hash::FxHashMap; +use std::collections::{BTreeMap, HashMap}; +use std::hint::black_box; // Тестовая структура (аналог вашей сущности) #[derive(Debug, Clone)] @@ -117,40 +117,60 @@ fn bench_insert(c: &mut Criterion) { for size in [10_000, 100_000, 500_000] { let entities = generate_entities(size); - group.bench_with_input(BenchmarkId::new("Vec (sorted)", size), &entities, |b, entities| { - let mut vec = Vec::with_capacity(size); - b.iter(|| { - bench_vec_insert_sorted(&mut vec, black_box(entities)); - }); - }); + group.bench_with_input( + BenchmarkId::new("Vec (sorted)", size), + &entities, + |b, entities| { + let mut vec = Vec::with_capacity(size); + b.iter(|| { + bench_vec_insert_sorted(&mut vec, black_box(entities)); + }); + }, + ); - group.bench_with_input(BenchmarkId::new("BTreeMap", size), &entities, |b, entities| { - let mut map = BTreeMap::new(); - b.iter(|| { - bench_btreemap_insert(&mut map, black_box(entities)); - }); - }); + group.bench_with_input( + BenchmarkId::new("BTreeMap", size), + &entities, + |b, entities| { + let mut map = BTreeMap::new(); + b.iter(|| { + bench_btreemap_insert(&mut map, black_box(entities)); + }); + }, + ); - group.bench_with_input(BenchmarkId::new("HashMap (std)", size), &entities, |b, entities| { - let mut map = HashMap::new(); - b.iter(|| { - bench_hashmap_insert(&mut map, black_box(entities)); - }); - }); + group.bench_with_input( + BenchmarkId::new("HashMap (std)", size), + &entities, + |b, entities| { + let mut map = HashMap::new(); + b.iter(|| { + bench_hashmap_insert(&mut map, black_box(entities)); + }); + }, + ); - group.bench_with_input(BenchmarkId::new("HashMap (hashbrown)", size), &entities, |b, entities| { - let mut map = HashbrownMap::new(); - b.iter(|| { - bench_hashbrown_insert(&mut map, black_box(entities)); - }); - }); + group.bench_with_input( + BenchmarkId::new("HashMap (hashbrown)", size), + &entities, + |b, entities| { + let mut map = HashbrownMap::new(); + b.iter(|| { + bench_hashbrown_insert(&mut map, black_box(entities)); + }); + }, + ); - group.bench_with_input(BenchmarkId::new("FxHashMap", size), &entities, |b, entities| { - let mut map = FxHashMap::default(); - b.iter(|| { - bench_fxhashmap_insert(&mut map, black_box(entities)); - }); - }); + group.bench_with_input( + BenchmarkId::new("FxHashMap", size), + &entities, + |b, entities| { + let mut map = FxHashMap::default(); + b.iter(|| { + bench_fxhashmap_insert(&mut map, black_box(entities)); + }); + }, + ); } group.finish(); @@ -180,21 +200,29 @@ fn bench_search(c: &mut Criterion) { let mut fxhashmap = FxHashMap::default(); bench_fxhashmap_insert(&mut fxhashmap, &entities); - group.bench_with_input(BenchmarkId::new("Vec (binary search)", size), &search_ids, |b, ids| { - b.iter(|| { - for &id in ids { - black_box(bench_vec_search_separated(&entity_ids, &vec, black_box(id))); - } - }); - }); + group.bench_with_input( + BenchmarkId::new("Vec (binary search)", size), + &search_ids, + |b, ids| { + b.iter(|| { + for &id in ids { + black_box(bench_vec_search_separated(&entity_ids, &vec, black_box(id))); + } + }); + }, + ); - group.bench_with_input(BenchmarkId::new("Vec (binary search) separated", size), &search_ids, |b, ids| { - b.iter(|| { - for &id in ids { - black_box(bench_vec_search(&vec, black_box(id))); - } - }); - }); + group.bench_with_input( + BenchmarkId::new("Vec (binary search) separated", size), + &search_ids, + |b, ids| { + b.iter(|| { + for &id in ids { + black_box(bench_vec_search(&vec, black_box(id))); + } + }); + }, + ); group.bench_with_input(BenchmarkId::new("BTreeMap", size), &search_ids, |b, ids| { b.iter(|| { @@ -204,29 +232,41 @@ fn bench_search(c: &mut Criterion) { }); }); - group.bench_with_input(BenchmarkId::new("HashMap (std)", size), &search_ids, |b, ids| { - b.iter(|| { - for &id in ids { - black_box(bench_hashmap_search(&hashmap, black_box(id))); - } - }); - }); + group.bench_with_input( + BenchmarkId::new("HashMap (std)", size), + &search_ids, + |b, ids| { + b.iter(|| { + for &id in ids { + black_box(bench_hashmap_search(&hashmap, black_box(id))); + } + }); + }, + ); - group.bench_with_input(BenchmarkId::new("HashMap (hashbrown)", size), &search_ids, |b, ids| { - b.iter(|| { - for &id in ids { - black_box(bench_hashbrown_search(&hashbrown, black_box(id))); - } - }); - }); + group.bench_with_input( + BenchmarkId::new("HashMap (hashbrown)", size), + &search_ids, + |b, ids| { + b.iter(|| { + for &id in ids { + black_box(bench_hashbrown_search(&hashbrown, black_box(id))); + } + }); + }, + ); - group.bench_with_input(BenchmarkId::new("FxHashMap", size), &search_ids, |b, ids| { - b.iter(|| { - for &id in ids { - black_box(bench_fxhashmap_search(&fxhashmap, black_box(id))); - } - }); - }); + group.bench_with_input( + BenchmarkId::new("FxHashMap", size), + &search_ids, + |b, ids| { + b.iter(|| { + for &id in ids { + black_box(bench_fxhashmap_search(&fxhashmap, black_box(id))); + } + }); + }, + ); } group.finish(); @@ -282,4 +322,4 @@ fn bench_memory_efficiency(c: &mut Criterion) { } criterion_group!(benches, bench_insert, bench_search, bench_memory_efficiency); -criterion_main!(benches); \ No newline at end of file +criterion_main!(benches); diff --git a/src/data/entities.rs b/src/data/entities.rs index 7251123..5fd80ae 100644 --- a/src/data/entities.rs +++ b/src/data/entities.rs @@ -1,4 +1,3 @@ -use std::cmp::Ordering; use crate::app::AppState; use crate::core::srs::{FADE_PER_DAY, MAX_SCORE, OrderMode, WordOpenMode}; use crate::data::{INVALID_ID, Id}; @@ -7,6 +6,7 @@ use chrono::{DateTime, Utc}; use log::info; use rayon::prelude::*; use rhai::{Engine, Scope}; +use std::cmp::Ordering; use std::collections::HashMap; use std::marker::PhantomData; use std::time::Instant; @@ -184,24 +184,23 @@ pub struct EntityHandle { pub(crate) id: Id, index: i32, pub(crate) _phantom: PhantomData, - } impl Clone for EntityHandle { fn clone(&self) -> Self { - EntityHandle{ + EntityHandle { id: self.id, index: self.index, _phantom: Default::default(), } } } -impl EntityHandle { +impl EntityHandle { pub fn new(id: Id, index: usize, real: bool) -> Self { Self { id, - index : index as i32 | (!real as i32) << 31, - _phantom: Default::default() + index: index as i32 | (!real as i32) << 31, + _phantom: Default::default(), } } pub fn index(&self) -> usize { @@ -236,18 +235,18 @@ impl PartialOrd for EntityHandle { impl Ord for EntityHandle { fn cmp(&self, other: &Self) -> Ordering { if self.real() && !other.real() { - return Ordering::Less + return Ordering::Less; } - - if !self.real() && other.real(){ - return Ordering::Greater + + if !self.real() && other.real() { + return Ordering::Greater; } - + self.id.cmp(&other.id) } } -pub trait EntityData{ +pub trait EntityData { fn id(&self) -> Id; fn set_id(&mut self, id: Id); } diff --git a/src/data/repositories.rs b/src/data/repositories.rs index f94ba8b..0c0e54b 100644 --- a/src/data/repositories.rs +++ b/src/data/repositories.rs @@ -98,7 +98,7 @@ where rule: Box bool + Send + Sync>, parallel: bool, _marker: PhantomData<(R, D)>, - shift_stamp: u16 + shift_stamp: u16, } impl, D: DbLayer> Query { @@ -177,8 +177,8 @@ pub trait BatchedDbLayer where T: EntityData, { - fn delete_many(&mut self, ids: &[Id], connection: &Connection) -> MaybeError; - fn add_many_entity(&mut self, entity: &mut [T], connection: &Connection) -> MaybeError; + fn delete_many_entities(&mut self, ids: &[Id], connection: &Connection) -> MaybeError; + fn add_many_entities(&mut self, entity: &mut [T], connection: &Connection) -> MaybeError; } #[derive(Clone)] @@ -405,7 +405,7 @@ impl + DbLayer> BatchedRepository }); self.data_layer - .add_many_entity(&mut temp_values, &self.connection.read())?; + .add_many_entities(&mut temp_values, &self.connection.read())?; let start = self.cache.len(); @@ -447,7 +447,7 @@ impl + DbLayer> BatchedRepository !remove }); self.data_layer - .delete_many(cached.as_slice(), &self.connection.read())?; + .delete_many_entities(cached.as_slice(), &self.connection.read())?; let len = cached.len(); cell = 0; self.cache.retain(|x| {