Skip to content

Commit

Permalink
Misc performance improvements (#403)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjohnsonpint authored Oct 1, 2024
1 parent 3c08470 commit 56f6d8d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 14 additions & 14 deletions graphql/datasource/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ 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
}
}

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 {
Expand Down Expand Up @@ -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() {
Expand All @@ -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)
}
}

Expand All @@ -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),
Expand All @@ -158,7 +158,7 @@ func (p *HypDSPlanner) captureField(ref int) *fieldInfo {
}
}

return &f
return f
}

func (p *HypDSPlanner) captureInputData(fieldRef int) error {
Expand Down
12 changes: 6 additions & 6 deletions graphql/datasource/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
Expand All @@ -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)
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 56f6d8d

Please sign in to comment.