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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Squash allocations in selector parsing and other hotspots #9564

Merged
merged 5 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
8 changes: 6 additions & 2 deletions felix/calc/active_rules_calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ func (arc *ActiveRulesCalculator) OnUpdate(update api.Update) (_ bool) {
rules := update.Value.(*model.ProfileRules)
oldRules, _ := arc.allProfileRules.Get(key.Name)
if reflect.DeepEqual(oldRules, rules) {
log.WithField("key", update.Key).Debug("No-op profile change; ignoring.")
if log.IsLevelEnabled(log.DebugLevel) {
log.WithField("key", update.Key).Debug("No-op profile change; ignoring.")
}
return
}
arc.allProfileRules.Set(key.Name, rules)
Expand Down Expand Up @@ -198,7 +200,9 @@ func (arc *ActiveRulesCalculator) OnUpdate(update api.Update) (_ bool) {
log.Debugf("Updating ARC for policy %v", key)
policy := update.Value.(*model.Policy)
if reflect.DeepEqual(oldPolicy, policy) {
log.WithField("key", update.Key).Debug("No-op policy change; ignoring.")
if log.IsLevelEnabled(log.DebugLevel) {
log.WithField("key", update.Key).Debug("No-op policy change; ignoring.")
}
return
}
arc.allPolicies.Set(key, policy)
Expand Down
17 changes: 13 additions & 4 deletions felix/calc/validation_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@ func NewValidationFilter(sink api.SyncerCallbacks, felixConfig *config.Config) *
return &ValidationFilter{
sink: sink,
config: felixConfig,

cachedKVLogEntry: logrus.WithFields(logrus.Fields{
"key": "",
"value": "",
}),
}
}

type ValidationFilter struct {
sink api.SyncerCallbacks
config *config.Config

cachedKVLogEntry *logrus.Entry
}

func (v *ValidationFilter) OnStatusUpdated(status api.SyncStatus) {
Expand All @@ -47,10 +54,12 @@ func (v *ValidationFilter) OnStatusUpdated(status api.SyncStatus) {
func (v *ValidationFilter) OnUpdates(updates []api.Update) {
filteredUpdates := make([]api.Update, len(updates))
for i, update := range updates {
logCxt := logrus.WithFields(logrus.Fields{
"key": update.Key,
"value": update.Value,
})
// WithFields allocates a lot so we re-use a cached log entry for these
// logs.
logCxt := v.cachedKVLogEntry
v.cachedKVLogEntry.Data["key"] = update.Key
v.cachedKVLogEntry.Data["value"] = update.Value

logCxt.Debug("Validating KV pair.")
validatorFunc := v1v.Validate
if _, isV3 := update.Key.(model.ResourceKey); isV3 {
Expand Down
4 changes: 3 additions & 1 deletion felix/dataplane/linux/int_dataplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -2043,7 +2043,9 @@ func (d *InternalDataplane) onDatastoreMessage(msg interface{}) {
}

func (d *InternalDataplane) processMsgFromCalcGraph(msg interface{}) {
log.WithField("msg", proto.MsgStringer{Msg: msg}).Infof("Received %T update from calculation graph", msg)
if log.IsLevelEnabled(log.InfoLevel) {
log.Infof("Received %T update from calculation graph. msg=%s", msg, proto.MsgStringer{Msg: msg}.String())
}
d.datastoreBatchSize++
d.dataplaneNeedsSync = true
d.recordMsgStat(msg)
Expand Down
5 changes: 4 additions & 1 deletion felix/iptables/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,8 +675,11 @@ func (t *Table) loadDataplaneState() {

// Check that the rules we think we've programmed are still there and mark any inconsistent
// chains for refresh.
logCxt := t.logCxt.WithField("chainName", "")
for chainName, expectedHashes := range t.chainToDataplaneHashes {
logCxt := t.logCxt.WithField("chainName", chainName)
// Re-using one logrus.Entry to reduce allocations.
logCxt.Data["chainName"] = chainName

if t.dirtyChains.Contains(chainName) || t.dirtyInsertAppend.Contains(chainName) {
// Already an update pending for this chain; no point in flagging it as
// out-of-sync.
Expand Down
6 changes: 5 additions & 1 deletion libcalico-go/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ clean:
GENERATED_FILES:=./lib/apis/v3/zz_generated.deepcopy.go \
./lib/upgrade/migrator/clients/v1/k8s/custom/zz_generated.deepcopy.go \
./lib/apis/v3/generated.openapi.go \
./lib/apis/v1/generated.openapi.go
./lib/apis/v1/generated.openapi.go \
./lib/selector/tokenizer/kind_string.go

.PHONY: gen-files
## Force rebuild generated go utilities (e.g. deepcopy-gen) and generated files
Expand Down Expand Up @@ -97,6 +98,9 @@ gen-crds:
--output-pkg "$(PACKAGE_NAME)/lib/apis/v1" \
"$(PACKAGE_NAME)/lib/apis/v1"'

./lib/selector/tokenizer/kind_string.go: ./lib/selector/tokenizer/tokenizer.go
$(DOCKER_GO_BUILD) go generate ./lib/selector/tokenizer

###############################################################################
# Static checks
###############################################################################
Expand Down
Loading
Loading