Pārlūkot izejas kodu

Subtile optimization for Chunk::new

They now use Vec::extend_from_slice rather than iterating a range of 0..1024 (This should be faster, or at least clearer how we do Chunk::new)
david 1 mēnesi atpakaļ
vecāks
revīzija
f7de35ef44
1 mainītis faili ar 3 papildinājumiem un 6 dzēšanām
  1. 3 6
      src/chunk.rs

+ 3 - 6
src/chunk.rs

@@ -52,16 +52,12 @@ impl IndexMut<&Point<u64>> for Chunk {
 impl Chunk {
     pub fn new() -> Self {
         let mut s = Self { tiles: Vec::with_capacity(1024) };
-        for _ in 0..1024 {
-            s.tiles.push(0);
-        }
+        s.tiles.extend_from_slice(&[0u64; 1024]);
         s
     }
     pub fn new_filled(value: u64) -> Self {
         let mut s = Self { tiles: Vec::with_capacity(1024) };
-        for _ in 0..1024 {
-            s.tiles.push(value);
-        }
+        s.tiles.extend_from_slice(&[value; 1024]);
         s
     }
     pub fn get(&self, pos: &Point<u64>) -> Option<&u64> {
@@ -109,6 +105,7 @@ impl Chunk {
     }
     pub fn clear(&mut self) {
         self.tiles.clear();
+        self.tiles.extend_from_slice(&[0u64; 1024]);
     }
 }