Skip to content

Commit

Permalink
fix: enable pretouch apis
Browse files Browse the repository at this point in the history
  • Loading branch information
liuq19 committed Apr 19, 2024
1 parent d613727 commit e6740d9
Show file tree
Hide file tree
Showing 6 changed files with 924 additions and 862 deletions.
23 changes: 12 additions & 11 deletions decoder/decoder.go
Expand Up @@ -18,10 +18,8 @@ package decoder

import (
`io`
`reflect`

`github.com/bytedance/sonic/dev/decoder`
`github.com/bytedance/sonic/option`
)

func init() {
Expand Down Expand Up @@ -51,18 +49,21 @@ const (
var (
// NewDecoder creates a new decoder instance.
NewDecoder = decoder.NewDecoder

// Pretouch compiles vt ahead-of-time to avoid JIT compilation on-the-fly, in
// order to reduce the first-hit latency.
//
// Opts are the compile options, for example, "option.WithCompileRecursiveDepth" is
// a compile option to set the depth of recursive compile for the nested struct type.
Pretouch = decoder.Pretouch

// Skip skips only one json value, and returns first non-blank character position and its ending position if it is valid.
// Otherwise, returns negative error code using start and invalid character position using end
Skip = decoder.Skip
)

// Pretouch compiles vt ahead-of-time to avoid JIT compilation on-the-fly, in
// order to reduce the first-hit latency.
//
// Opts are the compile options, for example, "option.WithCompileRecursiveDepth" is
// a compile option to set the depth of recursive compile for the nested struct type.
func Pretouch(vt reflect.Type, opts ...option.CompileOption) error {
return nil
}

// TODO: replace stream decoder with dev/decoder

type StreamDecoder = decoder.StreamDecoder

// NewStreamDecoder adapts to encoding/json.NewDecoder API.
Expand Down
2 changes: 1 addition & 1 deletion dev/decoder/compile_struct.go
Expand Up @@ -88,7 +88,7 @@ func (c *compiler) compileFieldStringOption(vt reflect.Type) decFunc {
func (c *compiler) compileStruct(vt reflect.Type) decFunc {
c.enter(vt)
defer c.exit(vt)
if c.depth >= c.opts.MaxInlineDepth || (c.counts > 0 && vt.NumField() >= _MAX_FIELDS) {
if c.depth >= c.opts.MaxInlineDepth + 1 || (c.counts > 0 && vt.NumField() >= _MAX_FIELDS) {
return &recuriveDecoder{
typ: rt.UnpackType(vt),
}
Expand Down
57 changes: 54 additions & 3 deletions dev/decoder/decoder.go
Expand Up @@ -7,11 +7,9 @@ import (
"encoding/json"
"github.com/bytedance/sonic/ast"
"github.com/bytedance/sonic/dev/internal/rt"
"github.com/davecgh/go-spew/spew"
"github.com/bytedance/sonic/option"
)

var _ = spew.Dump

// Decoder is the decoder context object
type Decoder struct {
json string
Expand Down Expand Up @@ -96,3 +94,56 @@ func Skip(data []byte) (start int, end int) {
}
return start, pos
}

// Pretouch compiles vt ahead-of-time to avoid JIT compilation on-the-fly, in
// order to reduce the first-hit latency.
//
// Opts are the compile options, for example, "option.WithCompileRecursiveDepth" is
// a compile option to set the depth of recursive compile for the nested struct type.
func Pretouch(vt reflect.Type, opts ...option.CompileOption) error {
cfg := option.DefaultCompileOptions()
for _, opt := range opts {
opt(&cfg)
}
return pretouchRec(map[reflect.Type]bool{vt:true}, cfg)
}

func pretouchType(_vt reflect.Type, opts option.CompileOptions) (map[reflect.Type]bool, error) {
/* compile function */
compiler := newCompiler().apply(opts)
decoder := func(vt *rt.GoType, _ ...interface{}) (interface{}, error) {
if f, err := compiler.compileType(_vt); err != nil {
return nil, err
} else {
return f, nil
}
}

/* find or compile */
vt := rt.UnpackType(_vt)
if val := programCache.Get(vt); val != nil {
return nil, nil
} else if _, err := programCache.Compute(vt, decoder); err == nil {
return compiler.visited, nil
} else {
return nil, err
}
}

func pretouchRec(vtm map[reflect.Type]bool, opts option.CompileOptions) error {
if opts.RecursiveDepth < 0 || len(vtm) == 0 {
return nil
}
next := make(map[reflect.Type]bool)
for vt := range(vtm) {
sub, err := pretouchType(vt, opts)
if err != nil {
return err
}
for svt := range(sub) {
next[svt] = true
}
}
opts.RecursiveDepth -= 1
return pretouchRec(next, opts)
}

Check failure on line 149 in dev/decoder/decoder.go

View workflow job for this annotation

GitHub Actions / build (1.20.x)

syntax error: unexpected var after top level declaration

Check failure on line 149 in dev/decoder/decoder.go

View workflow job for this annotation

GitHub Actions / build (1.20.x)

syntax error: unexpected var after top level declaration

0 comments on commit e6740d9

Please sign in to comment.