Skip to content

Commit

Permalink
jit: parallise compiler invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
xNaCly committed Apr 12, 2024
1 parent f9bf294 commit eb3efbb
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion core/consts/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ var RETURN = Return{}
var FUNC_TABLE = make(map[uint32]any, 64)

// JIT_CONSTANT determines
var JIT_CONSTANT uint16 = 1000
var JIT_CONSTANT uint64 = 1_000
19 changes: 10 additions & 9 deletions core/expr/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Call struct {
Token *token.Token
Key uint32
Args []types.Node
Calls uint16
Calls uint64
}

func (c *Call) GetChildren() []types.Node {
Expand Down Expand Up @@ -44,21 +44,22 @@ func (c *Call) Eval() any {
}

if !def.WasJitted && c.Calls >= consts.JIT_CONSTANT && def.Jited == nil && JIT != nil {
debug.Logf("[JIT] Attempting to compile function %q\n", c.Token.Raw)
def.WasJitted = true
fun, err := JIT.Compile(def)
if err != nil {
debug.Logf("[JIT] Failed to compile function %q: %s, bailing out to the interpreter\n", c.Token.Raw, err)
goto backout
}
def.Jited = fun
go func(functionDef *Func) {
debug.Logf("[JIT] Attempting to compile function %q\n", c.Token.Raw)
fun, err := JIT.Compile(def)
if err != nil {
debug.Logf("[JIT] Failed to compile function %q: %s, bailing out to the interpreter\n", c.Token.Raw, err)
return
}
def.Jited = fun
}(def)
} else if def.Jited != nil {
return def.Jited(c.Args[0].Eval())
} else {
c.Calls++
}

backout:
return callFunction(c.Token, def.Body, def.Params, c.Args)
}

Expand Down
1 change: 0 additions & 1 deletion core/expr/jit.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ type Jit struct {

// Compile compiles
func (j *Jit) Compile(ast *Func) (func(any) any, error) {
fmt.Println("Called")
j.mutex.Lock()
defer j.mutex.Unlock()

Expand Down
2 changes: 1 addition & 1 deletion examples/jit.phia
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
n)

(let sum 0)
(for [n] 50_000_000
(for [n] 100_000
(let sum
(+ (t n) sum)
)
Expand Down

0 comments on commit eb3efbb

Please sign in to comment.