From: Joshua Simmons Date: Mon, 14 Nov 2022 20:31:16 +0000 (+0100) Subject: Sprinkle some inlines and unrolling for debug perf X-Git-Url: https://git.nega.tv//gitweb.cgi?a=commitdiff_plain;h=ba7cdefec1c26d6b02838f497c4911cf77d1466f;p=josh%2Fnarcissus Sprinkle some inlines and unrolling for debug perf --- diff --git a/narcissus-core/src/pool.rs b/narcissus-core/src/pool.rs index a1b86be..ada1b7d 100644 --- a/narcissus-core/src/pool.rs +++ b/narcissus-core/src/pool.rs @@ -50,6 +50,7 @@ impl Handle { /// # Panics /// /// Panics if the generation counter is even, as that would reference an empty slot. + #[inline(always)] fn encode(encode_multiplier: u32, generation: u32, slot_index: SlotIndex) -> Self { assert!(generation & 1 == 1); @@ -125,6 +126,7 @@ struct Slot { } impl Slot { + #[inline(always)] const fn new() -> Self { Self { // Clear the generation counter, but leave the index bits set. @@ -133,16 +135,19 @@ impl Slot { } /// Extract the current generation counter from this slot. + #[inline(always)] fn generation(&self) -> u32 { (self.value_index_and_gen >> GEN_SHIFT) & GEN_MASK } /// Extract the current value index from this slot. + #[inline(always)] fn value_index(&self) -> ValueIndex { ValueIndex((self.value_index_and_gen >> IDX_SHIFT) & IDX_MASK) } /// Updates the slot's value index without modifying the generation. + #[inline(always)] fn update_value_index(&mut self, value_index: ValueIndex) { debug_assert!(value_index.0 & IDX_MASK == value_index.0); self.value_index_and_gen = @@ -150,6 +155,7 @@ impl Slot { } /// Sets the slot's value index, incrementing the generation counter. + #[inline(always)] fn set_value_index(&mut self, value_index: ValueIndex) { let new_generation = self.generation().wrapping_add(1); self.value_index_and_gen = @@ -157,6 +163,7 @@ impl Slot { } /// Clears the slot's value index, incrementing the generation counter. + #[inline(always)] fn clear_value_index(&mut self) { // Since we're clearing we need to reset the generation to one referencing an empty slot. But we still want to // invalidate old handles. @@ -270,6 +277,7 @@ impl Slots { Self { len: 0, ptr } } + #[inline(always)] fn get(&self, slot_index: SlotIndex) -> Option<&Slot> { let index = slot_index.0 as usize; if index < self.len { @@ -279,6 +287,7 @@ impl Slots { } } + #[inline(always)] fn get_mut(&mut self, slot_index: SlotIndex) -> Option<&mut Slot> { let index = slot_index.0 as usize; if index < self.len { @@ -345,6 +354,7 @@ impl Values { } /// Update the lookup table for the given `ValueIndex` with a new `SlotIndex` + #[inline(always)] fn set_slot(&mut self, value_index: ValueIndex, slot_index: SlotIndex) { let value_index = value_index.0 as usize; assert!(value_index < self.len); @@ -357,6 +367,7 @@ impl Values { } /// Retreive the `SlotIndex` corresponding to the given `ValueIndex` from the lookup table. + #[inline(always)] fn get_slot(&mut self, value_index: ValueIndex) -> SlotIndex { let value_index = value_index.0 as usize; assert!(value_index < self.len); @@ -365,6 +376,7 @@ impl Values { } /// Push a new value into the values storage. Returns the index of the added value. + #[inline(always)] fn push(&mut self, value: T) -> ValueIndex { if self.len == self.cap { self.grow(); @@ -381,6 +393,7 @@ impl Values { /// the lookup tables for the moved element. /// /// Returns the removed value. + #[inline(always)] fn swap_remove(&mut self, value_index: ValueIndex, slots: &mut Slots) -> T { let last_value_index = ValueIndex((self.len - 1) as u32); @@ -414,6 +427,7 @@ impl Values { /// Retreive a reference to the value at `value_index` /// Panics if `value_index` is out of bounds + #[inline(always)] fn get(&self, value_index: ValueIndex) -> &T { let value_index = value_index.0 as usize; assert!(value_index < self.len); @@ -423,6 +437,7 @@ impl Values { /// Retreive a mutable reference to the value at `value_index` /// Panics if `value_index` is out of bounds + #[inline(always)] fn get_mut(&mut self, value_index: ValueIndex) -> &mut T { let value_index = value_index.0 as usize; assert!(value_index < self.len); diff --git a/narcissus-maths/src/mat3.rs b/narcissus-maths/src/mat3.rs index b0db8b4..3ef28c8 100644 --- a/narcissus-maths/src/mat3.rs +++ b/narcissus-maths/src/mat3.rs @@ -167,15 +167,21 @@ impl std::ops::Mul for Mat3 { fn mul(self, rhs: Self) -> Self::Output { let mut result = Mat3::IDENTITY; { - let result = result.as_rows_mut(); let lhs = self.as_rows(); let rhs = rhs.as_rows(); - for i in 0..3 { - for j in 0..3 { - result[i][j] = - lhs[i][0] * rhs[0][j] + lhs[i][1] * rhs[1][j] + lhs[i][2] * rhs[2][j]; - } - } + let result = result.as_rows_mut(); + + result[0][0] = lhs[0][0] * rhs[0][0] + lhs[0][1] * rhs[1][0] + lhs[0][2] * rhs[2][0]; + result[0][1] = lhs[0][0] * rhs[0][1] + lhs[0][1] * rhs[1][1] + lhs[0][2] * rhs[2][1]; + result[0][2] = lhs[0][0] * rhs[0][2] + lhs[0][1] * rhs[1][2] + lhs[0][2] * rhs[2][2]; + + result[1][0] = lhs[1][0] * rhs[0][0] + lhs[1][1] * rhs[1][0] + lhs[1][2] * rhs[2][0]; + result[1][1] = lhs[1][0] * rhs[0][1] + lhs[1][1] * rhs[1][1] + lhs[1][2] * rhs[2][1]; + result[1][2] = lhs[1][0] * rhs[0][2] + lhs[1][1] * rhs[1][2] + lhs[1][2] * rhs[2][2]; + + result[2][0] = lhs[2][0] * rhs[0][0] + lhs[2][1] * rhs[1][0] + lhs[2][2] * rhs[2][0]; + result[2][1] = lhs[2][0] * rhs[0][1] + lhs[2][1] * rhs[1][1] + lhs[2][2] * rhs[2][1]; + result[2][2] = lhs[2][0] * rhs[0][2] + lhs[2][1] * rhs[1][2] + lhs[2][2] * rhs[2][2]; } result }