Skip to content

Conversation

@stylewarning
Copy link
Member

@stylewarning stylewarning commented Aug 10, 2025

This PR is currently an experiment to address #1597.

@stylewarning stylewarning force-pushed the multiple-values branch 5 times, most recently from 0397365 to 16093b6 Compare August 11, 2025 03:47
@stylewarning
Copy link
Member Author

Current progress:

  • Functions which directly return Tuple at their exit points are converted to multiple values. Let's call these functions MV-functions.
  • Calling fst or snd on an MV-function application gets optimized.
  • Two functions are codegened for each MV-function: the normal allocating entrypoint and the multiple-value entrypoint. Code is not duplicated between them.

What's not done:

  • (inline) on an MV-function is broken.
  • It doesn't do anything transitively (i.e., a function which returns MV-function applications doesn't make the original function an MV-function).
  • It doesn't work when lambdas/HOFs are involved (e.g., (define (f) ((fn () (Tuple 0 0))))). Lambdas themselves don't have/don't get MV-versions. This severely limits the utility, since rec is expanded as a lambda.
  • Recursion and mutual recursion of MV-functions aren't detected.
  • match on an MV-function application doesn't get detected and codegened.

The current design uses tons of hacks (e.g., communicating data through symbol-plists) to get the job done. More careful design and implementation will be needed when the concept is proved out.

With all that said, here's an example of what the branch can do now. Consider this small example:

(coalton-toplevel
  (define (retup)
    (Tuple 1.0 2.0))

  (define (detup)
    (* 3 (fst (retup)))))

On main:

COALTON-USER> (cl:time (cl:loop :repeat 100000000 :do (coalton (detup))))
Evaluation took:
  1.249 seconds of real time
  1.250580 seconds of total run time (1.245448 user, 0.005132 system)
  [ Real times consist of 0.021 seconds GC time, and 1.228 seconds non-GC time. ]
  [ Run times consist of 0.021 seconds GC time, and 1.230 seconds non-GC time. ]
  100.16% CPU
  10 lambdas converted
  6,400,435,664 bytes consed

On this branch:

COALTON-USER> (cl:time (cl:loop :repeat 100000000 :do (coalton (detup))))
Evaluation took:
  0.234 seconds of real time
  0.234212 seconds of total run time (0.233849 user, 0.000363 system)
  100.00% CPU
  0 bytes consed

@stylewarning stylewarning changed the title [WIP] multiple values [WIP] multiple values versions of Tuple-returning functions Aug 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants