Skip to content

Perf: Optimize Screenshot diff check#271

Merged
marandaneto merged 5 commits intoPostHog:mainfrom
jeremiahseun:perf/optimize-screenshot-diff-check
Feb 2, 2026
Merged

Perf: Optimize Screenshot diff check#271
marandaneto merged 5 commits intoPostHog:mainfrom
jeremiahseun:perf/optimize-screenshot-diff-check

Conversation

@jeremiahseun
Copy link
Contributor

💡 Motivation and Context

This PR optimises the Session Replay screenshot capturing logic to reduce CPU usage and improve performance.
Previously, we were encoding the screen snapshot to PNG format before checking if the screen had actually changed. PNG encoding is a computationally expensive operation.
The Fix:
I switched the "diff check" logic to use ui.ImageByteFormat.rawRgba instead of PNG. The Raw RGBA format is significantly faster to generate as it requires no compression. We now only proceed to the expensive PNG encoding step after we have confirmed that the new frame is different from the previous one.
This change avoids unnecessary PNG encoding cycles when the UI is static, leading to smoother app performance during recording.

💚 How did you test it?

I added two new test files to verify both the performance gain and the correctness of the implementation:

  1. test/benchmark_screenshot_performance_test.dart: A benchmark test that compares the speed of "PNG Encoding + Compare" vs "Raw RGBA Encoding + Compare".
    • Result: Confirmed a significant speedup factor (e.g., ~15-20x faster) for the diffing step.
  2. test/validation_screenshot_diff_test.dart: A validation test that ensures rawRgba bytes are identical for identical images and different for distinct images.
    • Result: Verified that the optimisation does not introduce false positives or negatives in change detection.

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

Screenshots

Screenshot 2026-01-29 at 14 40 24

Replaces PNG encoding with raw RGBA bytes for the screenshot change detection.
This avoids expensive PNG compression when checking if the screen has changed.
Introduces benchmark and validation tests for screenshot encoding and comparison. The benchmark test compares PNG and raw RGBA encoding speeds, while the validation test ensures raw RGBA bytes can accurately detect image differences.
@jeremiahseun jeremiahseun requested a review from a team January 29, 2026 13:42
@jeremiahseun jeremiahseun changed the title Perf/optimize screenshot diff check Perf: Optimize Screenshot diff check Jan 29, 2026
@marandaneto
Copy link
Member

@jeremiahseun mind adding a changelog entry under next? https://github.com/PostHog/posthog-flutter/blob/main/CHANGELOG.md#next

Copy link
Member

@marandaneto marandaneto left a comment

Choose a reason for hiding this comment

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

thanks @jeremiahseun

@jeremiahseun
Copy link
Contributor Author

thanks @jeremiahseun

Sure @marandaneto
Should I leave those test files or I should remove them?
test/benchmark_screenshot_performance_test.dart
test/validation_screenshot_diff_test.dart

@marandaneto
Copy link
Member

thanks @jeremiahseun

Sure @marandaneto Should I leave those test files or I should remove them? test/benchmark_screenshot_performance_test.dart test/validation_screenshot_diff_test.dart

i think you can remove both

@jeremiahseun
Copy link
Contributor Author

@marandaneto I have updated the CHANGELOG and removed the test files.

@marandaneto marandaneto enabled auto-merge (squash) February 2, 2026 13:51
@marandaneto marandaneto merged commit d475dc0 into PostHog:main Feb 2, 2026
16 checks passed
@jeremiahseun jeremiahseun deleted the perf/optimize-screenshot-diff-check branch February 2, 2026 17:23
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