johnynek / bosatsu

A python-ish pure and total functional programming language
Apache License 2.0
222 stars 11 forks source link

Inline applying lambda in Python code generation #1124

Closed johnynek closed 5 months ago

johnynek commented 5 months ago

Here is an example:

 def log2(i):
-    ___t64 = 0 < i
-    ___t65 = 0
-    ___t66 = i
-    ___t67 = 0
-    while ___t64:
-        ___t65 = (lambda n, cnt: (n // 2, cnt + 1))(___t66, ___t67)
-        ___t68 = ___t65[0]
-        ___t67 = ___t65[1]
-        ___t64 = (0 < ___t68) and (___t68 < ___t66)
-        ___t66 = ___t68
-    return ___t67
+    ___t16 = 0 < i
+    ___t17 = 0
+    ___t18 = i
+    ___t19 = 0
+    while ___t16:
+        ___t20 = ___t18 // 2
+        ___t19 = ___t19 + 1
+        ___t16 = (0 < ___t20) and (___t20 < ___t18)
+        ___t18 = ___t20
+    return ___t19

In the int_loop built-in function, we often wind up with (lambda x, y: ...)(x_arg, y_arg) patterns, which could be inlined. Also, in this example, we know we are returning a tuple2. So, we can try to inline the lambda, and if that works, we can often avoid allocating the tuple as well.

codecov-commenter commented 5 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (4b0fb03) 92.24% compared to head (bf70ce4) 92.28%.

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #1124 +/- ## ========================================== + Coverage 92.24% 92.28% +0.03% ========================================== Files 96 96 Lines 10974 11041 +67 Branches 2605 2630 +25 ========================================== + Hits 10123 10189 +66 - Misses 851 852 +1 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.