Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: just in time compilation #9

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft

core: just in time compilation #9

wants to merge 13 commits into from

Conversation

xNaCly
Copy link
Owner

@xNaCly xNaCly commented Apr 10, 2024

Overview

The JIT compiler makes use of the plugin package. It compiles functions, either anonymous or named, and does so by generating go code, compiling this code into a go plugin and loading the compiled function from the plugin into the running process. Thus potentially improving performance for long-running or processing intensive Sophia scripts.

A rudimentary overview of the idea behind the JIT compiler in conjunction with the aforementioned plugin package can be found, here.

Current Performance improvements

All tests done with the currently supported features in examples/jit.phia and hyperfine without warmup runs:

hyperfine "./sophia examples/jit.phia" "./sophia -jit=0 examples/jit.phia"

An improvement prefixed with - is to be read as a decline in performance.

2024-04-11: Started

Benchmarks for commit f9bf294

Calls Jit enabled Jit disabled Improvement (ms) Improvement
100k 208ms 40ms -168ms -5x
200k 205ms 74ms -131ms -2.76x
300k 210ms 112ms -98ms -1.93x
400k 224ms 148ms -76ms -1.51x
500k 238ms 185ms -53ms -1.29x
600k 242ms 220ms -22ms -1.10x
700k 250ms 257ms 7ms 1.03x
800k 260ms 291ms 31ms 1.12x
900k 269ms 329ms 60ms 1.22x
1mio 276ms 362ms 86ms 1.32x
10mio 1.048s 3.618s 2,570ms (2.57s) 3.45x
50mio 4.461s 18.034s 13,573ms (13.573s) 4.04x

JIT Benchmark - Sophia

The break-even point when comparing the JIT and the AST walker lays at around 700k iterations.

2024-04-11: Paralleled compiler invocation

Benchmarks for commit eb3efbb

Calls Jit enabled Jit disabled Improvement (ms) Improvement
100000 38.6ms 38.1ms -0.50 1.01x
200000 62.2ms 74.4ms 12.20 1.2x
300000 82.7ms 111ms 28.30 1.34x
400000 104.4ms 147.9ms 43.50 1.42x
500000 127.7ms 184.8ms 57.10 1.45x
600000 196.3ms 218.8ms 22.50 1.11x
700000 205.7ms 255.6ms 49.90 1.24x
800000 214.5ms 292.6ms 78.10 1.36x
900000 223.9ms 327.5ms 103.60 1.47x
1000000 230.6ms 364.3ms 133.70 1.58x
10000000 1,004ms 3,615ms 2,611.00 3.6x
50000000 4,430ms 18,130ms 13,700.00 4.09x

JIT Benchmark - Sophia

Comparing for function calls < 1 million:

f9bf294 vs eb3efbb

Paralleling the compiler invocation improved the performance in comparison to the previous commit for all function call amounts. The best improvements were made for less function calls, such as 100k calls which saw an improvement of previously 200ms to now 38ms, the same can be seen for 200k, 300k, 400k and 500k iterations which were all around 100-150ms faster than they were in the previous commit.

2024-04-14: Arithmetics

Benchmarks for commit bbd91b5

With using all newly supported features:

;; jit currently supports
(fun t [a b] 
    (let add (+ a b))
    (let sub (- a b))
    (let mul (* a b))
    (let div (/ a b))
    (let mod (% a b))
    [add sub mul div mod])

(for [n] 100_000
    (t n n))
Calls Jit enabled Jit disabled Improvement (ms) Improvement
100,000.00 86.3 95.3 9.00 1.10x
200,000.00 151.8 187.6 35.80 1.23x
300,000.00 239.5 279.5 40.00 1.16x
400,000.00 266.4 370.6 104.20 1.39x
500,000.00 286.5 467.3 180.80 1.63x
600,000.00 316.5 558.8 242.30 1.76x
700,000.00 345.5 651.6 306.10 1.88x
800,000.00 366.2 745.1 378.90 2.03x
900,000.00 390 834.7 444.70 2.14x
1,000,000.00 412 931 519.00 2.25x
10,000,000.00 2,632 9,255 6,623.00 3.51x
50,000,000.00 12,450 46,350 33,900.00 3.72x

JIT Benchmark - Sophia

Progress

  • constants
    • floats
    • strings
    • booleans
  • variables
  • arrays
  • template strings
  • arithmetic operators
    • addition
    • subtraction
    • multiplication
    • division
    • modulus
    • negation
  • comparison operators
    • greater than
    • less than
    • equals
  • boolean operators
    • and
    • or
    • negation
  • function calls
  • lambda functions
  • function definition
  • for loops
  • if statements
  • index
    • array
    • object
  • match / enhanced switch
  • merge
    • array
    • string
  • return

Downsides

Downsides of the proposed JIT compiler include:

  • the plugin package supports: Linux, macOS, BSD, not windows
  • compiling a go plugin requires the go compiler tool chain to be present on the system the JIT operates on

@xNaCly xNaCly added the enhancement New feature or request label Apr 10, 2024
@xNaCly xNaCly marked this pull request as ready for review April 11, 2024 18:18
@xNaCly xNaCly marked this pull request as draft April 12, 2024 09:27
@xNaCly xNaCly changed the title core: started work on the jit compiler core: just in time compilation Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant