abjad
abjad copied to clipboard
Improve performance of membership checks in `replace_at` and `_initialize_color_dictionary` using sets
This PR optimizes membership checks by replacing lists with sets in two key functions:
replace_atfunction:
- The index_values used in the
if index % index_period in index_values:check has been converted to asetfor faster membership testing. This change improves performance whenindex_valuescontains multiple elements, as theinoperation on asethas an average time complexity of O(1), compared to O(n) for a list.
_initialize_color_dictionaryfunction:
- The
keysfrom the dictionary were previously converted to a list for membership testing. This has been replaced by using aset, significantly improving performance, especially when the dictionary is large. Theinoperation on thesetreduces the time complexity from O(n) to O(1).
Both changes are aimed at enhancing the performance of membership tests without altering the logic or behavior of the existing code. These improvements are particularly beneficial in cases where the collections being checked contain many elements.
Let me know if you'd like to add or adjust anything!