Skip to content

Add video renderer pool prewarming API #839

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
21 changes: 17 additions & 4 deletions Sources/StreamVideoSwiftUI/Utils/ReusePool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ final class ReusePool<Element: AnyObject & Hashable> {
self.initialCapacity = initialCapacity
self.factory = factory

// Initialize the pool with a set number of elements
for _ in 0..<initialCapacity {
let element = factory()
available.append(element)
// Initialize the pool with a set number of elements (thread-safe)
queue.sync {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm i'm not sure about this one. Even without the sync, there aren't multiple threads accessing the initialiser at the same (if they were they would be creating different instances).

for _ in 0..<initialCapacity {
let element = factory()
available.append(element)
}
}
}

Expand Down Expand Up @@ -94,4 +96,15 @@ final class ReusePool<Element: AnyObject & Hashable> {
}
}
}

/// Adds a pre-created element to the available pool.
/// Used for prewarming to add elements beyond initial capacity.
///
/// - Parameter element: The pre-created element to add to the pool.
func addToAvailable(_ element: Element) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This method defies the Pool's purpose. The pool is responsible for creating its elements on demand.

queue.sync {
available.append(element)
log.debug("Added prewarmed \(type(of: element)):\(String(describing: element)) to available pool.")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ final class VideoRendererPool: @unchecked Sendable {
func releaseRenderer(_ renderer: VideoRenderer) {
pool.release(renderer)
}

/// Configures the global video renderer pool with the specified initial capacity.
/// This method is intended for internal use by StreamVideo.
///
/// - Parameter initialCapacity: The number of video renderers to pre-create.
@MainActor
static func configure(initialCapacity: Int) async {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is what is happening on the reusePool initialisation. I don't see a reason for this method.

Copy link
Author

Choose a reason for hiding this comment

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

The default value is 0: https://github.com/GetStream/stream-video-swift/blob/develop/Sources/StreamVideoSwiftUI/Utils/VideoRendererPool/VideoRendererPool.swift#L20

This entire PR could be removed if that default value is changed, but I didn't want to change the default behavior for all users unexpectedly. So this PR is just a way to expose that number.

// Create pool with 0 initial capacity first
currentValue = VideoRendererPool(initialCapacity: 0)

// Then add renderers one by one with yielding
for _ in 0..<initialCapacity {
let renderer = VideoRenderer(frame: CGRect(origin: .zero, size: .zero))
currentValue.pool.addToAvailable(renderer)

// Yield to allow other main thread work between renderer creations
await Task.yield()
}
}
}

/// - Note: I have no other way of satisfying the compiler here.
Expand Down
29 changes: 29 additions & 0 deletions Sources/StreamVideoSwiftUI/VideoRendererConfiguration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// Copyright © 2025 Stream.io Inc. All rights reserved.
//

import Foundation

/// Configuration options for video rendering performance optimization.
public enum VideoRendererConfiguration {

/// Prewarms video renderers to avoid UI blocking when video views are first created.
/// This should be called early in your app lifecycle, before any video views appear.
/// Safe to call from any thread - will never block the caller.
///
/// - Parameter count: The number of video renderers to pre-create (default is 2).
///
/// ## Usage
/// ```swift
/// // Early in app lifecycle - simple call, never blocks
/// VideoRendererConfiguration.prewarm(count: 5)
///
/// // Then later initialize StreamVideo as usual
/// let streamVideo = StreamVideo(apiKey: apiKey, user: user, token: token)
/// ```
public static func prewarm(count: Int = 2) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This method looks unnecessary. Can you elaborate more on why this is needed and why the existing warm up on init isn't sufficient?

Task.detached {
await VideoRendererPool.configure(initialCapacity: count)
}
}
}