Skip to content

Commit

Permalink
resolver: faster time formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mjarkk committed Jun 27, 2021
1 parent 6d3edb7 commit fd4a9ac
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
8 changes: 5 additions & 3 deletions resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,9 @@ func (ctx *Ctx) resolveFieldDataValue(query *field, codeStructure *obj, dept uin
ctx.write([]byte("null"))
return
}
stringToJson([]byte(timeToString(timeValue)), &ctx.result)
ctx.writeByte('"')
timeToString(&ctx.result, timeValue)
ctx.writeByte('"')
return
default:
ctx.addErr("has invalid data type")
Expand Down Expand Up @@ -833,8 +835,8 @@ func parseTime(val string) (time.Time, error) {
return parsedTime, nil
}

func timeToString(t time.Time) string {
return t.Format("2006-01-02T15:04:05.000Z")
func timeToString(target *[]byte, t time.Time) {
*target = t.AppendFormat(*target, "2006-01-02T15:04:05.000Z")
}

func (s *Schema) objToQlTypeName(item *obj, target *bytes.Buffer) {
Expand Down
37 changes: 22 additions & 15 deletions resolve_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,10 @@ import (
"os"
"runtime/pprof"
"testing"
"time"
)

func BenchmarkCheckNames(b *testing.B) {
// On laptop
// BenchmarkCheckNames-12 2277 480462 ns/op 189581 B/op 2343 allocs/op
// BenchmarkCheckNames-12 35962 32972 ns/op 17992 B/op 339 allocs/op

// On desktop
// BenchmarkCheckNames-16 13941 86455 ns/op 23152 B/op 993 allocs/op

for i := 0; i < b.N; i++ {
ParseQueryAndCheckNames(schemaQuery, nil)
}
Expand All @@ -23,11 +17,11 @@ func BenchmarkCheckNames(b *testing.B) {
func BenchmarkResolve(b *testing.B) {
// On laptop
// BenchmarkResolve-12 854 1383447 ns/op 833377 B/op 11670 allocs/op // First ran
// BenchmarkResolve-12 852 1379526 ns/op 833150 B/op 11668 allocs/op // Placed some resolver global variables in global scope
// BenchmarkResolve-12 915 1283598 ns/op 782547 B/op 10384 allocs/op // Use path from Ctx
// BenchmarkResolve-12 886 1308011 ns/op 782452 B/op 10379 allocs/op // Use array for value
// BenchmarkResolve-12 1202 998317 ns/op 313687 B/op 6064 allocs/op // Reduced a lot of string usage
// BenchmarkResolve-12 1294 898636 ns/op 307930 B/op 5686 allocs/op // Change value formatting to allocate less
// BenchmarkResolve-12 852 1379526 ns/op 833150 B/op 11668 allocs/op
// BenchmarkResolve-12 915 1283598 ns/op 782547 B/op 10384 allocs/op
// BenchmarkResolve-12 886 1308011 ns/op 782452 B/op 10379 allocs/op
// BenchmarkResolve-12 1202 998317 ns/op 313687 B/op 6064 allocs/op
// BenchmarkResolve-12 1294 898636 ns/op 307930 B/op 5686 allocs/op
// BenchmarkResolve-12 3206 345997 ns/op 57292 B/op 3686 allocs/op
// BenchmarkResolve-12 3452 320228 ns/op 57235 B/op 3686 allocs/op
// BenchmarkResolve-12 3250 311136 ns/op 57281 B/op 3686 allocs/op
Expand Down Expand Up @@ -62,9 +56,6 @@ func BenchmarkResolve(b *testing.B) {
}

func BenchmarkEncodeString(b *testing.B) {
// BenchmarkEncodeString-12 7684732 151.1 ns/op 0 B/op 0 allocs/op
// BenchmarkEncodeString-12 9391905 121.1 ns/op 0 B/op 0 allocs/op

inputString1 := []byte("abc")
inputString2 := []byte("Some long string that includes spaces and a .")
inputString3 := []byte(`Wow this includes \\ and && and <> and ""`)
Expand All @@ -77,3 +68,19 @@ func BenchmarkEncodeString(b *testing.B) {
out = out[:0]
}
}

func BenchmarkResolveTime(b *testing.B) {
s, _ := ParseSchema(TestExecTimeIOData{}, M{}, nil)

now := time.Now()
testTimeInput := []byte{}
timeToString(&testTimeInput, now)
query := `{foo(t: "` + string(testTimeInput) + `")}`

for i := 0; i < b.N; i++ {
_, errs := s.Resolve(query, ResolveOptions{})
for _, err := range errs {
panic(err)
}
}
}
10 changes: 6 additions & 4 deletions resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,15 +703,17 @@ func (TestExecTimeIOData) ResolveFoo(args struct{ T time.Time }) time.Time {

func TestExecTimeIO(t *testing.T) {
now := time.Now()
testTimeInput := timeToString(now)
testTimeInput := []byte{}
timeToString(&testTimeInput, now)

out, errs := parseAndTest(t, `{foo(t: "`+testTimeInput+`")}`, TestExecTimeIOData{}, M{})
out, errs := parseAndTest(t, `{foo(t: "`+string(testTimeInput)+`")}`, TestExecTimeIOData{}, M{})
for _, err := range errs {
panic(err)
}

exectedOutTime := timeToString(now.AddDate(3, 2, 1).Add(time.Hour + time.Second))
Equal(t, `{"foo":"`+exectedOutTime+`"}`, out)
exectedOutTime := []byte{}
timeToString(&exectedOutTime, now.AddDate(3, 2, 1).Add(time.Hour+time.Second))
Equal(t, `{"foo":"`+string(exectedOutTime)+`"}`, out)
}

func TestExecInputIDInvalidArguments(t *testing.T) {
Expand Down

0 comments on commit fd4a9ac

Please sign in to comment.