Skip to content

Commit

Permalink
use pl_capture_reset to save on allocs
Browse files Browse the repository at this point in the history
  • Loading branch information
guregu committed Jun 5, 2024
1 parent eab5adf commit 3795c79
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/trealla
6 changes: 3 additions & 3 deletions trealla/interop.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ func (pl *prolog) hostCall( /*c *wasmtime.Caller,*/ subquery, msgptr, msgsize, r
if err := subq.readOutput(); err != nil {
return 0, wasmtime.NewTrap(err.Error())
}
if _, err := pl.pl_capture.Call(pl.store, pl.ptr); err != nil {
return 0, wasmtime.NewTrap(err.Error())
}
// if _, err := pl.pl_capture.Call(pl.store, pl.ptr); err != nil {
// return 0, wasmtime.NewTrap(err.Error())
// }

return wasmTrue, nil
}
Expand Down
Binary file modified trealla/libtpl.wasm
Binary file not shown.
31 changes: 21 additions & 10 deletions trealla/prolog.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,17 @@ type prolog struct {
spawning map[int32]*query
limiter chan struct{}

ptr int32
realloc wasmFunc
free wasmFunc
pl_consult wasmFunc
pl_capture wasmFunc
pl_capture_read wasmFunc
pl_capture_free wasmFunc
pl_query wasmFunc
pl_redo wasmFunc
pl_done wasmFunc
ptr int32
realloc wasmFunc
free wasmFunc
pl_consult wasmFunc
pl_capture wasmFunc
pl_capture_read wasmFunc
pl_capture_reset wasmFunc
pl_capture_free wasmFunc
pl_query wasmFunc
pl_redo wasmFunc
pl_done wasmFunc

procs map[string]Predicate

Expand Down Expand Up @@ -182,6 +183,11 @@ func (pl *prolog) init(parent *prolog) error {
return err
}

pl.pl_capture_reset, err = pl.function("pl_capture_reset")
if err != nil {
return err
}

pl.pl_capture_free, err = pl.function("pl_capture_free")
if err != nil {
return err
Expand Down Expand Up @@ -261,6 +267,11 @@ func (pl *prolog) init(parent *prolog) error {
}
pl.ptr = ptr.(int32)

_, err = pl.pl_capture.Call(pl.store, pl.ptr)
if err != nil {
return err
}

if err := pl.loadBuiltins(); err != nil {
return fmt.Errorf("trealla: failed to load builtins: %w", err)
}
Expand Down
14 changes: 2 additions & 12 deletions trealla/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func (q *query) readOutput() error {
if err != nil {
return err
}
defer pl.pl_capture_reset.Call(pl.store, pl.ptr)

stdoutlen := pl.indirect(q.stdoutlen)
stdoutptr := pl.indirect(q.stdoutptr)
Expand All @@ -134,7 +135,7 @@ func (q *query) readOutput() error {
}
q.stderr.WriteString(stderr)

pl.pl_capture_free.Call(pl.store, pl.ptr)
// pl.pl_capture_free.Call(pl.store, pl.ptr)

return nil
}
Expand Down Expand Up @@ -208,11 +209,6 @@ func (pl *prolog) start(ctx context.Context, goal string, options ...QueryOption
ch <- fmt.Errorf("trealla: panic: %v", ex)
}
}()
_, err := pl.pl_capture.Call(pl.store, pl.ptr)
if err != nil {
ch <- err
return
}

v, err := pl.pl_query.Call(pl.store, pl.ptr, goalstr.ptr, subqptr, 0)
if err == nil {
Expand Down Expand Up @@ -294,12 +290,6 @@ func (q *query) redo(ctx context.Context) bool {
}
}()

_, err := pl.pl_capture.Call(pl.store, pl.ptr)
if err != nil {
ch <- err
return
}

v, err := pl.pl_redo.Call(pl.store, q.subquery)
if err == nil {
ret = v.(int32)
Expand Down

0 comments on commit 3795c79

Please sign in to comment.