on master help sign in
sierpinski example: Don't allocate a bunch of temporary vectors
87d4f24Nick Fitzgerald
examples/sierpinski-triangle/src/lib.rs+14−15
@@ -79,27 +79,23 @@ impl Container {
79 y: f64,
79 y: f64,
80 s: f64,
80 s: f64,
81 content: u32,
81 content: u32,
82− ) -> Vec<Node<'bump>>
82+ children: &mut bumpalo::collections::Vec<Node<'bump>>,
83− where
83+ ) where
84 'a: 'bump,
84 'a: 'bump,
85 {
85 {
86 if s <= self.size {
86 if s <= self.size {
87− return vec![self.dot(bump, x, y, content)];
87+ children.push(self.dot(bump, x, y, content));
88+ return;
88 }
89 }
89
90
90 let s = s / 2.0;
91 let s = s / 2.0;
91
92
92− vec![
93− // Top of triangle
93+ // Top of triangle
94− self.triangle(bump, x, y - (s / 2.0), s, content),
94+ self.triangle(bump, x, y - (s / 2.0), s, content, children);
95− // Bottom-left of triangle
95+ // Bottom-left of triangle
96− self.triangle(bump, x - s, y + (s / 2.0), s, content),
96+ self.triangle(bump, x - s, y + (s / 2.0), s, content, children);
97− // Bottom-right of triangle
97+ // Bottom-right of triangle
98− self.triangle(bump, x + s, y + (s / 2.0), s, content),
98+ self.triangle(bump, x + s, y + (s / 2.0), s, content, children);
99− ]
100− .into_iter()
101− .flatten()
102− .collect()
103 }
99 }
104}
100}
105
101
@@ -119,10 +115,13 @@ impl Render for Container {
119
115
120 let modulus = elapsed as u32 % 10;
116 let modulus = elapsed as u32 % 10;
121
117
118+ let mut children = bumpalo::collections::Vec::new_in(bump);
119+ self.triangle(bump, 0.0, 0.0, 1000.0, modulus, &mut children);
120+
122 div(bump)
121 div(bump)
123 .attr("class", "container")
122 .attr("class", "container")
124 .attr("style", self.container_transform(bump, elapsed))
123 .attr("style", self.container_transform(bump, elapsed))
125− .children(self.triangle(bump, 0.0, 0.0, 1000.0, modulus))
124+ .children(children)
126 .finish()
125 .finish()
127 }
126 }
128}
127}