Rename methods

This commit is contained in:
micialware committed 2026-08-30 19:22:28 +03:00
1 parent b006ece0c1
commit 3ac7cd068b
3 files changed
+76 -37

No files matched your search

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