Skip to content

Commit

Permalink
Avoid allocation of dynamic config constraint precedence (temporalio#…
Browse files Browse the repository at this point in the history
…6252)

## What changed?
Inline slices representation constraint precedence in generated code.

## Why?
This saves an allocation per dynamic config call.

## How did you test it?
Existing tests + bench.
  • Loading branch information
dnr authored Jul 9, 2024
1 parent c0ddb58 commit 7779fd7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 77 deletions.
38 changes: 27 additions & 11 deletions cmd/tools/gendynamicconfig/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ type (
IsGeneric bool
}
settingPrecedence struct {
Name string
GoArgs string
GoArgNames string
Index int
Name string
GoArgs string
Expr string
Index int
}
)

Expand Down Expand Up @@ -86,30 +86,51 @@ var (
{
Name: "Global",
GoArgs: "",
Expr: "[]Constraints{{}}",
},
{
Name: "Namespace",
GoArgs: "namespace string",
Expr: "[]Constraints{{Namespace: namespace}, {}}",
},
{
Name: "NamespaceID",
GoArgs: "namespaceID string",
Expr: "[]Constraints{{NamespaceID: namespaceID}, {}}",
},
{
Name: "TaskQueue",
GoArgs: "namespace string, taskQueue string, taskQueueType enumspb.TaskQueueType",
// A task-queue-name-only filter applies to a single task queue name across all
// namespaces, with higher precedence than a namespace-only filter. This is intended to
// be used by the default partition count and is probably not useful otherwise.
Expr: `[]Constraints{
{Namespace: namespace, TaskQueueName: taskQueue, TaskQueueType: taskQueueType},
{Namespace: namespace, TaskQueueName: taskQueue},
{TaskQueueName: taskQueue},
{Namespace: namespace},
{},
}`,
},
{
Name: "ShardID",
GoArgs: "shardID int32",
Expr: "[]Constraints{{ShardID: shardID}, {}}",
},
{
Name: "TaskType",
GoArgs: "taskType enumsspb.TaskType",
Expr: "[]Constraints{{TaskType: taskType}, {}}",
},
{
Name: "Destination",
GoArgs: "namespace string, destination string",
Expr: `[]Constraints{
{Namespace: namespace, Destination: destination},
{Destination: destination},
{Namespace: namespace},
{},
}`,
},
}
)
Expand Down Expand Up @@ -199,13 +220,14 @@ func (s {{.P.Name}}TypedSetting[T]) Get(c *Collection) TypedPropertyFn[T] {
func (s {{.P.Name}}TypedSetting[T]) Get(c *Collection) TypedPropertyFnWith{{.P.Name}}Filter[T] {
{{- end}}
return func({{.P.GoArgs}}) T {
prec := {{.P.Expr}}
return matchAndConvert(
c,
s.key,
s.def,
s.cdef,
s.convert,
precedence{{.P.Name}}({{.P.GoArgNames}}),
prec,
)
}
}
Expand Down Expand Up @@ -263,13 +285,7 @@ import (
const PrecedenceUnknown Precedence = 0
`, nil)
for idx, prec := range precedences {
// fill in Index and GoArgNames
prec.Index = idx + 1
var argNames []string
for _, argAndType := range strings.Split(prec.GoArgs, ",") {
argNames = append(argNames, strings.Split(strings.TrimSpace(argAndType), " ")[0])
}
prec.GoArgNames = strings.Join(argNames, ", ")
generatePrecEnum(w, prec)
}
for _, tp := range types {
Expand Down
59 changes: 0 additions & 59 deletions common/dynamicconfig/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ import (

"github.com/mitchellh/mapstructure"

enumspb "go.temporal.io/api/enums/v1"

enumsspb "go.temporal.io/server/api/enums/v1"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/log/tag"
"go.temporal.io/server/common/primitives/timestamp"
Expand Down Expand Up @@ -164,62 +161,6 @@ func matchAndConvert[T any](
return typedVal
}

func precedenceGlobal() []Constraints {
return []Constraints{
{},
}
}

func precedenceNamespace(namespace string) []Constraints {
return []Constraints{
{Namespace: namespace},
{},
}
}

func precedenceNamespaceID(namespaceID string) []Constraints {
return []Constraints{
{NamespaceID: namespaceID},
{},
}
}

func precedenceTaskQueue(namespace string, taskQueue string, taskType enumspb.TaskQueueType) []Constraints {
return []Constraints{
{Namespace: namespace, TaskQueueName: taskQueue, TaskQueueType: taskType},
{Namespace: namespace, TaskQueueName: taskQueue},
// A task-queue-name-only filter applies to a single task queue name across all
// namespaces, with higher precedence than a namespace-only filter. This is intended to
// be used by defaultNumTaskQueuePartitions and is probably not useful otherwise.
{TaskQueueName: taskQueue},
{Namespace: namespace},
{},
}
}

func precedenceDestination(namespace string, destination string) []Constraints {
return []Constraints{
{Namespace: namespace, Destination: destination},
{Destination: destination},
{Namespace: namespace},
{},
}
}

func precedenceShardID(shardID int32) []Constraints {
return []Constraints{
{ShardID: shardID},
{},
}
}

func precedenceTaskType(taskType enumsspb.TaskType) []Constraints {
return []Constraints{
{TaskType: taskType},
{},
}
}

func convertInt(val any) (int, error) {
switch val := val.(type) {
case int:
Expand Down
32 changes: 25 additions & 7 deletions common/dynamicconfig/setting_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7779fd7

Please sign in to comment.