From bba4345b0fff4c3b93e0802cd1a722a17e674343 Mon Sep 17 00:00:00 2001 From: Laurence Date: Sun, 16 Nov 2025 08:40:26 +0000 Subject: [PATCH 1/2] main: optimize calculatePeerBandwidth to avoid nested peer scans Build a set of current peer public keys during the primary iteration and prune lastReadings in a single pass, removing the O(n^2) nested loop. No behavior change; improves efficiency when peer lists and lastReadings grow large. --- main.go | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 7a99c4d..71f5b19 100644 --- a/main.go +++ b/main.go @@ -639,7 +639,7 @@ func ensureMSSClamping() error { if out, err := addCmd.CombinedOutput(); err != nil { errMsg := fmt.Sprintf("Failed to add MSS clamping rule for chain %s: %v (output: %s)", chain, err, string(out)) - logger.Error(errMsg) + logger.Error("%s", errMsg) errors = append(errors, fmt.Errorf("%s", errMsg)) continue } @@ -656,7 +656,7 @@ func ensureMSSClamping() error { if out, err := checkCmd.CombinedOutput(); err != nil { errMsg := fmt.Sprintf("Rule verification failed for chain %s: %v (output: %s)", chain, err, string(out)) - logger.Error(errMsg) + logger.Error("%s", errMsg) errors = append(errors, fmt.Errorf("%s", errMsg)) continue } @@ -1003,8 +1003,13 @@ func calculatePeerBandwidth() ([]PeerBandwidth, error) { mu.Lock() defer mu.Unlock() + // Track the set of peers currently present on the device to prune stale readings efficiently + currentPeerKeys := make(map[string]struct{}, len(device.Peers)) + for _, peer := range device.Peers { publicKey := peer.PublicKey.String() + currentPeerKeys[publicKey] = struct{}{} + currentReading := PeerReading{ BytesReceived: peer.ReceiveBytes, BytesTransmitted: peer.TransmitBytes, @@ -1061,14 +1066,7 @@ func calculatePeerBandwidth() ([]PeerBandwidth, error) { // Clean up old peers for publicKey := range lastReadings { - found := false - for _, peer := range device.Peers { - if peer.PublicKey.String() == publicKey { - found = true - break - } - } - if !found { + if _, exists := currentPeerKeys[publicKey]; !exists { delete(lastReadings, publicKey) } } From 971452e5d35dbc6a23f15a7f6e82d0bdc0c87e7c Mon Sep 17 00:00:00 2001 From: Laurence Date: Sun, 16 Nov 2025 08:42:57 +0000 Subject: [PATCH 2/2] revert: drop logger formatting changes from calcpeerbandwidth optimization branch --- main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 71f5b19..c8578cd 100644 --- a/main.go +++ b/main.go @@ -639,7 +639,7 @@ func ensureMSSClamping() error { if out, err := addCmd.CombinedOutput(); err != nil { errMsg := fmt.Sprintf("Failed to add MSS clamping rule for chain %s: %v (output: %s)", chain, err, string(out)) - logger.Error("%s", errMsg) + logger.Error(errMsg) errors = append(errors, fmt.Errorf("%s", errMsg)) continue } @@ -656,7 +656,7 @@ func ensureMSSClamping() error { if out, err := checkCmd.CombinedOutput(); err != nil { errMsg := fmt.Sprintf("Rule verification failed for chain %s: %v (output: %s)", chain, err, string(out)) - logger.Error("%s", errMsg) + logger.Error(errMsg) errors = append(errors, fmt.Errorf("%s", errMsg)) continue }