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

add GetAndSet to bitmap #942

Merged
merged 4 commits into from
Jan 11, 2025
Merged

add GetAndSet to bitmap #942

merged 4 commits into from
Jan 11, 2025

Conversation

paulwe
Copy link
Contributor

@paulwe paulwe commented Jan 11, 2025

precomputing the mask seems to be a very minor pessimization but we can skip one call to getSlotAndOffset in (*RTPStatsReceiver).Update by combining get/set into one method.

also some other driveby cleanup

goos: darwin
goarch: arm64
pkg: github.com/livekit/protocol/utils
cpu: Apple M1 Pro
BenchmarkBitmap
BenchmarkBitmap/Set
BenchmarkBitmap/Set-10 584310650 2.055 ns/op
BenchmarkBitmap/Set-10 586064010 2.049 ns/op
BenchmarkBitmap/Set-10 582722875 2.057 ns/op
BenchmarkBitmap/Set-10 585069157 2.063 ns/op
BenchmarkBitmap/Set-10 566389279 2.063 ns/op
PASS
ok github.com/livekit/protocol/utils 15.692s

goos: darwin
goarch: arm64
pkg: github.com/livekit/protocol/utils
cpu: Apple M1 Pro
BenchmarkBitmap
BenchmarkBitmap/Set
BenchmarkBitmap/Set-10 591135066 2.030 ns/op
BenchmarkBitmap/Set-10 592524807 2.055 ns/op
BenchmarkBitmap/Set-10 585467359 2.022 ns/op
BenchmarkBitmap/Set-10 591682300 2.024 ns/op
BenchmarkBitmap/Set-10 593558163 2.027 ns/op

@paulwe paulwe requested a review from boks1971 January 11, 2025 18:47
Copy link

changeset-bot bot commented Jan 11, 2025

⚠️ No Changeset found

Latest commit: 060682a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

💥 An error occurred when fetching the changed packages and changesets in this PR
Some errors occurred when validating the changesets config:
The package or glob expression "github.com/livekit/protocol" specified in the `fixed` option does not match any package in the project. You may have misspelled the package name or provided an invalid glob expression. Note that glob expressions must be defined according to https://www.npmjs.com/package/micromatch.

Copy link
Contributor

@boks1971 boks1971 left a comment

Choose a reason for hiding this comment

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

ouch, that was a pessimisation, did not expect that.

@paulwe
Copy link
Contributor Author

paulwe commented Jan 11, 2025

i'm not sure this will have any effect either way. Set has higher cpu use than IsSet which runs unconditionally. maybe the cost is dominated by some cache operation...
Screenshot 2025-01-11 at 10 53 06 AM

Screenshot 2025-01-11 at 10 52 08 AM

in a microbenchmark the extra call has no impact

BenchmarkBitmap
BenchmarkBitmap/IsSet/Set
BenchmarkBitmap/IsSet/Set-10 544463373 2.092 ns/op
BenchmarkBitmap/IsSet/Set-10 583395589 2.140 ns/op
BenchmarkBitmap/IsSet/Set-10 590237834 2.048 ns/op
BenchmarkBitmap/IsSet/Set-10 589507502 2.046 ns/op
BenchmarkBitmap/IsSet/Set-10 587347552 2.043 ns/op
BenchmarkBitmap/GetAndSet
BenchmarkBitmap/GetAndSet-10 589057398 2.048 ns/op
BenchmarkBitmap/GetAndSet-10 592168935 2.044 ns/op
BenchmarkBitmap/GetAndSet-10 591168433 2.042 ns/op
BenchmarkBitmap/GetAndSet-10 592299975 2.034 ns/op
BenchmarkBitmap/GetAndSet-10 592091506 2.040 ns/op

@boks1971
Copy link
Contributor

i'm not sure this will have any effect either way. Set has higher cpu use than IsSet which runs unconditionally. maybe the cost is dominated by some cache operation...

IsSet runs only for out-of-order packets. That's why it must be smaller.

@paulwe paulwe merged commit 7b5c5cd into main Jan 11, 2025
3 checks passed
@paulwe paulwe deleted the improve-bitmap branch January 11, 2025 19:13
@boks1971
Copy link
Contributor

ClearRangeAndSet might have a bigger impact if it can be optimised as it runs for all in-order packets. ClearRange will be a no-op unless there are gaps - https://github.com/livekit/livekit/blob/28c39efa06c980d9cfa9c110b26b7b721d2503fe/pkg/sfu/rtpstats/rtpstats_receiver.go#L325

@paulwe
Copy link
Contributor Author

paulwe commented Jan 11, 2025

inlining doesn't seem to have an effect either 🫤

@paulwe
Copy link
Contributor Author

paulwe commented Jan 11, 2025

ClearRangeAndSet might have a bigger impact if it can be optimised as it runs for all in-order packets. ClearRange will be a no-op unless there are gaps - https://github.com/livekit/livekit/blob/28c39efa06c980d9cfa9c110b26b7b721d2503fe/pkg/sfu/rtpstats/rtpstats_receiver.go#L325

these don't use the same bitmap indices so i don't see anything to eliminate by merging them

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