-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce allocations to store config on context in Activator (#11013)
* Reduce allocations to store config on context in Activator The current store implementation has 5 allocations in its HTTP Middleware - 2 in the call for `r.WithContext(ctx)` - 1 to allocate the new value context - 1 to allocate a new *Config - 1 to load the untyped config This reduces the footprint of the store to just one allocation by 1. Storing the intermediate Config until it needs to be updated. 2. Collapsing `r.WithContext(ctx)` calls with the context handler. * Fix optical illusion
- Loading branch information
1 parent
1a0d326
commit d5d489c
Showing
6 changed files
with
126 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
Copyright 2021 The Knative Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package config | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
ltesting "knative.dev/pkg/logging/testing" | ||
tracingconfig "knative.dev/pkg/tracing/config" | ||
) | ||
|
||
var tracingConfig = &corev1.ConfigMap{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "knative-serving", | ||
Name: "config-tracing", | ||
}, | ||
Data: map[string]string{ | ||
"backend": "none", | ||
}, | ||
} | ||
|
||
func TestStore(t *testing.T) { | ||
logger := ltesting.TestLogger(t) | ||
store := NewStore(logger) | ||
store.OnConfigChanged(tracingConfig) | ||
|
||
ctx := store.ToContext(context.Background()) | ||
cfg := FromContext(ctx) | ||
|
||
if got, want := cfg.Tracing.Backend, tracingconfig.None; got != want { | ||
t.Fatalf("Tracing.Backend = %v, want %v", got, want) | ||
} | ||
|
||
newConfig := &corev1.ConfigMap{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "knative-serving", | ||
Name: "config-tracing", | ||
}, | ||
Data: map[string]string{ | ||
"backend": "zipkin", | ||
"zipkin-endpoint": "foo.bar", | ||
}, | ||
} | ||
store.OnConfigChanged(newConfig) | ||
|
||
ctx = store.ToContext(context.Background()) | ||
cfg = FromContext(ctx) | ||
|
||
if got, want := cfg.Tracing.Backend, tracingconfig.Zipkin; got != want { | ||
t.Fatalf("Tracing.Backend = %v, want %v", got, want) | ||
} | ||
} | ||
|
||
func BenchmarkStoreToContext(b *testing.B) { | ||
logger := ltesting.TestLogger(b) | ||
store := NewStore(logger) | ||
store.OnConfigChanged(tracingConfig) | ||
|
||
b.Run("sequential", func(b *testing.B) { | ||
for j := 0; j < b.N; j++ { | ||
store.ToContext(context.Background()) | ||
} | ||
}) | ||
|
||
b.Run("parallel", func(b *testing.B) { | ||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
store.ToContext(context.Background()) | ||
} | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters