Skip to content

Commit

Permalink
Improve concurrent data structures
Browse files Browse the repository at this point in the history
  • Loading branch information
terrarier2111 committed Mar 4, 2024
1 parent 9f9d1b9 commit 90c3f4a
Show file tree
Hide file tree
Showing 23 changed files with 474 additions and 563 deletions.
203 changes: 110 additions & 93 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ reqwest = { version = "0.11", features = [ "blocking" ]}
glutin = "0.31"
glutin-winit = "0.4"

arc-swap = "1.7.0"

[dependencies.leafish_resources]
path = "./resources"
version = "0"
Expand Down
2 changes: 1 addition & 1 deletion blocks/src/bin/dump_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
let id = str::parse::<usize>(&args[2]).unwrap();

let id_map = VanillaIDMap::new(protocol_version);
let block = id_map.by_vanilla_id(id, Arc::new(RwLock::new(HashMap::new())));
let block = id_map.by_vanilla_id(id, &Arc::new(HashMap::new()));

println!("{:?}", block);
}
26 changes: 12 additions & 14 deletions blocks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ pub use self::material::Material;

pub use self::blocks::Block::*;
pub use self::blocks::*;
use parking_lot::RwLock;
use std::sync::Arc;

pub trait WorldAccess {
fn get_block(&self, pos: Position) -> Block;
Expand Down Expand Up @@ -51,7 +49,7 @@ impl VanillaIDMap {
pub fn by_vanilla_id(
&self,
id: usize,
modded_block_ids: Arc<RwLock<HashMap<usize, String>>>, // TODO: remove and add to constructor, but have to mutate in Server
modded_block_ids: &HashMap<usize, String>, // TODO: remove and add to constructor, but have to mutate in Server
) -> Block {
match &self.mapping {
IDMapKind::Flat(blocks) => {
Expand All @@ -63,7 +61,7 @@ impl VanillaIDMap {
block
} else {
let data = id & 0xf;
if let Some(name) = modded_block_ids.read().get(&(id >> 4)) {
if let Some(name) = modded_block_ids.get(&(id >> 4)) {
if let Some(blocks_by_data) = self.modded.get(name) {
blocks_by_data[data].unwrap_or(Block::Missing {})
} else {
Expand Down Expand Up @@ -99,13 +97,13 @@ mod tests {
fn hier_1_12_2() {
let id_map = VanillaIDMap::new(340);
assert_eq!(
id_map.by_vanilla_id(255 << 4, Arc::new(RwLock::new(HashMap::new()))),
id_map.by_vanilla_id(255 << 4, &Arc::new(HashMap::new())),
StructureBlock {
mode: StructureBlockMode::Save
}
);
assert_eq!(
id_map.by_vanilla_id((255 << 4) | 3, Arc::new(RwLock::new(HashMap::new()))),
id_map.by_vanilla_id((255 << 4) | 3, &Arc::new(HashMap::new())),
StructureBlock {
mode: StructureBlockMode::Data
}
Expand All @@ -116,13 +114,13 @@ mod tests {
fn flat_1_13_2() {
let id_map = VanillaIDMap::new(404);
assert_eq!(
id_map.by_vanilla_id(8595, Arc::new(RwLock::new(HashMap::new()))),
id_map.by_vanilla_id(8595, &Arc::new(HashMap::new())),
StructureBlock {
mode: StructureBlockMode::Save
}
);
assert_eq!(
id_map.by_vanilla_id(8598, Arc::new(RwLock::new(HashMap::new()))),
id_map.by_vanilla_id(8598, &Arc::new(HashMap::new())),
StructureBlock {
mode: StructureBlockMode::Data
}
Expand All @@ -133,11 +131,11 @@ mod tests {
fn flat_1_14_4() {
let id_map = VanillaIDMap::new(477);
assert_eq!(
id_map.by_vanilla_id(9113, Arc::new(RwLock::new(HashMap::new()))),
id_map.by_vanilla_id(9113, &Arc::new(HashMap::new())),
Conduit { waterlogged: true }
);
assert_eq!(
id_map.by_vanilla_id(9114, Arc::new(RwLock::new(HashMap::new()))),
id_map.by_vanilla_id(9114, &Arc::new(HashMap::new())),
Conduit { waterlogged: false }
);
}
Expand All @@ -146,11 +144,11 @@ mod tests {
fn flat_1_15_1() {
let id_map = VanillaIDMap::new(575);
assert_eq!(
id_map.by_vanilla_id(9113, Arc::new(RwLock::new(HashMap::new()))),
id_map.by_vanilla_id(9113, &Arc::new(HashMap::new())),
Conduit { waterlogged: true }
);
assert_eq!(
id_map.by_vanilla_id(9114, Arc::new(RwLock::new(HashMap::new()))),
id_map.by_vanilla_id(9114, &Arc::new(HashMap::new())),
Conduit { waterlogged: false }
);
}
Expand All @@ -159,7 +157,7 @@ mod tests {
fn flat_1_16() {
let id_map = VanillaIDMap::new(735);
assert_eq!(
id_map.by_vanilla_id(1048, Arc::new(RwLock::new(HashMap::new()))),
id_map.by_vanilla_id(1048, &Arc::new(HashMap::new())),
NoteBlock {
instrument: NoteBlockInstrument::Pling,
note: 24,
Expand All @@ -172,7 +170,7 @@ mod tests {
fn flat_1_16_2() {
let id_map = VanillaIDMap::new(751);
assert_eq!(
id_map.by_vanilla_id(1048, Arc::new(RwLock::new(HashMap::new()))),
id_map.by_vanilla_id(1048, &Arc::new(HashMap::new())),
NoteBlock {
instrument: NoteBlockInstrument::Pling,
note: 24,
Expand Down
31 changes: 14 additions & 17 deletions src/entity/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ use crate::shared::Position as BPosition;
use crate::types::hash::FNVHash;
use crate::types::GameMode;
use crate::world;
use arc_swap::ArcSwapOption;
use bevy_ecs::prelude::*;
use cgmath::{Decomposed, Matrix4, Point3, Quaternion, Rad, Rotation3, Vector3};
use collision::{Aabb, Aabb3};
use instant::Instant;
use parking_lot::Mutex;
use std::collections::HashMap;
use std::hash::BuildHasherDefault;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

pub fn add_systems(m: &mut Manager) {
Expand Down Expand Up @@ -113,8 +114,8 @@ pub fn create_remote(m: &mut Manager, name: &str) -> Entity {
#[derive(Component)]
pub struct PlayerModel {
model: Option<model::ModelHandle>,
skin_url: Arc<Mutex<Option<String>>>,
dirty: bool,
skin_url: ArcSwapOption<String>,
dirty: AtomicBool,
name: String,

has_head: bool,
Expand All @@ -132,8 +133,8 @@ impl PlayerModel {
pub fn new(name: &str, has_head: bool, has_name_tag: bool, first_person: bool) -> Self {
Self {
model: None,
skin_url: Arc::new(Mutex::new(None)),
dirty: false,
skin_url: ArcSwapOption::new(None),
dirty: AtomicBool::new(false),
name: name.to_owned(),

has_head,
Expand All @@ -149,13 +150,9 @@ impl PlayerModel {
}

pub fn set_skin(&mut self, skin: Option<String>) {
if *self.skin_url.lock() != skin {
if let Some(skin) = skin {
self.skin_url.lock().replace(skin);
} else {
self.skin_url.lock().take();
}
self.dirty = true;
if self.skin_url.load().as_ref().map(|skin| skin.as_ref()) != skin.as_ref() {
self.skin_url.store(skin.map(Arc::new));
self.dirty.store(true, Ordering::Release);
}
}
}
Expand All @@ -172,7 +169,7 @@ fn update_render_players(
use std::f32::consts::PI;
use std::f64::consts::PI as PI64;

if player_model.dirty {
if player_model.dirty.load(Ordering::Acquire) {
add_player(renderer.clone(), &mut player_model);
}

Expand Down Expand Up @@ -331,9 +328,9 @@ pub fn player_added(

// TODO: Setup culling
fn add_player(renderer: Arc<Renderer>, player_model: &mut PlayerModel) {
player_model.dirty = false;
player_model.dirty.store(false, Ordering::Release);

let skin = if let Some(url) = player_model.skin_url.lock().as_ref() {
let skin = if let Some(url) = player_model.skin_url.load().as_ref() {
renderer.get_skin(renderer.get_textures_ref(), url)
} else {
render::Renderer::get_texture(renderer.get_textures_ref(), "entity/steve")
Expand Down Expand Up @@ -495,11 +492,11 @@ fn add_player(renderer: Arc<Renderer>, player_model: &mut PlayerModel) {
],
renderer,
);
let skin_url = player_model.skin_url.clone();
let skin_url = player_model.skin_url.load();
model.2 = player_model.model.as_ref().map_or(
Some(Arc::new(move |renderer: Arc<Renderer>| {
let skin_url = skin_url.clone();
if let Some(url) = skin_url.lock().as_ref() {
if let Some(url) = skin_url.as_ref() {
renderer.get_textures_ref().read().release_skin(url); // TODO: Move this into the custom drop handling fn!
};
})),
Expand Down
8 changes: 4 additions & 4 deletions src/inventory/anvil_inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct AnvilInventory {
}

impl AnvilInventory {
pub fn new(renderer: Arc<Renderer>, base_slots: Arc<RwLock<SlotMapping>>, id: i32) -> Self {
pub fn new(renderer: &Arc<Renderer>, base_slots: Arc<RwLock<SlotMapping>>, id: i32) -> Self {
let mut slots = SlotMapping::new((WINDOW_WIDTH, WINDOW_HEIGHT));
slots.set_child(base_slots, (8, 84), (3..39).collect());

Expand Down Expand Up @@ -96,7 +96,7 @@ impl Inventory for AnvilInventory {

fn init(
&mut self,
renderer: Arc<Renderer>,
renderer: &Arc<Renderer>,
ui_container: &mut Container,
inventory_window: &mut InventoryWindow,
) {
Expand All @@ -107,7 +107,7 @@ impl Inventory for AnvilInventory {

let basic_elements = inventory_window.elements.get_mut(0).unwrap();
let basic_text_elements = inventory_window.text_elements.get_mut(0).unwrap();
let icon_scale = Hud::icon_scale(renderer.clone());
let icon_scale = Hud::icon_scale(renderer);

let top_left_x =
renderer.screen_data.read().center().0 as f64 - icon_scale * WINDOW_WIDTH as f64 / 2.0;
Expand Down Expand Up @@ -200,7 +200,7 @@ impl Inventory for AnvilInventory {

fn tick(
&mut self,
renderer: Arc<Renderer>,
renderer: &Arc<Renderer>,
ui_container: &mut Container,
inventory_window: &mut InventoryWindow,
) {
Expand Down
8 changes: 4 additions & 4 deletions src/inventory/beacon_inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct BeaconInfo {
}

impl BeaconInventory {
pub fn new(renderer: Arc<Renderer>, base_slots: Arc<RwLock<SlotMapping>>, id: i32) -> Self {
pub fn new(renderer: &Arc<Renderer>, base_slots: Arc<RwLock<SlotMapping>>, id: i32) -> Self {
let mut slots = SlotMapping::new((WINDOW_WIDTH, WINDOW_HEIGHT));
slots.set_child(base_slots, (36, 137), (1..37).collect());

Expand Down Expand Up @@ -103,7 +103,7 @@ impl Inventory for BeaconInventory {

fn init(
&mut self,
renderer: Arc<Renderer>,
renderer: &Arc<Renderer>,
ui_container: &mut Container,
inventory_window: &mut InventoryWindow,
) {
Expand All @@ -113,7 +113,7 @@ impl Inventory for BeaconInventory {
inventory_window.text_elements.push(vec![]);

let basic_elements = inventory_window.elements.get_mut(0).unwrap();
let icon_scale = Hud::icon_scale(renderer.clone());
let icon_scale = Hud::icon_scale(renderer);

let top_left_x =
renderer.screen_data.read().center().0 as f64 - icon_scale * WINDOW_WIDTH as f64 / 2.0;
Expand Down Expand Up @@ -245,7 +245,7 @@ impl Inventory for BeaconInventory {
// 17-21 = accepted payments
fn tick(
&mut self,
renderer: Arc<Renderer>,
renderer: &Arc<Renderer>,
ui_container: &mut Container,
inventory_window: &mut InventoryWindow,
) {
Expand Down
11 changes: 5 additions & 6 deletions src/inventory/brewing_stand_inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct BrewingStandInventory {

impl BrewingStandInventory {
pub fn new(
renderer: Arc<Renderer>,
renderer: &Arc<Renderer>,
base_slots: Arc<RwLock<SlotMapping>>,
name: String,
id: i32,
Expand Down Expand Up @@ -100,7 +100,7 @@ impl Inventory for BrewingStandInventory {

fn init(
&mut self,
renderer: Arc<Renderer>,
renderer: &Arc<Renderer>,
ui_container: &mut Container,
inventory_window: &mut InventoryWindow,
) {
Expand All @@ -112,7 +112,7 @@ impl Inventory for BrewingStandInventory {
let basic_elements = inventory_window.elements.get_mut(0).unwrap();
let basic_text_elements = inventory_window.text_elements.get_mut(0).unwrap();

let icon_scale = Hud::icon_scale(renderer.clone());
let icon_scale = Hud::icon_scale(renderer);
let top_left_x =
renderer.screen_data.read().center().0 as f64 - icon_scale * WINDOW_WIDTH as f64 / 2.0;
let top_left_y =
Expand Down Expand Up @@ -190,12 +190,11 @@ impl Inventory for BrewingStandInventory {

fn tick(
&mut self,
renderer: Arc<Renderer>,
renderer: &Arc<Renderer>,
ui_container: &mut Container,
inventory_window: &mut InventoryWindow,
) {
self.slots
.tick(renderer.clone(), ui_container, inventory_window, 1);
self.slots.tick(renderer, ui_container, inventory_window, 1);
if self.dirty {
self.dirty = false;
let icon_scale = Hud::icon_scale(renderer);
Expand Down
8 changes: 4 additions & 4 deletions src/inventory/chest_inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct ChestInventory {

impl ChestInventory {
pub fn new(
renderer: Arc<Renderer>,
renderer: &Arc<Renderer>,
base_slots: Arc<RwLock<SlotMapping>>,
rows: u8,
name: String,
Expand Down Expand Up @@ -86,7 +86,7 @@ impl Inventory for ChestInventory {

fn init(
&mut self,
renderer: Arc<Renderer>,
renderer: &Arc<Renderer>,
ui_container: &mut Container,
inventory_window: &mut InventoryWindow,
) {
Expand All @@ -98,7 +98,7 @@ impl Inventory for ChestInventory {
let basic_elements = inventory_window.elements.get_mut(0).unwrap();
let basic_text_elements = inventory_window.text_elements.get_mut(0).unwrap();

let icon_scale = Hud::icon_scale(renderer.clone()) as i32;
let icon_scale = Hud::icon_scale(renderer) as i32;
let chest_height_scaled = icon_scale * chest_height(self.rows);
let inventory_height_scaled = icon_scale * INVENTORY_HEIGHT;
let total_height_scaled = chest_height_scaled + inventory_height_scaled;
Expand Down Expand Up @@ -189,7 +189,7 @@ impl Inventory for ChestInventory {

fn tick(
&mut self,
renderer: Arc<Renderer>,
renderer: &Arc<Renderer>,
ui_container: &mut Container,
inventory_window: &mut InventoryWindow,
) {
Expand Down
8 changes: 4 additions & 4 deletions src/inventory/crafting_table_inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct CraftingTableInventory {
}

impl CraftingTableInventory {
pub fn new(renderer: Arc<Renderer>, base_slots: Arc<RwLock<SlotMapping>>, id: i32) -> Self {
pub fn new(renderer: &Arc<Renderer>, base_slots: Arc<RwLock<SlotMapping>>, id: i32) -> Self {
let mut slots = SlotMapping::new((WINDOW_WIDTH, WINDOW_HEIGHT));
slots.set_child(base_slots, (8, 84), (10..46).collect());

Expand Down Expand Up @@ -77,7 +77,7 @@ impl Inventory for CraftingTableInventory {

fn init(
&mut self,
renderer: Arc<Renderer>,
renderer: &Arc<Renderer>,
ui_container: &mut Container,
inventory_window: &mut InventoryWindow,
) {
Expand All @@ -90,7 +90,7 @@ impl Inventory for CraftingTableInventory {
let basic_text_elements = inventory_window.text_elements.get_mut(0).unwrap();

let center = renderer.screen_data.read().center();
let icon_scale = Hud::icon_scale(renderer.clone());
let icon_scale = Hud::icon_scale(renderer);

// Crafting table texture
basic_elements.push(
Expand Down Expand Up @@ -143,7 +143,7 @@ impl Inventory for CraftingTableInventory {

fn tick(
&mut self,
renderer: Arc<Renderer>,
renderer: &Arc<Renderer>,
ui_container: &mut Container,
inventory_window: &mut InventoryWindow,
) {
Expand Down
Loading

0 comments on commit 90c3f4a

Please sign in to comment.