Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@
## 2026-06-09 - Avoid string allocations in tight loops
**Learning:** Using string manipulation functions like `strings.Split` and `strings.Join` inside O(n) loops (e.g., building graphs from large reports) causes massive and unnecessary allocation overhead (10-20 memory allocations per host due to slice/string buffers).
**Action:** When extracting a specific part of a string (like a subnet from an IP address), prefer zero-allocation byte indexing functions (`strings.LastIndexByte`) and simple string slicing (`string[:index]`) instead of `Split` + `Join` to prevent GC bottlenecks.

## 2024-06-10 - O(n) String Slicing Over Allocation
**Learning:** Using `strings.Split` and `strings.Join` for simple string formatting checks (like checking if an OUI matches by splitting and re-joining MAC parts) inside tight loops creates excessive memory allocations and GC pressure.
**Action:** When verifying simple structure formats or extracting fixed-width prefixes from strings (e.g. MAC address OUI prefixes), prefer zero-allocation byte indexing or manual character checking (`mac[2] == ':' && mac[5] == ':'`) over `strings.Split` and `strings.Join`.
8 changes: 5 additions & 3 deletions pkg/fingerprint/oui.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ var ouiMap = map[string]string{
}

// VendorFromMAC returns the vendor name from the MAC address using a built-in OUI map.
// ⚑ Bolt Optimization: Replaced strings.Split and strings.Join with zero-allocation string slicing
// to prevent GC bottlenecks in high-frequency loops.
func VendorFromMAC(mac string) string {
mac = strings.ToUpper(strings.TrimSpace(mac))
mac = strings.ReplaceAll(mac, "-", ":")
parts := strings.Split(mac, ":")
if len(parts) >= 3 {
prefix := strings.Join(parts[:3], ":")

if len(mac) >= 8 && mac[2] == ':' && mac[5] == ':' {
prefix := mac[:8]
Comment on lines +35 to +36

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current zero-allocation optimization has a functional discrepancy compared to the original strings.Split implementation when handling malformed MAC addresses.

Specifically, if a MAC address has a third octet longer than 2 characters (e.g., B8:27:EB123), the original code would split it into ["B8", "27", "EB123"] and join the first 3 parts to get "B8:27:EB123" (which does not match any OUI). However, the new implementation only checks mac[2] == ':' && mac[5] == ':' and then slices mac[:8], which yields "B8:27:EB" and incorrectly matches the OUI.

To prevent false positives on malformed MAC addresses, we should ensure that if the MAC address is longer than 8 characters, the character at index 8 is a separator (:).

Suggested change
if len(mac) >= 8 && mac[2] == ':' && mac[5] == ':' {
prefix := mac[:8]
if len(mac) >= 8 && mac[2] == ':' && mac[5] == ':' && (len(mac) == 8 || mac[8] == ':') {
prefix := mac[:8]

if vendor, ok := ouiMap[prefix]; ok {
return vendor
}
Expand Down
Loading