Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

use goroutine about processing each array elements #316

Merged
merged 2 commits into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions codegen/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,28 +212,37 @@ func (f *Field) doWriteJson(val string, remainingMods []string, astType *ast.Typ
}
var arr = "arr" + strconv.Itoa(depth)
var index = "idx" + strconv.Itoa(depth)
var wg = "wg" + strconv.Itoa(depth)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this probably doesn't need to be nested, and can be shared by the whole field.

var usePtr bool
if len(remainingMods) == 1 && !isPtr {
usePtr = true
}

return tpl(`{{.arr}} := graphql.Array{}
return tpl(`
{{.arr}} := make(graphql.Array, len({{.val}}))
var {{.wg}} sync.WaitGroup
{{.wg}}.Add(len({{.val}}))
for {{.index}} := range {{.val}} {
{{- if not .isScalar }}
rctx := &graphql.ResolverContext{
Index: &{{.index}},
Result: {{ if .usePtr }}&{{end}}{{.val}}[{{.index}}],
}
ctx := graphql.WithResolverContext(ctx, rctx)
{{- end}}
{{.arr}} = append({{.arr}}, func() graphql.Marshaler {
{{ .next }}
}())
go func({{.index}} int) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably avoid creating a new gofunc if:

  • there is only one element in the array
  • the child field doesn't have a resolver (accessing properties is cheap and should happen sequentially)

defer {{.wg}}.Done()
{{- if not .isScalar }}
rctx := &graphql.ResolverContext{
Index: &{{.index}},
Result: {{ if .usePtr }}&{{end}}{{.val}}[{{.index}}],
}
ctx := graphql.WithResolverContext(ctx, rctx)
{{- end}}
{{.arr}}[{{.index}}] = func() graphql.Marshaler {
{{ .next }}
}()
}({{.index}})
}
{{.wg}}.Wait()
return {{.arr}}`, map[string]interface{}{
"val": val,
"arr": arr,
"index": index,
"wg": wg,
"isScalar": f.IsScalar,
"usePtr": usePtr,
"next": f.doWriteJson(val+"["+index+"]", remainingMods[1:], astType.Elem, false, depth+1),
Expand Down
Loading