dweymouth / supersonic

A lightweight and full-featured cross-platform desktop client for self-hosted music servers
GNU General Public License v3.0
670 stars 26 forks source link

fix: Avoid slices.Contains within tight loops #348

Closed adamantike closed 3 months ago

adamantike commented 3 months ago

Having a slices.Contains check within a for loop has a complexity of O(n*m), with a worst case scenario of O(n^2) for when both collections have the same size.

This is because slices.Contains internally runs just a regular loop, checking for equality element by element.

Instead, this diff changes every occurence of this pattern to converting the collection we compare against to a set, and then running a faster lookup against it.