Skip to content

Commit 2b61be2

Browse files
committed
Auto merge of #13232 - jplatte:mbe-refactor, r=Veykril
Refactor macro-by-example code I had a look at the MBE code because of #7857. I found some easy readability wins, that might also _marginally_ improve perf.
2 parents dbb8fed + c4a87ee commit 2b61be2

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

crates/mbe/src/expander/matcher.rs

+14-23
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,16 @@ impl BindingsBuilder {
203203
}
204204

205205
fn build(self, idx: &BindingsIdx) -> Bindings {
206-
let mut bindings = Bindings::default();
207-
self.build_inner(&mut bindings, &self.nodes[idx.0]);
208-
bindings
206+
self.build_inner(&self.nodes[idx.0])
209207
}
210208

211-
fn build_inner(&self, bindings: &mut Bindings, link_nodes: &[LinkNode<Rc<BindingKind>>]) {
209+
fn build_inner(&self, link_nodes: &[LinkNode<Rc<BindingKind>>]) -> Bindings {
210+
let mut bindings = Bindings::default();
212211
let mut nodes = Vec::new();
213212
self.collect_nodes(link_nodes, &mut nodes);
214213

215214
for cmd in nodes {
216-
match &**cmd {
215+
match cmd {
217216
BindingKind::Empty(name) => {
218217
bindings.push_empty(name);
219218
}
@@ -246,13 +245,15 @@ impl BindingsBuilder {
246245
}
247246
}
248247
}
248+
249+
bindings
249250
}
250251

251252
fn collect_nested_ref<'a>(
252253
&'a self,
253254
id: usize,
254255
len: usize,
255-
nested_refs: &mut Vec<&'a Vec<LinkNode<Rc<BindingKind>>>>,
256+
nested_refs: &mut Vec<&'a [LinkNode<Rc<BindingKind>>]>,
256257
) {
257258
self.nested[id].iter().take(len).for_each(|it| match it {
258259
LinkNode::Node(id) => nested_refs.push(&self.nodes[*id]),
@@ -262,26 +263,16 @@ impl BindingsBuilder {
262263

263264
fn collect_nested(&self, idx: usize, nested_idx: usize, nested: &mut Vec<Bindings>) {
264265
let last = &self.nodes[idx];
265-
let mut nested_refs = Vec::new();
266+
let mut nested_refs: Vec<&[_]> = Vec::new();
266267
self.nested[nested_idx].iter().for_each(|it| match *it {
267268
LinkNode::Node(idx) => nested_refs.push(&self.nodes[idx]),
268269
LinkNode::Parent { idx, len } => self.collect_nested_ref(idx, len, &mut nested_refs),
269270
});
270271
nested_refs.push(last);
271-
272-
nested_refs.into_iter().for_each(|iter| {
273-
let mut child_bindings = Bindings::default();
274-
self.build_inner(&mut child_bindings, iter);
275-
nested.push(child_bindings)
276-
})
272+
nested.extend(nested_refs.into_iter().map(|iter| self.build_inner(iter)));
277273
}
278274

279-
fn collect_nodes_ref<'a>(
280-
&'a self,
281-
id: usize,
282-
len: usize,
283-
nodes: &mut Vec<&'a Rc<BindingKind>>,
284-
) {
275+
fn collect_nodes_ref<'a>(&'a self, id: usize, len: usize, nodes: &mut Vec<&'a BindingKind>) {
285276
self.nodes[id].iter().take(len).for_each(|it| match it {
286277
LinkNode::Node(it) => nodes.push(it),
287278
LinkNode::Parent { idx, len } => self.collect_nodes_ref(*idx, *len, nodes),
@@ -291,7 +282,7 @@ impl BindingsBuilder {
291282
fn collect_nodes<'a>(
292283
&'a self,
293284
link_nodes: &'a [LinkNode<Rc<BindingKind>>],
294-
nodes: &mut Vec<&'a Rc<BindingKind>>,
285+
nodes: &mut Vec<&'a BindingKind>,
295286
) {
296287
link_nodes.iter().for_each(|it| match it {
297288
LinkNode::Node(it) => nodes.push(it),
@@ -386,10 +377,10 @@ fn match_loop_inner<'t>(
386377
let op = match item.dot.peek() {
387378
None => {
388379
// We are at or past the end of the matcher of `item`.
389-
if item.up.is_some() {
380+
if let Some(up) = &item.up {
390381
if item.sep_parsed.is_none() {
391382
// Get the `up` matcher
392-
let mut new_pos = *item.up.clone().unwrap();
383+
let mut new_pos = (**up).clone();
393384
new_pos.bindings = bindings_builder.copy(&new_pos.bindings);
394385
// Add matches from this repetition to the `matches` of `up`
395386
bindings_builder.push_nested(&mut new_pos.bindings, &item.bindings);
@@ -402,7 +393,7 @@ fn match_loop_inner<'t>(
402393

403394
// Check if we need a separator.
404395
// We check the separator one by one
405-
let sep_idx = *item.sep_parsed.as_ref().unwrap_or(&0);
396+
let sep_idx = item.sep_parsed.unwrap_or(0);
406397
let sep_len = item.sep.as_ref().map_or(0, Separator::tt_count);
407398
if item.sep.is_some() && sep_idx != sep_len {
408399
let sep = item.sep.as_ref().unwrap();

0 commit comments

Comments
 (0)