Skip to content

Avoid RTPBuffer allocation - #390

Merged
arjunshajitech merged 1 commit into
pion:mainfrom
arjunshajitech:fix-new-interceptor
Sep 8, 2026
Merged

Avoid RTPBuffer allocation#390
arjunshajitech merged 1 commit into
pion:mainfrom
arjunshajitech:fix-new-interceptor

Conversation

@arjunshajitech

Copy link
Copy Markdown
Contributor

Description

Previously, NewInterceptor validated the RTP buffer size by calling rtpbuffer.NewRTPBuffer
this resulted in unnecessary memory allocations during interceptor construction, even though
the buffer itself was not used at that stage.

Reference issue

Fixes #...

@codecov

codecov Bot commented Dec 17, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 71.42857% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.33%. Comparing base (a0af02f) to head (8e2a58f).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
internal/rtpbuffer/rtpbuffer.go 0.00% 1 Missing and 1 partial ⚠️
pkg/nack/receive_log.go 0.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #390      +/-   ##
==========================================
- Coverage   81.52%   81.33%   -0.20%     
==========================================
  Files          88       89       +1     
  Lines        4656     4650       -6     
==========================================
- Hits         3796     3782      -14     
- Misses        684      688       +4     
- Partials      176      180       +4     
Flag Coverage Δ
go 81.33% <71.42%> (-0.20%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@JoTurk JoTurk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

cool change, thank you.

Comment thread internal/rtpbuffer/rtpbuffer.go Outdated
Comment on lines 39 to 47
@@ -36,15 +46,11 @@ func NewRTPBuffer(size uint16) (*RTPBuffer, error) {
}
allowedSizes = append(allowedSizes, 1<<i)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe we can also avoid allocating allowedSizes every time we call IsBufferSizeValid. Maybe just on the error path?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Nice! fixed.

Comment thread internal/rtpbuffer/rtpbuffer.go Outdated
}, nil
}

func IsBufferSizeValid(size uint16) (bool, error) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does this need to be public and why we can't just return error, bool seems to be redundant here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Although returning an error would be sufficient, the function must be public because it is called from the NACK packet code in the RTP buffer.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Makes sense, thank you.

Comment thread pkg/nack/receive_log.go Outdated

if !correctSize {
return nil, fmt.Errorf("%w: %d is not a valid size, allowed sizes: %v", ErrInvalidSize, size, allowedSizes)
if err := rtpbuffer.IsBufferSizeValid(size); err != nil {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This now uses rtpbuffer.IsBufferSizeValid to check if we can create a receiveLog. What if we ever change the conditions for only one of them? Could we rename the IsBufferSizeValid function to IsPowerOfTwo and move it out of the rtpbuffer package? That way, it would be clear what it does, and if we change the conditions of either receiveLog or RTPBuffer, it would not affect the other.

@arjunshajitech arjunshajitech Sep 8, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@mengelbart I’ve updated it to use a separate IsPowerOfTwo helper instead of rtpbuffer.IsBufferSizeValid

Previously, NewInterceptor validated the RTP
buffer size by calling rtpbuffer.NewRTPBuffer
this resulted in unnecessary memory allocations
during interceptor construction, even though
the buffer itself was not used at that stage.
@arjunshajitech
arjunshajitech merged commit 3783f4d into pion:main Sep 8, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants