⚡ Reuse static regexes in extractVideoId - #1001
Merged
Merged
Conversation
* perf(paging): reuse static regexes in extractVideoId Move YouTube video ID extraction Regex compilation out of extractVideoId into a static companion object constant list to eliminate redundant pattern recompilation per extracted video item.
There was a problem hiding this comment.
🟢 Approval recommended
The change is low-risk, performance-motivated, and includes a focused unit test; only minor test-naming consistency feedback remains.
Pull request overview
This PR refactors YouTube video ID parsing in SearchPagingSource by hoisting the URL-matching Regex instances into a companion object list so they’re compiled once and reused, reducing per-item overhead during search result mapping.
Changes:
- Hoist
extractVideoIdregex patterns intoSearchPagingSource.Companion.VIDEO_ID_PATTERNSand reuse them across calls. - Move
extractVideoIdinto the companion object (internal) to support reuse and testing. - Add a unit test covering common YouTube URL formats (watch, youtu.be, shorts, embed).
File summaries
| File | Description |
|---|---|
| app/src/main/java/io/github/aedev/flow/data/paging/SearchPagingSource.kt | Reuses precompiled regex patterns for video ID extraction via companion object. |
| app/src/test/java/io/github/aedev/flow/data/paging/SearchPagingSourceTest.kt | Adds coverage for video ID extraction across common URL shapes. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Owner
|
@OshekharO PR merged, thanks for your contribution |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
💡 What:
Moved regex pattern instantiations from local scope inside
SearchPagingSource.extractVideoIdto staticRegexinstances declared inSearchPagingSource.Companion.VIDEO_ID_PATTERNS.🎯 Why:
Previously,
extractVideoIdcreated and compiled a list of three regular expressions (v=([A-Za-z0-9_-]{11}),youtu\.be/([A-Za-z0-9_-]{11}), andshorts/([A-Za-z0-9_-]{11})) every single time it was called for each search result item. Hoisting them to companion object constants prevents redundant pattern compilation overhead and unnecessary object allocations.📊 Measured Improvement:
Unit benchmark test comparing 10,000 iterations over 4 standard YouTube URL variants:
Change type
Validation
./gradlew :app:assembleGithubDebug./gradlew :app:testGithubDebugUnitTestTest device and Android version:
Risk and compatibility