mighty-gerbils / gerbil

Gerbil Scheme
https://cons.io
GNU Lesser General Public License v2.1
1.13k stars 111 forks source link

Interface cast spin lock #1126

Closed vyzo closed 4 months ago

vyzo commented 4 months ago

This changes the prototype table lock to be a spin lock .... and it is almost 3x faster.

Before:

$ /tmp/interface-bench cast 10 1000000
(time (let () (declare (not safe)) (std/interface-benchmark#cast-benchmark _iters180_ _threads179_ std/interface-benchmark#do-cast)))
    2.117610 secs real time
    2.117559 secs cpu time (2.117150 user, 0.000409 system)
    142 collections accounting for 0.122705 secs real time (0.122418 user, 0.000250 system)
    1120026112 bytes allocated
    672 minor faults
    no major faults
    5529541900 cpu cycles

After:

$ /tmp/interface-bench2 cast 10 1000000
(time (let () (declare (not safe)) (std/interface-benchmark#cast-benchmark _iters180_ _threads179_ std/interface-benchmark#do-cast)))
    0.857509 secs real time
    0.857492 secs cpu time (0.857288 user, 0.000204 system)
    142 collections accounting for 0.104038 secs real time (0.103984 user, 0.000000 system)
    1119990592 bytes allocated
    672 minor faults
    no major faults
    2239135638 cpu cycles
netlify[bot] commented 4 months ago

Deploy Preview for elastic-ritchie-8f47f9 ready!

Name Link
Latest commit 8808136afc57ed3487407d733ae7127dd90e542e
Latest deploy log https://app.netlify.com/sites/elastic-ritchie-8f47f9/deploys/65d472029dc6de000875d4ef
Deploy Preview https://deploy-preview-1126--elastic-ritchie-8f47f9.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

fare commented 4 months ago

I think we should be using futexes or something else on Linux that doesn't busywait and waste batteries on laptop smpβ€”and importantly, this should be all abstracted over somehow in a macro.

If you want this in, fine for now, but then you should open an issue about getting locking right on SMP.

vyzo commented 4 months ago

It is abstracted over a macro, see __lock-inline! and __unlock-inline!.

It is a workable solution for SMP, the critical sections are expected to be small (less than 10ns each) so the spin/busy wait is not that bad.

Ideally we'd have futexes; unfortunately full gambit mutexes are so much slower it is not even funny.

vyzo commented 4 months ago

Follow up issue in #1128.