From 56f6d8d774805ca4425c91dec3c3881629ca805f Mon Sep 17 00:00:00 2001 From: Matt Johnson-Pint Date: Tue, 1 Oct 2024 08:49:57 -0700 Subject: [PATCH] Misc performance improvements (#403) --- CHANGELOG.md | 1 + graphql/datasource/planner.go | 28 ++++++++++++++-------------- graphql/datasource/source.go | 12 ++++++------ 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5b45297..ce3469da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Use reader-writer lock on AWS secrets cache [#400](https://github.com/hypermodeAI/runtime/pull/400) - Improve bucket layout for FunctionExecutionDurationMilliseconds metric and add function_name label [#401](https://github.com/hypermodeAI/runtime/pull/401) - Improve JSON performance [#402](https://github.com/hypermodeAI/runtime/pull/402) +- Misc performance improvements [#403](https://github.com/hypermodeAI/runtime/pull/403) ## 2024-09-26 - Version 0.12.6 diff --git a/graphql/datasource/planner.go b/graphql/datasource/planner.go index 2137e3e0..60434c6e 100644 --- a/graphql/datasource/planner.go +++ b/graphql/datasource/planner.go @@ -23,7 +23,7 @@ type HypDSPlanner struct { config HypDSConfig visitor *plan.Visitor variables resolve.Variables - fields map[int]*fieldInfo + fields map[int]fieldInfo template struct { function *fieldInfo data []byte @@ -31,13 +31,13 @@ type HypDSPlanner struct { } type fieldInfo struct { - ref int `json:"-"` - Name string `json:"name"` - Alias string `json:"alias,omitempty"` - TypeName string `json:"type,omitempty"` - Fields []*fieldInfo `json:"fields,omitempty"` - IsMapType bool `json:"isMapType,omitempty"` - fieldRefs []int `json:"-"` + ref int `json:"-"` + Name string `json:"name"` + Alias string `json:"alias,omitempty"` + TypeName string `json:"type,omitempty"` + Fields []fieldInfo `json:"fields,omitempty"` + IsMapType bool `json:"isMapType,omitempty"` + fieldRefs []int `json:"-"` } func (t *fieldInfo) AliasOrName() string { @@ -82,14 +82,14 @@ func (p *HypDSPlanner) Register(visitor *plan.Visitor, configuration plan.DataSo } func (p *HypDSPlanner) EnterDocument(operation, definition *ast.Document) { - p.fields = make(map[int]*fieldInfo, len(operation.Fields)) + p.fields = make(map[int]fieldInfo, len(operation.Fields)) } func (p *HypDSPlanner) EnterField(ref int) { // Capture information about every field in the query. f := p.captureField(ref) - p.fields[ref] = f + p.fields[ref] = *f // If the field is enclosed by a root node, then it represents the function we want to call. if p.enclosingTypeIsRootNode() { @@ -116,11 +116,11 @@ func (p *HypDSPlanner) stitchFields(f *fieldInfo) { return } - f.Fields = make([]*fieldInfo, len(f.fieldRefs)) + f.Fields = make([]fieldInfo, len(f.fieldRefs)) for i, ref := range f.fieldRefs { field := p.fields[ref] f.Fields[i] = field - p.stitchFields(field) + p.stitchFields(&field) } } @@ -139,7 +139,7 @@ func (p *HypDSPlanner) captureField(ref int) *fieldInfo { definition := p.visitor.Definition walker := p.visitor.Walker - f := fieldInfo{ + f := &fieldInfo{ ref: ref, Name: operation.FieldNameString(ref), Alias: operation.FieldAliasString(ref), @@ -158,7 +158,7 @@ func (p *HypDSPlanner) captureField(ref int) *fieldInfo { } } - return &f + return f } func (p *HypDSPlanner) captureInputData(fieldRef int) error { diff --git a/graphql/datasource/source.go b/graphql/datasource/source.go index 40db0c0f..021a26ff 100644 --- a/graphql/datasource/source.go +++ b/graphql/datasource/source.go @@ -41,10 +41,10 @@ func (ds *HypermodeDataSource) Load(ctx context.Context, input []byte, out *byte } // Load the data - result, gqlErrors, err := ds.callFunction(ctx, ci) + result, gqlErrors, err := ds.callFunction(ctx, &ci) // Write the response - err = writeGraphQLResponse(ctx, out, result, gqlErrors, err, ci) + err = writeGraphQLResponse(ctx, out, result, gqlErrors, err, &ci) if err != nil { logger.Error(ctx).Err(err).Msg("Error creating GraphQL response.") } @@ -57,7 +57,7 @@ func (*HypermodeDataSource) LoadWithFiles(ctx context.Context, input []byte, fil panic("not implemented") } -func (ds *HypermodeDataSource) callFunction(ctx context.Context, callInfo callInfo) (any, []resolve.GraphQLError, error) { +func (ds *HypermodeDataSource) callFunction(ctx context.Context, callInfo *callInfo) (any, []resolve.GraphQLError, error) { // Get the function info fnInfo, err := ds.WasmHost.GetFunctionInfo(callInfo.Function.Name) @@ -100,7 +100,7 @@ func (ds *HypermodeDataSource) callFunction(ctx context.Context, callInfo callIn return result, gqlErrors, err } -func writeGraphQLResponse(ctx context.Context, out *bytes.Buffer, result any, gqlErrors []resolve.GraphQLError, fnErr error, ci callInfo) error { +func writeGraphQLResponse(ctx context.Context, out *bytes.Buffer, result any, gqlErrors []resolve.GraphQLError, fnErr error, ci *callInfo) error { fieldName := ci.Function.AliasOrName() @@ -241,7 +241,7 @@ func transformObject(data []byte, tf *fieldInfo) ([]byte, error) { // but will be missing outer quotes. So we need to add them back. v = []byte(`"` + string(v) + `"`) } - val, err = transformValue(v, f) + val, err = transformValue(v, &f) if err != nil { return nil, err } @@ -382,7 +382,7 @@ func transformPseudoMap(data []byte, tf *fieldInfo) ([]byte, error) { return buf.Bytes(), nil } -func transformErrors(messages []utils.LogMessage, ci callInfo) []resolve.GraphQLError { +func transformErrors(messages []utils.LogMessage, ci *callInfo) []resolve.GraphQLError { errors := make([]resolve.GraphQLError, 0, len(messages)) for _, msg := range messages { // Only include errors. Other messages will be captured later and