Skip to content

[video_player] Optimize caption retrieval with binary search in VideoPlayerController#8347

Merged
auto-submit[bot] merged 36 commits intoflutter:mainfrom
abdelaziz-mahdy:binary-search-captions
Mar 10, 2026
Merged

[video_player] Optimize caption retrieval with binary search in VideoPlayerController#8347
auto-submit[bot] merged 36 commits intoflutter:mainfrom
abdelaziz-mahdy:binary-search-captions

Conversation

@abdelaziz-mahdy
Copy link
Contributor

Just found the todo in the code and it was from 3 years ago, so i wanted to implement it since it may help people using captions to have more optimized code

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group.

@tarrinneal
Copy link
Contributor

@stuartmorgan does this change need a test? It shouldn't be changing any behaviors, I'm not sure how a test would work honestly.

@stuartmorgan-g
Copy link
Collaborator

@stuartmorgan does this change need a test? It shouldn't be changing any behaviors, I'm not sure how a test would work honestly.

The fact that it's a more efficient search doesn't inherently need a test; that's essentially just a refactoring. However, this seems like a good opportunity to make sure we have reasonably robust coverage of caption correctness. E.g., is there a unit test that would fail if the initial sort were commented out?

@abdelaziz-mahdy
Copy link
Contributor Author

@stuartmorgan does this change need a test? It shouldn't be changing any behaviors, I'm not sure how a test would work honestly.

The fact that it's a more efficient search doesn't inherently need a test; that's essentially just a refactoring. However, this seems like a good opportunity to make sure we have reasonably robust coverage of caption correctness. E.g., is there a unit test that would fail if the initial sort were commented out?

I can add that , so in general more caption edge cases

@abdelaziz-mahdy
Copy link
Contributor Author

abdelaziz-mahdy commented Dec 27, 2024

I just checked the test for sorting, and it shows that the captions are not sorted. After rechecking the code, I realized that the captions are implemented as a getter, which means it’s a read-only operation.

I have two solutions in mind:

1- Modify the public-facing controller by adding a getter to retrieve the sorted captions. However, based on my observations, it’s not recommended to change the interface, so this solution might not be valid.

2- Ensure that all ClosedCaptionFile classes return sorted captions. I will implement this solution for now, and if the public-facing approach turns out to be better, we can always revert to it.

Edit: fix number 2 is not clean since it doesnt enforce the sorting so i am going with the first fix

@abdelaziz-mahdy
Copy link
Contributor Author

tests implemented and improved the logic for the binary search since it was incorrect in some cases

for this test test('captions are sorted correctly on initialization'
this test can be ignored and if thats the case we can remove the new public member to the class (my main issue is that i was not able to expose a private member only for testing) if someone can point me to the right direction that will help a lot

@stuartmorgan-g
Copy link
Collaborator

From triage: what's the status of this PR? Is it ready for re-review?

@abdelaziz-mahdy
Copy link
Contributor Author

From triage: what's the status of this PR? Is it ready for re-review?

Yes it's ready for a re-review

Copy link
Contributor

@tarrinneal tarrinneal left a comment

Choose a reason for hiding this comment

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

Changelog doesn't match our guidelines. And a tip with the failing tests.

Seems good to me besides.

@abdelaziz-mahdy
Copy link
Contributor Author

I switched the logic to use whenComplete to only call the sort once on the future completion, I hope that matches what you meant, if not I currently don't know how to solve the event listener call to avoid resorting on every event fired,

the plan in my mind currently to add a flag so I know if the current file was sorted or not, but don't know if its good or not.

@nateshmbhat

This comment was marked as off-topic.

@stuartmorgan-g
Copy link
Collaborator

@abdelaziz-mahdy Are you still planning on updating this PR to address the review comments above?

@abdelaziz-mahdy
Copy link
Contributor Author

abdelaziz-mahdy commented Jul 29, 2025

@abdelaziz-mahdy Are you still planning on updating this PR to address the review comments above?

Sadly I have been quite busy lately, and not easy to figure out, so I cant fix it yet

@stuartmorgan-g stuartmorgan-g marked this pull request as draft August 26, 2025 18:23
@stuartmorgan-g
Copy link
Collaborator

Marking as a draft pending updates.

@Piinks
Copy link
Contributor

Piinks commented Nov 3, 2025

Greetings from stale PR triage! 👋
Is this change still on your radar?

@abdelaziz-mahdy
Copy link
Contributor Author

abdelaziz-mahdy commented Nov 23, 2025

Review comments addressed. Ready for re-review.

Changes Made:

  1. Optimized caption lookup
  • Added local variable in _getCaptionAt to avoid repeated force-unwraps
  • Removed unnecessary validity check after binary search (compare function guarantees correctness)
  1. Fixed caching strategy
  • Refactored setClosedCaptionFile to always sort when explicitly setting a new file
  • Updated _updateClosedCaptionWithFuture to only sort on first initialization (_sortedCaptions == null)
  • Prevents expensive re-sorting on every event listener call
  1. Fixed test structure
  • Unnested incorrectly placed tests
  1. Added edge case tests

Added 5 new tests covering binary search edge cases:

  • Exact caption start/end time boundaries
  • Gaps between captions
  • Single caption scenario
  • Overlapping captions

The caching optimization ensures captions are only sorted when the file actually changes (initialization or explicit setClosedCaptionFile call), avoiding performance issues.

@abdelaziz-mahdy abdelaziz-mahdy marked this pull request as ready for review November 23, 2025 15:10
Copy link
Collaborator

@stuartmorgan-g stuartmorgan-g left a comment

Choose a reason for hiding this comment

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

LGTM, sorry for the delay in getting back to this.

@tarrinneal This just needs a final second review.

@tarrinneal
Copy link
Contributor

Sorry for the wait, this one slid through my process

@tarrinneal tarrinneal added the autosubmit Merge PR when tree becomes green via auto submit App label Mar 10, 2026
@auto-submit auto-submit bot merged commit 349d885 into flutter:main Mar 10, 2026
81 checks passed
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Mar 11, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Mar 11, 2026
github-merge-queue bot pushed a commit to flutter/flutter that referenced this pull request Mar 11, 2026
flutter/packages@ee460d6...ecace66

2026-03-11 [email protected] [pigeon] Produce a helpful error for bad
method return type (flutter/packages#11204)
2026-03-11 [email protected] [pigeon] Use hasLength and isEmpty in
tests for better failure messages (flutter/packages#11205)
2026-03-11 [email protected] [rfw] Opt out of icon tree shaking
(flutter/packages#11216)
2026-03-11 [email protected] [pigeon] Tidy imports and "ignore"
comments (flutter/packages#11149)
2026-03-11 49699333+dependabot[bot]@users.noreply.github.com
[dependabot]: Bump org.jetbrains.kotlin:kotlin-bom from 2.2.21 to 2.3.10
in /packages/pigeon/platform_tests/test_plugin/android
(flutter/packages#10984)
2026-03-11 [email protected] [pigeon] Improve casting and
nullability-handling in generated code (flutter/packages#11163)
2026-03-10 [email protected] Roll Flutter from
2ec61af to 195ae7b (36 revisions) (flutter/packages#11222)
2026-03-10 [email protected] [vector_graphics] Respect BoxFit parameter with
viewbox (flutter/packages#11012)
2026-03-10 [email protected] Add AI contribution guidelines to PR
checklist (flutter/packages#11195)
2026-03-10 [email protected] [video_player] Optimize caption
retrieval with binary search in VideoPlayerController
(flutter/packages#8347)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC [email protected] on the revert to ensure that a
human
is aware of the problem.

To file a bug in Flutter:
https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App p: video_player

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants