Switch to ahash for some free performance

This commit is contained in:
Sijmen 2021-05-30 03:48:43 +02:00
parent ea31c2d4dd
commit 1bda22acbf
Signed by: vijfhoek
GPG key ID: 82D05C89B28B0DAE
4 changed files with 53 additions and 10 deletions

43
Cargo.lock generated
View file

@ -18,6 +18,17 @@ version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "739f4a8db6605981345c5654f3a85b056ce52f37a39d34da03f25bf2151ea16e"
[[package]]
name = "ahash"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43bb833f0bf979d8475d38fbf09ed3b8a55e1885fe93ad3f93239fc6a4f17b98"
dependencies = [
"getrandom",
"once_cell",
"version_check",
]
[[package]]
name = "aho-corasick"
version = "0.7.18"
@ -605,6 +616,17 @@ dependencies = [
"byteorder",
]
[[package]]
name = "getrandom"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753"
dependencies = [
"cfg-if 1.0.0",
"libc",
"wasi",
]
[[package]]
name = "gfx-auxil"
version = "0.9.0"
@ -818,7 +840,7 @@ version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04"
dependencies = [
"ahash",
"ahash 0.4.7",
]
[[package]]
@ -1229,6 +1251,12 @@ dependencies = [
"cc",
]
[[package]]
name = "once_cell"
version = "1.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3"
[[package]]
name = "parking_lot"
version = "0.11.1"
@ -1415,6 +1443,7 @@ checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
name = "rustwgpu"
version = "0.1.0"
dependencies = [
"ahash 0.7.4",
"anyhow",
"bytemuck",
"cgmath",
@ -1576,6 +1605,18 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "version_check"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
[[package]]
name = "wasm-bindgen"
version = "0.2.74"

View file

@ -7,6 +7,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
ahash = "0.7.4"
anyhow = "1.0.40"
bytemuck = { version = "1.5.1", features = ["derive"] }
cgmath = "0.18.0"

View file

@ -1,6 +1,6 @@
use crate::instance::Instance;
use ahash::AHashMap;
use cgmath::{InnerSpace, Vector3};
use std::collections::HashMap;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BlockType {
@ -22,7 +22,7 @@ pub struct Chunk {
impl Chunk {
pub fn to_instances(&self) -> Vec<(BlockType, Vec<Instance>)> {
let mut map: HashMap<BlockType, Vec<Instance>> = HashMap::new();
let mut map: AHashMap<BlockType, Vec<Instance>> = AHashMap::new();
for (y, y_blocks) in self.blocks.iter().enumerate() {
for (z, z_blocks) in y_blocks.iter().enumerate() {

View file

@ -1,5 +1,6 @@
use std::{mem::size_of, time::Instant};
use ahash::AHashMap;
use wgpu::{
util::{BufferInitDescriptor, DeviceExt},
BufferAddress, BufferDescriptor,
@ -22,13 +23,13 @@ pub struct WorldState {
pub uniforms: Uniforms,
pub uniform_buffer: wgpu::Buffer,
pub uniform_bind_group: wgpu::BindGroup,
pub texture_bind_groups: HashMap<BlockType, wgpu::BindGroup>,
pub texture_bind_groups: AHashMap<BlockType, wgpu::BindGroup>,
pub camera: Camera,
pub projection: Projection,
pub instance_lists: Vec<(BlockType, Vec<Instance>)>,
pub vertex_buffer: wgpu::Buffer,
pub index_buffer: wgpu::Buffer,
pub instance_buffers: HashMap<BlockType, wgpu::Buffer>,
pub instance_buffers: AHashMap<BlockType, wgpu::Buffer>,
pub depth_texture: Texture,
pub light_bind_group: wgpu::BindGroup,
pub chunk: Chunk,
@ -38,7 +39,7 @@ impl WorldState {
fn create_textures(
render_device: &wgpu::Device,
render_queue: &wgpu::Queue,
) -> (wgpu::BindGroupLayout, HashMap<BlockType, wgpu::BindGroup>) {
) -> (wgpu::BindGroupLayout, AHashMap<BlockType, wgpu::BindGroup>) {
let dirt_texture = Texture::from_bytes(
render_device,
render_queue,
@ -83,7 +84,7 @@ impl WorldState {
],
});
let bind_groups: HashMap<BlockType, wgpu::BindGroup> = [
let bind_groups: AHashMap<BlockType, wgpu::BindGroup> = [
(BlockType::Dirt, dirt_texture),
(BlockType::Cobblestone, cobblestone_texture),
]
@ -297,7 +298,7 @@ impl WorldState {
chunk: &Chunk,
) -> (
Vec<(BlockType, Vec<Instance>)>,
HashMap<BlockType, wgpu::Buffer>,
AHashMap<BlockType, wgpu::Buffer>,
) {
let instance_lists = chunk.to_instances();
@ -305,9 +306,9 @@ impl WorldState {
.iter()
.map(|(block_type, _)| {
let buffer = render_device.create_buffer(&BufferDescriptor {
label: Some("instance_buffer"),
label: Some("instance_buffer"),
size: (size_of::<Instance>() * 16 * 16 * 16) as BufferAddress,
usage: wgpu::BufferUsage::VERTEX | wgpu::BufferUsage::COPY_DST,
usage: wgpu::BufferUsage::VERTEX | wgpu::BufferUsage::COPY_DST,
mapped_at_creation: false,
});