Skip to content

Commit

Permalink
chore: more performance improvements (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto authored Aug 16, 2024
1 parent e878c64 commit 1d20443
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Next

- recording: improve ios session recording performance by avoiding redrawing after screen updates ([#166](https://github.com/PostHog/posthog-ios/pull/166))
- `debouncerDelay` is changed from 500ms to 1s since the iOS screenshot has to be taken in the main thread and its more sensitive to performance issues

## 3.7.1 - 2024-08-13

- recording: improve ios session recording performance by doing some work off of the main thread ([#158](https://github.com/PostHog/posthog-ios/pull/158))
Expand Down
3 changes: 0 additions & 3 deletions PostHog/Replay/PostHogReplayIntegration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,6 @@
}

@objc private func snapshot() {
// TODO: add debouncer with debouncerDelayMs to take into account how long it takes to execute the
// snapshot method

if !PostHogSDK.shared.isSessionReplayActive() {
return
}
Expand Down
4 changes: 2 additions & 2 deletions PostHog/Replay/PostHogSessionReplayConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
/// Deboucer delay used to reduce the number of snapshots captured and reduce performance impact
/// This is used for capturing the view as a wireframe or screenshot
/// The lower the number more snapshots will be captured but higher the performance impact
/// Defaults to 500ms
@objc public var debouncerDelay: TimeInterval = 1.0 / 2.0
/// Defaults to 1s
@objc public var debouncerDelay: TimeInterval = 1.0

// TODO: sessionRecording config such as networkPayloadCapture, captureConsoleLogs, sampleRate, etc
}
Expand Down
18 changes: 13 additions & 5 deletions PostHog/Replay/UIView+Util.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,25 @@
}

func toImage() -> UIImage? {
let size = bounds.size
// Avoid Rendering Offscreen Views
let bounds = superview?.bounds ?? bounds
let size = bounds.intersection(bounds).size

if !size.hasSize() {
return nil
}

let renderer = UIGraphicsImageRenderer(size: size)
let rendererFormat = UIGraphicsImageRendererFormat.default()

let image = renderer.image { context in
// Render the view's layer into the current context
layer.render(in: context.cgContext)
// This can significantly improve rendering performance because the renderer won't need to
// process transparency.
rendererFormat.opaque = isOpaque
// Another way to improve rendering performance is to scale the renderer's content.
// rendererFormat.scale = 0.5
let renderer = UIGraphicsImageRenderer(size: size, format: rendererFormat)

let image = renderer.image { _ in
drawHierarchy(in: bounds, afterScreenUpdates: false)
}

return image
Expand Down

0 comments on commit 1d20443

Please sign in to comment.