Skip to content

Commit

Permalink
[*] refactor: 中间备份
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-M-C committed Apr 26, 2021
1 parent 9ec10c6 commit aa3f096
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 40 deletions.
36 changes: 0 additions & 36 deletions conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package jsonvalue
import (
"bytes"
"fmt"
"log"
"reflect"
"strconv"
)
Expand All @@ -20,41 +19,6 @@ func parseFloat(b []byte) (float64, error) {
return strconv.ParseFloat(unsafeBtoS(b), 64)
}

// reference: https://golang.org/src/encoding/json/decode.go, func unquote()
func parseString(b []byte) (string, []byte, error) { // TODO:
firstQuote := bytes.Index(b, []byte{'"'})
lastQuote := bytes.LastIndex(b, []byte{'"'})
if firstQuote < 0 || firstQuote == lastQuote {
return "", nil, ErrIllegalString
}

b = b[firstQuote : lastQuote-firstQuote+1]

t, ok := unquoteBytes(b)
if !ok {
return "", nil, fmt.Errorf("invalid string '%s'", unsafeBtoS(b))
}
return unsafeBtoS(t), b, nil
}

func unquoteBytes(s []byte) (t []byte, ok bool) {
if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' {
return
}
return parseStrText(s[1 : len(s)-1])
}

func parseStrText(s []byte) (t []byte, ok bool) {
it := &iter{b: s}
le, err := it.parseStrFromBytesBackward(0, len(it.b))
if err != nil {
log.Printf("it.parseStrFromBytesBackward error: %v", err)
return nil, false
}

return s[:le], true
}

func formatBool(b bool) string {
if b {
return "true"
Expand Down
4 changes: 2 additions & 2 deletions jsonvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ func (v *V) valueBytes() []byte {
return v.srcByte[v.srcOffset:v.srcEnd]
}

// UnmarshalString is equavilent to Unmarshal(unsafeBtoS(b)), but much more efficient.
// UnmarshalString is equavilent to Unmarshal([]byte(b)), but much more efficient.
//
// UnmarshalString 等效于 Unmarshal(unsafeBtoS(b)),但效率更高。
// UnmarshalString 等效于 Unmarshal([]byte(b)),但效率更高。
func UnmarshalString(s string) (*V, error) {
// reference: https://stackoverflow.com/questions/41591097/slice-bounds-out-of-range-when-using-unsafe-pointer
// sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
Expand Down
24 changes: 22 additions & 2 deletions pprof_test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"os"
"runtime/pprof"
"runtime/trace"
"time"

jsonvalue "github.com/Andrew-M-C/go.jsonvalue"
Expand All @@ -15,6 +16,7 @@ import (
// go run .
// go tool pprof -http=:6060 ./jsonvalue-unmarshal.profile
// go tool pprof -http=:6061 ./jsoniter-get.profile
// go tool trace jsonvalue-unmarshal-trace.profile

const (
iteration = 200000
Expand All @@ -25,12 +27,29 @@ var (
printf = log.Printf
)

func jsonvalueUnmarshalTraceTest() {
ft, err := os.OpenFile("jsonvalue-unmarshal-trace.profile", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
if err != nil {
log.Fatal(err)
}
defer ft.Close()
trace.Start(ft)
defer trace.Stop()

_, err = jsonvalue.Unmarshal(unmarshalText)
if err != nil {
printf("unmarshal error: %v", err)
return
}

printf("jsonvalue unmarshal trace done")
}

func jsonvalueUnmarshalTest() {
f, err := os.OpenFile("jsonvalue-unmarshal.profile", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
if err != nil {
log.Fatal(err)
}

defer f.Close()
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
Expand All @@ -43,7 +62,6 @@ func jsonvalueUnmarshalTest() {
}
}

printf("jsonvalue unmarshal done")
}

func jsonvalueMarshalTest() {
Expand Down Expand Up @@ -250,6 +268,8 @@ func main() {
printf("done, elapsed %v", time.Since(start))
}

run(jsonvalueUnmarshalTraceTest)

run(jsonvalueUnmarshalTest)
run(jsonvalueMarshalTest)

Expand Down

0 comments on commit aa3f096

Please sign in to comment.