java-topology/docs/tickets/jitsi-videobridge-0002-bandwidth-allocator-selected-sources-contains.md

1.2 KiB
Raw Blame History

jitsi-videobridge-0002 — BandwidthAllocator.kt: List.contains() in selectedSources getter (O(n²))

Severity: MEDIUM File: jvb/src/main/kotlin/org/jitsi/videobridge/cc/allocation/BandwidthAllocator.kt Lines: 221-225

Pattern

private val selectedSources: List<String>
    get() {
        val selectedSources = allocationSettings.onStageSources.toMutableList()
        allocationSettings.selectedSources.forEach {
            if (!selectedSources.contains(it)) {   // O(n) per item
                selectedSources.add(it)
            }
        }
        return selectedSources
    }

selectedSources is MutableList<String>.contains() is O(n) scan per element of selectedSources. Result is O(|onStage| × |selected|).

Complexity

O(|onStageSources| × |selectedSources|). In conferences with many pinned sources (tile view) both lists grow to O(N). Called every allocation cycle.

Fix

Use LinkedHashSet to build the deduped list in O(1) per insertion:

val merged = LinkedHashSet(allocationSettings.onStageSources)
merged.addAll(allocationSettings.selectedSources)
return merged.toList()

Speedup (estimated)

~30× at N=50 sources. O(n²) → O(n).