rt-tiles: cache thread-local de tuile dans hauteur_colonne (supprime la contention du verrou)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Cette révision appartient à :
1 Parent
1beb7b993c
révision
d3a85357e7
1 fichier modifié
+20
-1
@@ -4,8 +4,17 @@
|
|||||||
use rt_core::{TileId, WorldOrigin};
|
use rt_core::{TileId, WorldOrigin};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::sync::atomic::{AtomicU64, Ordering};
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
|
|
||||||
|
static STORE_IDS: AtomicU64 = AtomicU64::new(1);
|
||||||
|
|
||||||
|
thread_local! {
|
||||||
|
/// Dernière tuile résolue par ce thread : (id du store, tuile). Évite le verrou par colonne.
|
||||||
|
static DERNIERE: RefCell<Option<(u64, TileId, Arc<Tile>)>> = const { RefCell::new(None) };
|
||||||
|
}
|
||||||
|
|
||||||
pub const BORDER: i32 = 2;
|
pub const BORDER: i32 = 2;
|
||||||
pub const SIDE: usize = 516;
|
pub const SIDE: usize = 516;
|
||||||
pub const N: usize = SIDE * SIDE;
|
pub const N: usize = SIDE * SIDE;
|
||||||
@@ -179,13 +188,14 @@ impl Tile {
|
|||||||
/// Cache de tuiles mmapées, partageable entre threads.
|
/// Cache de tuiles mmapées, partageable entre threads.
|
||||||
/// (Le LRU borné en octets et le prefetch rayon sont prévus au prototype 10.)
|
/// (Le LRU borné en octets et le prefetch rayon sont prévus au prototype 10.)
|
||||||
pub struct TileStore {
|
pub struct TileStore {
|
||||||
|
uid: u64,
|
||||||
dir: PathBuf,
|
dir: PathBuf,
|
||||||
tiles: RwLock<HashMap<TileId, Option<Arc<Tile>>>>,
|
tiles: RwLock<HashMap<TileId, Option<Arc<Tile>>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TileStore {
|
impl TileStore {
|
||||||
pub fn new(dir: PathBuf) -> Self {
|
pub fn new(dir: PathBuf) -> Self {
|
||||||
TileStore { dir, tiles: RwLock::new(HashMap::new()) }
|
TileStore { uid: STORE_IDS.fetch_add(1, Ordering::Relaxed), dir, tiles: RwLock::new(HashMap::new()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(&self, id: TileId) -> Result<Arc<Tile>, TileError> {
|
pub fn get(&self, id: TileId) -> Result<Arc<Tile>, TileError> {
|
||||||
@@ -204,7 +214,16 @@ impl TileStore {
|
|||||||
/// y du bloc solide le plus haut de la colonne (x, z), ou None hors données.
|
/// y du bloc solide le plus haut de la colonne (x, z), ou None hors données.
|
||||||
pub fn hauteur_colonne(&self, x: i32, z: i32) -> Option<i32> {
|
pub fn hauteur_colonne(&self, x: i32, z: i32) -> Option<i32> {
|
||||||
let id = TileId::of_block(x, z);
|
let id = TileId::of_block(x, z);
|
||||||
|
let t = DERNIERE.with(|c| {
|
||||||
|
if let Some((u, i, t)) = &*c.borrow() {
|
||||||
|
if *u == self.uid && *i == id {
|
||||||
|
return Some(t.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
let t = self.get(id).ok()?;
|
let t = self.get(id).ok()?;
|
||||||
|
*c.borrow_mut() = Some((self.uid, id, t.clone()));
|
||||||
|
Some(t)
|
||||||
|
})?;
|
||||||
let s = t.sample(x - id.tx * TileId::SIZE, z - id.tz * TileId::SIZE);
|
let s = t.sample(x - id.tx * TileId::SIZE, z - id.tz * TileId::SIZE);
|
||||||
(s.ground_dm != 0).then(|| s.ground_y(&t.origin()))
|
(s.ground_dm != 0).then(|| s.ground_y(&t.origin()))
|
||||||
}
|
}
|
||||||
|
|||||||
Référencer dans un nouveau ticket
Bloquer un utilisateur