From 5ab33195fb46c27a1b374532a2e30a5a9cf5314b Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 8 Feb 2024 20:34:34 -0800 Subject: compiler: inlined chain: simplify variadic lambdas. The opip syntax often generates lambdas that have a trailing parameter and use [sys:apply ...]. This is wasteful in the second and subsequent argument positions of a chain, because we know that only a single value is coming from the previous function. We can pattern match these lambdas and convert the trailing argument to a single fixed parameter. * stdlib/compiler.tl (simplify-variadic-lambda): New function. (inline-chain-rec): Try to simplify every function through simplify-variadic-lambda. The leftmost function is treated in inline-chain, so these are all second and subsequent functions. --- stdlib/compiler.tl | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/stdlib/compiler.tl b/stdlib/compiler.tl index 00dbd292..e722cf41 100644 --- a/stdlib/compiler.tl +++ b/stdlib/compiler.tl @@ -2303,12 +2303,25 @@ ,*lm-body)) lm-expr))))) +(defun simplify-variadic-lambda (form) + (if-match @(require (lambda @(and @params @(or @(end (@nil . @rest)) + @rest)) + [sys:apply . @args]) + rest + (eq 1 (count rest (flatten args))) + (eq [args -1] rest)) + form + ^(lambda (,*(butlastn 0 params) ,rest) + [call ,*(butlastn 1 args) ,rest]) + form)) + (defun inline-chain-rec (form arg) (match-ecase form ((chain @fun) - ^(call ,fun ,arg)) + ^(call ,(simplify-variadic-lambda fun) ,arg)) ((chain @fun . @rest) - (inline-chain-rec ^(chain ,*rest) ^(call ,fun ,arg))))) + (inline-chain-rec ^(chain ,*rest) + ^(call ,(simplify-variadic-lambda fun) ,arg))))) (defun can-inline-chain (form) (let (yes) -- cgit v1.2.3