Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Aug 19, 2022
1 parent 08a4050 commit 9734845
Show file tree
Hide file tree
Showing 8 changed files with 385 additions and 273 deletions.
2 changes: 0 additions & 2 deletions decoder/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,11 +514,9 @@ func (self *_Compiler) compile(vt reflect.Type) (ret _Program, err error) {
}

func (self *_Compiler) compileOne(p *_Program, sp int, vt reflect.Type) {
println("decode compile:", vt.Name())
/* check for recursive nesting */
ok := self.tab[vt]
if ok {
println("decode recurse:", vt.Name())
p.rtt(_OP_recurse, vt)
return
}
Expand Down
21 changes: 8 additions & 13 deletions decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
package decoder

import (
"encoding/json"
"fmt"
"reflect"
"runtime"

"github.com/bytedance/sonic/internal/native"
"github.com/bytedance/sonic/internal/native/types"
"github.com/bytedance/sonic/internal/rt"
"github.com/bytedance/sonic/option"
`encoding/json`
`reflect`
`runtime`

`github.com/bytedance/sonic/internal/native`
`github.com/bytedance/sonic/internal/native/types`
`github.com/bytedance/sonic/internal/rt`
`github.com/bytedance/sonic/option`
)

const (
Expand Down Expand Up @@ -159,16 +158,12 @@ func Pretouch(vt reflect.Type, opts ...option.CompileOption) error {
for _, opt := range opts {
opt(&cfg)
}
if vt.Kind() == reflect.Ptr {
vt = vt.Elem()
}
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)
fmt.Printf("options %#v\n", compiler.opts)
decoder := func(vt *rt.GoType) (interface{}, error) {
if pp, err := compiler.compile(_vt); err != nil {
return nil, err
Expand Down
2 changes: 0 additions & 2 deletions encoder/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,7 @@ func (self *_Compiler) compile(vt reflect.Type) (ret _Program, err error) {
func (self *_Compiler) compileOne(p *_Program, sp int, vt reflect.Type, pv bool) {
if self.tab[vt] {
p.rtt(_OP_recurse, vt)
println("encode recurse tab:", vt.Name())
} else {
println("encode compile:", vt.Name())
self.compileRec(p, sp, vt, pv)
}
}
Expand Down
12 changes: 3 additions & 9 deletions encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,7 @@ func Pretouch(vt reflect.Type, opts ...option.CompileOption) error {
opt(&cfg)
break
}
var m = map[reflect.Type]bool{vt:true}
if vt.Kind() == reflect.Ptr {
m[vt.Elem()] = true
} else {
m[reflect.PtrTo(vt)] = true
}
return pretouchRec(m, cfg)
return pretouchRec(map[reflect.Type]bool{vt:true}, cfg)
}

func pretouchType(_vt reflect.Type, opts option.CompileOptions) (map[reflect.Type]bool, error) {
Expand Down Expand Up @@ -320,12 +314,12 @@ func pretouchRec(vtm map[reflect.Type]bool, opts option.CompileOptions) error {
return nil
}
next := make(map[reflect.Type]bool)
for vt, _ := range(vtm) {
for vt := range(vtm) {
sub, err := pretouchType(vt, opts)
if err != nil {
return err
}
for svt, _ := range(sub) {
for svt := range(sub) {
next[svt] = true
}
}
Expand Down

0 comments on commit 9734845

Please sign in to comment.