Skip to content

perf: reduce heap allocations - #38

Merged
encodeous merged 1 commit into
mainfrom
ac/perf-1
Nov 9, 2025
Merged

encodeous merged 1 commit into
mainfrom
ac/perf-1

Conversation

@encodeous

@encodeous encodeous commented Nov 9, 2025 •

Copy link
Copy Markdown
Owner

This PR properly frees TCElement in the hot path, as well as reducing map allocations in the endpoint-based grouping logic.

@encodeous
encodeous force-pushed the ac/perf-1 branch 2 times, most recently from cc52d70 to c92e625 Compare November 9, 2025 16:03

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull Request Overview

This PR optimizes packet transmission by grouping packets by endpoint before sending, reducing the number of send operations while maintaining packet ordering within each endpoint group.

  • Introduced partitionBuffersByEndpoint function to group packets by destination endpoint while preserving stable ordering within groups
  • Refactored SendBuffers to leverage pre-partitioned, contiguous same-endpoint buffer sequences for more efficient batch sending
  • Fixed resource leak by properly releasing TCElement objects after transferring their contents to outbound elements

Reviewed Changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
polyamide/device/traffic_control.go Fixed import ordering per Go conventions and added proper cleanup of TCElement resources
polyamide/device/send.go Introduced endpoint partitioning logic to group packets by destination before transmission
polyamide/device/peer.go Optimized SendBuffers to send contiguous same-endpoint packets in batches, replacing map-based grouping
.gitignore Added workdir to ignore list
.dockerignore Added workdir and Dockerfile to ignore list

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread polyamide/device/send.go
Comment on lines +539 to +542
if err != nil {
device.Log.Errorf("%v - Failed to send data packets: %v", peer, err)
continue
}

Copilot AI Nov 9, 2025

Copy link

Choose a reason for hiding this comment

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

The lock acquired at line 526 via elemsContainer.Lock() is not released before the continue statement when partitionBuffersByEndpoint returns an error. This will cause a deadlock. The unlock should happen before continuing, or the cleanup code (lines 547-551) should be executed even on error.

Copilot uses AI. Check for mistakes.
Comment thread polyamide/device/send.go
Comment on lines +463 to +464
// If endpoint is nil, it has already been processed
// as part of an earlier group.

Copilot AI Nov 9, 2025

Copy link

Choose a reason for hiding this comment

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

The comment "If endpoint is nil, it has already been processed as part of an earlier group" is misleading. At this point in the code, all originally-nil endpoints have been replaced with peer.endpoints.val[0] in the previous loop (lines 451-459). The nil check here is only catching entries that were marked as visited (set to nil) during the current partitioning loop, not originally-nil entries.

Suggested change
// If endpoint is nil, it has already been processed
// as part of an earlier group.
// If endpoint is nil, it was marked as visited during this loop
// and has already been processed as part of an earlier group.

Copilot uses AI. Check for mistakes.
Comment thread polyamide/device/peer.go
}
// optimization, if multiple contiguous buffers share the same endpoint, send them in a single batch
prevIdx := 0
prevEp := eps[0]

Copilot AI Nov 9, 2025

Copy link

Choose a reason for hiding this comment

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

Potential panic if eps slice is empty. Line 149 accesses eps[0] without checking if the slice has any elements. While the current code flow may prevent empty slices from reaching this function, defensive programming would suggest adding a length check or early return for empty inputs.

Copilot uses AI. Check for mistakes.
@encodeous
encodeous merged commit 1cbed90 into main Nov 9, 2025
7 checks passed
@encodeous
encodeous deleted the ac/perf-1 branch September 5, 2026 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants