Browse Source

Add epaint::util::hash function for hashing a value

pull/752/head
Emil Ernerfeldt 3 years ago
parent
commit
ba0e3780a1
  1. 7
      egui/src/any/serializable/type_id.rs
  2. 11
      egui/src/util/cache.rs
  3. 1
      epaint/src/lib.rs
  4. 17
      epaint/src/text/fonts.rs
  5. 12
      epaint/src/util.rs

7
egui/src/any/serializable/type_id.rs

@ -13,11 +13,6 @@ impl TypeId {
impl From<std::any::TypeId> for TypeId { impl From<std::any::TypeId> for TypeId {
fn from(id: std::any::TypeId) -> Self { fn from(id: std::any::TypeId) -> Self {
use std::collections::hash_map::DefaultHasher; Self(epaint::util::hash(id))
use std::hash::{Hash, Hasher};
let mut hasher = DefaultHasher::new();
id.hash(&mut hasher);
Self(hasher.finish())
} }
} }

11
egui/src/util/cache.rs

@ -1,4 +1,4 @@
use std::hash::{Hash, Hasher}; use epaint::util::hash;
const SIZE: usize = 1024; // must be small for web/WASM build (for unknown reason) const SIZE: usize = 1024; // must be small for web/WASM build (for unknown reason)
@ -24,7 +24,7 @@ impl<K, V> std::fmt::Debug for Cache<K, V> {
impl<K, V> Cache<K, V> impl<K, V> Cache<K, V>
where where
K: Hash + PartialEq, K: std::hash::Hash + PartialEq,
{ {
pub fn get(&self, key: &K) -> Option<&V> { pub fn get(&self, key: &K) -> Option<&V> {
let bucket = (hash(key) % (SIZE as u64)) as usize; let bucket = (hash(key) % (SIZE as u64)) as usize;
@ -39,10 +39,3 @@ where
self.0[bucket] = Some((key, value)); self.0[bucket] = Some((key, value));
} }
} }
fn hash(value: impl Hash) -> u64 {
use std::collections::hash_map::DefaultHasher;
let mut hasher = DefaultHasher::default();
value.hash(&mut hasher);
hasher.finish()
}

1
epaint/src/lib.rs

@ -82,6 +82,7 @@ mod stroke;
pub mod tessellator; pub mod tessellator;
pub mod text; pub mod text;
mod texture_atlas; mod texture_atlas;
pub mod util;
pub use { pub use {
color::{Color32, Rgba}, color::{Color32, Rgba},

17
epaint/src/text/fonts.rs

@ -1,8 +1,4 @@
use std::{ use std::{collections::BTreeMap, sync::Arc};
collections::BTreeMap,
hash::{Hash, Hasher},
sync::Arc,
};
use crate::{ use crate::{
mutex::Mutex, mutex::Mutex,
@ -261,10 +257,7 @@ impl Fonts {
let mut atlas = atlas.lock(); let mut atlas = atlas.lock();
let texture = atlas.texture_mut(); let texture = atlas.texture_mut();
// Make sure we seed the texture version with something unique based on the default characters: // Make sure we seed the texture version with something unique based on the default characters:
use std::collections::hash_map::DefaultHasher; texture.version = crate::util::hash(&texture.pixels);
let mut hasher = DefaultHasher::default();
texture.pixels.hash(&mut hasher);
texture.version = hasher.finish();
} }
Self { Self {
@ -412,11 +405,7 @@ struct GalleyCache {
impl GalleyCache { impl GalleyCache {
fn layout(&mut self, fonts: &Fonts, job: LayoutJob) -> Arc<Galley> { fn layout(&mut self, fonts: &Fonts, job: LayoutJob) -> Arc<Galley> {
let hash = { let hash = crate::util::hash_with(&job, ahash::AHasher::new_with_keys(123, 456)); // TODO: even faster hasher?
let mut hasher = ahash::AHasher::new_with_keys(123, 456); // TODO: even faster hasher?
job.hash(&mut hasher);
hasher.finish()
};
match self.cache.entry(hash) { match self.cache.entry(hash) {
std::collections::hash_map::Entry::Occupied(entry) => { std::collections::hash_map::Entry::Occupied(entry) => {

12
epaint/src/util.rs

@ -0,0 +1,12 @@
/// Hash the given value with a predictable hasher.
#[inline]
pub fn hash(value: impl std::hash::Hash) -> u64 {
hash_with(value, std::collections::hash_map::DefaultHasher::default())
}
/// Hash the given value with the given hasher.
#[inline]
pub fn hash_with(value: impl std::hash::Hash, mut hasher: impl std::hash::Hasher) -> u64 {
value.hash(&mut hasher);
hasher.finish()
}
Loading…
Cancel
Save