Build macro argument slices without intermediate allocations

This commit is contained in:
gloamxun committed 2026-12-02 16:53:00 +08:00
1 parent fd87b6aa42
commit 7767e390d6
1 file changed
+9 -8
@@ -2383,23 +2383,24 @@ impl Executor {
if !definition.long && !definition.parameters.is_empty() { if !definition.long && !definition.parameters.is_empty() {
self.reads.insert(Dependency::ParagraphToken); self.reads.insert(Dependency::ParagraphToken);
} }
let mut arguments = Vec::new(); let mut arguments = Vec::with_capacity(definition.parameters.len());
for (index, delimiter) in definition.parameters.iter().enumerate() { for (index, delimiter) in definition.parameters.iter().enumerate() {
let mut argument = Vec::new(); let argument: Arc<[Token]> = if delimiter.is_empty() {
if delimiter.is_empty() {
let next = self.raw_non_space(files)?.token; let next = self.raw_non_space(files)?.token;
self.paragraph_argument(&next, definition.long)?; self.paragraph_argument(&next, definition.long)?;
if next.kind.begin_group() { if next.kind.begin_group() {
argument = self.after_open(files)?; let argument = self.after_open(files)?;
for token in &argument { for token in &argument {
self.paragraph_argument(token, definition.long)?; self.paragraph_argument(token, definition.long)?;
} }
Arc::from(argument)
} else if next.kind.end_group() { } else if next.kind.end_group() {
return self.fail("unbalanced macro argument"); return self.fail("unbalanced macro argument");
} else { } else {
argument.push(next); Arc::from([next])
} }
} else { } else {
let mut argument = Vec::new();
let mut depth = 0; let mut depth = 0;
let mut matched = 0; let mut matched = 0;
let mut overlaps = None; let mut overlaps = None;
@@ -2440,8 +2441,8 @@ impl Executor {
return self.fail("native token limit exceeded"); return self.fail("native token limit exceeded");
} }
} }
argument = Self::strip_outer_group(argument); Arc::from(Self::strip_outer_group(argument))
} };
if trace { if trace {
self.trace_macro_argument(&definition, index, &argument, files)?; self.trace_macro_argument(&definition, index, &argument, files)?;
} }
@@ -2458,7 +2459,7 @@ impl Executor {
definition, definition,
name: name.clone(), name: name.clone(),
cursor: 0, cursor: 0,
arguments: arguments.into_iter().map(Arc::from).collect(), arguments,
call: call.invocation.unwrap_or(call.span), call: call.invocation.unwrap_or(call.span),
}); });
Ok(()) Ok(())