unisonweb / unison

A friendly programming language from the future
https://unison-lang.org
Other
5.81k stars 271 forks source link

Make Ref.read Ref.write instructions #5415

Closed ChrisPenner closed 1 week ago

ChrisPenner commented 1 month ago

Overview

Benchmark:

  printTime
    "Mutate an IO.Ref 1000 times" 650 (n ->
      repeat n do
        r = IO.ref 0
        repeat 1000 do Ref.write r (Ref.read r + 1))

Old:

Mutate an IO.Ref 1000 times
675.29µs

New

Mutate an IO.Ref 1000 times
659.252µs

So, a difference of 675.29µs - 659.252µs = 16.038µs, and (16.038µs / 1000 repetitions) / 2 calls = 8.019ns per foreign call

Implementation notes

Implements Ref.read/Ref.write as instructions. Can also do Ref.cas/Ref.readForCas if it seems worthwhile.

Interesting/controversial decisions

This seems like a pretty minor optimization, we need to be careful about doing too many of these as well because each one increases the code-size of our critical path.

ChrisPenner commented 1 week ago

This got rolled into #5447