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

ref: Improve frames tracker performance #4469

Merged
merged 4 commits into from
Oct 28, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Improvements

- Improve frames tracker performance (#4469)
- Log a warning when dropping envelopes due to rate-limiting (#4463)

## 8.39.0-beta.1
Expand Down
23 changes: 9 additions & 14 deletions Sources/Sentry/SentryFramesTracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,11 @@ - (void)displayLinkCallback

- (void)reportNewFrame
{
NSArray *localListeners;
@synchronized(self.listeners) {
localListeners = [self.listeners allObjects];
}

NSDate *newFrameDate = [self.dateProvider date];

for (id<SentryFramesTrackerListener> listener in localListeners) {
// We need to copy the list because some listeners will remove themselves
// from the list during the callback, causing a crash during iteration.
NSArray<id<SentryFramesTrackerListener>> *listeners = [self.listeners copy];
for (id<SentryFramesTrackerListener> listener in listeners) {
[listener framesTrackerHasNewFrame:newFrameDate];
}
}
Expand Down Expand Up @@ -310,17 +307,15 @@ - (SentryFramesDelayResult *)getFramesDelay:(uint64_t)startSystemTimestamp

- (void)addListener:(id<SentryFramesTrackerListener>)listener
{

@synchronized(self.listeners) {
[self.listeners addObject:listener];
}
// Adding listeners on the main thread to avoid race condition with new frame callback
[self.dispatchQueueWrapper dispatchAsyncOnMainQueue:^{ [self.listeners addObject:listener]; }];
}

- (void)removeListener:(id<SentryFramesTrackerListener>)listener
{
@synchronized(self.listeners) {
[self.listeners removeObject:listener];
}
// Removing listeners on the main thread to avoid race condition with new frame callback
[self.dispatchQueueWrapper
dispatchAsyncOnMainQueue:^{ [self.listeners removeObject:listener]; }];
}

- (void)pause
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class SentryFramesTrackerTests: XCTestCase {
}

lazy var sut: SentryFramesTracker = SentryFramesTracker(displayLinkWrapper: displayLinkWrapper, dateProvider: dateProvider, dispatchQueueWrapper: SentryDispatchQueueWrapper(), notificationCenter: notificationCenter, keepDelayedFramesDuration: keepDelayedFramesDuration)

}

private var fixture: Fixture!
Expand Down Expand Up @@ -602,6 +603,16 @@ class SentryFramesTrackerTests: XCTestCase {
XCTAssertEqual(listener2.newFrameInvocations.count, 1)
XCTAssertEqual(listener2.newFrameInvocations.first?.timeIntervalSince1970, expectedFrameDate.timeIntervalSince1970)
}

func testListenerAreAddedInMainThread() {
let dispatchQueueWrapper = TestSentryDispatchQueueWrapper()
let sut = SentryFramesTracker(displayLinkWrapper: fixture.displayLinkWrapper, dateProvider: fixture.dateProvider, dispatchQueueWrapper: dispatchQueueWrapper, notificationCenter: fixture.notificationCenter, keepDelayedFramesDuration: fixture.keepDelayedFramesDuration)
let listener = FrameTrackerListener()

sut.add(listener)

XCTAssertEqual(dispatchQueueWrapper.blockOnMainInvocations.count, 1)
}

func testRemoveListener() {
let sut = fixture.sut
Expand Down
Loading