Feat: Enable nested destructuring optimization

This commit introduces optimizations for nested destructuring, allowing
tuples and records to be flattened and matched directly against function
arguments. This significantly improves performance by enabling more
constant folding and reducing intermediate allocations.

The changes include:
- Modifying the `Binder` to correctly count nested parameters.
- Enhancing `flatten_tuple` in the `Optimizer` to handle records and NOP
  nodes.
- Updating `map_params_to_args` to recursively destructure nested
  compound arguments.
- Adding integration tests to verify the correctness of tuple-to-tuple
  and record-to-tuple destructuring optimizations.
This commit is contained in:
Michael Schimmel
2026-02-22 16:34:40 +01:00
parent c03b2af770
commit 2e8d5284c2
5 changed files with 68 additions and 23 deletions
+17
View File
@@ -278,4 +278,21 @@ mod tests {
}
assert_eq!(format!("{}", result.unwrap()), "60");
}
#[test]
fn test_nested_destructuring_optimization() {
let env = Environment::new();
// 1. Tuple-to-Tuple
let source_tuple = "((fn [[x y]] (+ x y)) [10 20])";
assert_eq!(format!("{}", env.run_script(source_tuple).unwrap()), "30");
let dump_tuple = env.dump_ast(source_tuple).unwrap();
assert!(dump_tuple.contains("Constant: 30"), "Nested tuple should be folded to 30. Dump:\n{}", dump_tuple);
// 2. Record-to-Tuple
let source_record = "((fn [[x y]] (+ x y)) {:a 5 :b 7})";
assert_eq!(format!("{}", env.run_script(source_record).unwrap()), "12");
let dump_record = env.dump_ast(source_record).unwrap();
assert!(dump_record.contains("Constant: 12"), "Record-to-Tuple destructuring should be folded to 12. Dump:\n{}", dump_record);
}
}