Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace hash maps with fast hash maps #2150

Merged
merged 3 commits into from
Jun 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/backend/dx11/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ impl hal::PhysicalDevice<Backend> for PhysicalDevice {
context: device.context.clone(),
};
group.add_queue(queue);
(QueueFamilyId(0), group)
group
})
.collect()
);
Expand Down
4 changes: 2 additions & 2 deletions src/backend/dx12/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

use hal::{buffer, command as com, format, image, memory, pass, pso, query};
use hal::{DrawCount, IndexCount, IndexType, InstanceCount, VertexCount, VertexOffset, WorkGroupCount};
use hal::backend::FastHashMap;
use hal::format::Aspects;
use hal::range::RangeArg;

use std::{cmp, iter, mem, ptr};
use std::borrow::Borrow;
use std::collections::HashMap;
use std::ops::Range;
use std::sync::Arc;

Expand Down Expand Up @@ -1448,7 +1448,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
viewport: d3d12::D3D12_VIEWPORT,
data: internal::BlitData,
};
let mut instances = HashMap::<internal::BlitKey, Vec<Instance>>::new();
let mut instances = FastHashMap::<internal::BlitKey, Vec<Instance>>::default();
let mut barriers = Vec::new();

for region in regions {
Expand Down
6 changes: 3 additions & 3 deletions src/backend/dx12/src/internal.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use hal::backend::FastHashMap;
use hal::pso;
use spirv_cross::hlsl;
use std::{mem, ptr};
use std::collections::HashMap;
use std::sync::Mutex;

use d3d12;
Expand All @@ -28,7 +28,7 @@ pub struct BlitData {
}

pub type BlitKey = (dxgiformat::DXGI_FORMAT, d3d12::D3D12_FILTER);
type BlitMap = HashMap<BlitKey, BlitPipe>;
type BlitMap = FastHashMap<BlitKey, BlitPipe>;

pub(crate) struct ServicePipes {
pub(crate) device: ComPtr<d3d12::ID3D12Device>,
Expand All @@ -39,7 +39,7 @@ impl ServicePipes {
pub fn new(device: ComPtr<d3d12::ID3D12Device>) -> Self {
ServicePipes {
device,
blits_2d_color: Mutex::new(HashMap::new()),
blits_2d_color: Mutex::new(FastHashMap::default()),
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/backend/dx12/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mod root_constants;
mod window;

use hal::{error, format as f, image, memory, Features, FrameImage, Limits, QueueType};
use hal::queue::{QueueFamily as HalQueueFamily, QueueFamilyId, Queues};
use hal::queue::{QueueFamilyId, Queues};
use descriptors_cpu::DescriptorCpuPool;

use winapi::shared::{dxgi, dxgi1_2, dxgi1_3, dxgi1_4, winerror};
Expand Down Expand Up @@ -309,7 +309,7 @@ impl hal::PhysicalDevice<Backend> for PhysicalDevice {
}
}

(family.id(), group)
group
})
.collect();

Expand Down
4 changes: 2 additions & 2 deletions src/backend/gl/src/device.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::borrow::Borrow;
use std::cell::Cell;
use std::collections::HashMap;
use std::iter::repeat;
use std::ops::Range;
use std::{ptr, mem, slice};
Expand All @@ -10,6 +9,7 @@ use gl;
use gl::types::{GLint, GLenum, GLfloat};

use hal::{self as c, device as d, error, image as i, memory, pass, pso, buffer, mapping, query, window};
use hal::backend::FastHashMap;
use hal::format::{ChannelType, Format, Swizzle};
use hal::pool::CommandPoolCreateFlags;
use hal::queue::QueueFamilyId;
Expand Down Expand Up @@ -325,7 +325,7 @@ impl d::Device<B> for Device {
let limits = self.share.limits.into();
let memory = if flags.contains(CommandPoolCreateFlags::RESET_INDIVIDUAL) {
BufferMemory::Individual {
storage: HashMap::new(),
storage: FastHashMap::default(),
next_buffer_id: 0,
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/backend/gl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ impl hal::PhysicalDevice<Backend> for PhysicalDevice {
let mut family = hal::backend::RawQueueGroup::new(proto_family.clone());
let queue = queue::CommandQueue::new(&self.0, vao);
family.add_queue(queue);
(QueueFamilyId(0), family)
family
})
.collect()),
})
Expand Down
4 changes: 2 additions & 2 deletions src/backend/gl/src/pool.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use hal::{self, pool};
use hal::backend::FastHashMap;
use command::{self, Command, RawCommandBuffer};
use native as n;
use Backend;

use std::collections::HashMap;
use std::sync::{Arc, Mutex};


Expand Down Expand Up @@ -47,7 +47,7 @@ pub enum BufferMemory {
// Storing the memory for each command buffer separately to allow individual
// command buffer resets.
Individual {
storage: HashMap<u64, OwnedBuffer>,
storage: FastHashMap<u64, OwnedBuffer>,
next_buffer_id: u64,
},
}
Expand Down
1 change: 1 addition & 0 deletions src/backend/metal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ core-graphics = "0.14"
io-surface = "0.10"
smallvec = "0.6"
spirv_cross = "0.9.2"
fxhash = "0.2.1"
22 changes: 16 additions & 6 deletions src/backend/metal/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use internal::{BlitVertex, Channel, ClearKey, ClearVertex};

use std::borrow::Borrow;
use std::cell::RefCell;
use std::collections::HashMap;
use std::ops::{Deref, Range};
use std::sync::{Arc, Mutex};
use std::{iter, mem};
use std::slice;

use hal::{buffer, command as com, error, memory, pool, pso};
use hal::{DrawCount, FrameImage, VertexCount, VertexOffset, InstanceCount, IndexCount, WorkGroupCount};
use hal::backend::FastHashMap;
use hal::format::{Aspects, Format, FormatDesc};
use hal::image::{Extent, Filter, Layout, SubresourceRange};
use hal::pass::{AttachmentLoadOp, AttachmentOps};
Expand Down Expand Up @@ -1883,7 +1883,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
T: IntoIterator,
T::Item: Borrow<com::ImageBlit>
{
let mut vertices = HashMap::new(); // a list of vertices per mipmap
let mut vertices = FastHashMap::default(); // a list of vertices per mipmap
let mut frame = None;
let dst_texture = match dst.root {
native::ImageRoot::Texture(ref tex) => Some(tex.as_ref()),
Expand Down Expand Up @@ -2396,7 +2396,12 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
native::DescriptorSet::Emulated(ref desc_inner) => {
use native::DescriptorSetBinding::*;
let set = desc_inner.lock().unwrap();
for (&binding, values) in set.bindings.iter() {
let bindings = set.bindings
.iter()
.enumerate()
.filter_map(|(binding, values)| values.as_ref().map(|v| (binding as u32, v)));

for (binding, values) in bindings {
let desc_layout = set.layout.iter().find(|x| x.binding == binding).unwrap();

let mut bind_stages = SmallVec::<[_; 2]>::new();
Expand All @@ -2417,7 +2422,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
bind_stages.push((pso::Stage::Fragment, loc, &mut self.state.resources_fs));
}

match *values {
match values {
Sampler(ref samplers) => {
for &mut (stage, ref loc, ref mut resources) in &mut bind_stages {
let start = layout.res_overrides[loc].sampler_id as usize;
Expand Down Expand Up @@ -2576,15 +2581,20 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
native::DescriptorSet::Emulated(ref desc_inner) => {
use native::DescriptorSetBinding::*;
let set = desc_inner.lock().unwrap();
for (&binding, values) in set.bindings.iter() {
let bindings = set.bindings
.iter()
.enumerate()
.filter_map(|(binding, values)| values.as_ref().map(|v| (binding as u32, v)));

for (binding, values) in bindings {
let desc_layout = set.layout.iter().find(|x| x.binding == binding).unwrap();

if desc_layout.stage_flags.contains(pso::ShaderStageFlags::COMPUTE) {
let res = &layout.res_overrides[&msl::ResourceBindingLocation {
binding: binding as _,
.. location_cs
}];
match *values {
match values {
Sampler(ref samplers) => {
let start = res.sampler_id as usize;
resources.add_samplers(start, samplers.as_slice());
Expand Down
20 changes: 9 additions & 11 deletions src/backend/metal/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ use std::sync::{Arc, Condvar, Mutex};
use std::{cmp, mem, slice, time};

use hal::{self, error, image, pass, format, mapping, memory, buffer, pso, query, window};
use hal::backend::FastHashMap;
use hal::device::{BindError, OutOfMemory, FramebufferError, ShaderError};
use hal::memory::Properties;
use hal::pool::CommandPoolCreateFlags;
use hal::queue::{QueueFamily as HalQueueFamily, QueueFamilyId, Queues};
use hal::queue::{QueueFamilyId, Queues};
use hal::range::RangeArg;

use cocoa::foundation::{NSRange, NSUInteger};
Expand Down Expand Up @@ -316,7 +317,6 @@ impl hal::PhysicalDevice<Backend> for PhysicalDevice {
assert_eq!(families.len(), 1);
assert_eq!(families[0].1.len(), 1);
let family = *families[0].0;
let id = family.id();

if cfg!(feature = "auto-capture") {
let device = self.shared.device.lock().unwrap();
Expand All @@ -336,12 +336,9 @@ impl hal::PhysicalDevice<Backend> for PhysicalDevice {
memory_types: self.memory_types,
};

let mut queues = HashMap::new();
queues.insert(id, queue_group);

Ok(hal::Gpu {
device,
queues: Queues::new(queues),
queues: Queues::new(vec![queue_group]),
})
}

Expand Down Expand Up @@ -493,7 +490,7 @@ impl Device {
{
Ok(library) => Ok(n::ShaderModule::Compiled {
library,
entry_point_map: HashMap::new(),
entry_point_map: FastHashMap::default(),
}),
Err(err) => Err(ShaderError::CompilationFailed(err.into())),
}
Expand All @@ -504,7 +501,7 @@ impl Device {
raw_data: &[u8],
primitive_class: MTLPrimitiveTopologyClass,
overrides: &HashMap<msl::ResourceBindingLocation, msl::ResourceBinding>,
) -> Result<(metal::Library, HashMap<String, spirv::EntryPoint>), ShaderError> {
) -> Result<(metal::Library, FastHashMap<String, spirv::EntryPoint>), ShaderError> {
// spec requires "codeSize must be a multiple of 4"
assert_eq!(raw_data.len() & 3, 0);

Expand Down Expand Up @@ -554,7 +551,7 @@ impl Device {
ShaderError::CompilationFailed(msg)
})?;

let mut entry_point_map = HashMap::new();
let mut entry_point_map = FastHashMap::default();
for entry_point in entry_points {
info!("Entry point {:?}", entry_point);
let cleansed = ast.get_cleansed_entry_point_name(&entry_point.name, entry_point.execution_model)
Expand Down Expand Up @@ -960,7 +957,7 @@ impl hal::Device<Backend> for Device {

// Vertex buffers
let vertex_descriptor = metal::VertexDescriptor::new();
let mut vertex_buffer_map = n::VertexBufferMap::new();
let mut vertex_buffer_map = n::VertexBufferMap::default();
let mut next_buffer_index = pipeline_layout.attribute_buffer_index;
trace!("Vertex attribute remapping started");

Expand Down Expand Up @@ -1442,7 +1439,8 @@ impl hal::Device<Backend> for Device {
array_offset = 0;
binding += 1;
}
match (descriptor.borrow(), set.bindings.get_mut(&binding).unwrap()) {

match (descriptor.borrow(), set.bindings[binding as usize].as_mut().unwrap()) {
(&pso::Descriptor::Sampler(sampler), &mut n::DescriptorSetBinding::Sampler(ref mut vec)) => {
vec[array_offset] = Some(sampler.0.clone());
}
Expand Down
11 changes: 5 additions & 6 deletions src/backend/metal/src/internal.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use metal;
use hal::backend::FastHashMap;
use hal::command::ClearColorRaw;
use hal::format::{Aspects, ChannelType};
use hal::image::Filter;

use std::mem;
use std::collections::HashMap;
use std::path::Path;
use std::sync::Mutex;


#[derive(Debug)]
pub struct ClearVertex {
pub pos: [f32; 4],
Expand Down Expand Up @@ -88,8 +87,8 @@ pub struct ServicePipes {
ds_write_depth_state: metal::DepthStencilState,
ds_write_stencil_state: metal::DepthStencilState,
ds_write_all_state: metal::DepthStencilState,
clears: HashMap<ClearKey, metal::RenderPipelineState>,
blits: HashMap<BlitKey, metal::RenderPipelineState>,
clears: FastHashMap<ClearKey, metal::RenderPipelineState>,
blits: FastHashMap<BlitKey, metal::RenderPipelineState>,
copy_buffer: metal::ComputePipelineState,
fill_buffer: metal::ComputePipelineState,
}
Expand Down Expand Up @@ -124,8 +123,8 @@ impl ServicePipes {
let fill_buffer = Self::create_fill_buffer(&library, device);

ServicePipes {
clears: HashMap::new(),
blits: HashMap::new(),
clears: FastHashMap::default(),
blits: FastHashMap::default(),
sampler_nearest,
sampler_linear,
ds_write_depth_state,
Expand Down
Loading