Skip to content

Commit

Permalink
More efficient diff function removing quadratic iterable (#22859)
Browse files Browse the repository at this point in the history
* More efficient diff function removing quadratic iterable

* fix format
  • Loading branch information
KevinMind committed Nov 15, 2024
1 parent 32072e0 commit 9220565
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/olympia/blocklist/mlbf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
def ordered_diff_lists(
previous: List[str], current: List[str]
) -> Tuple[List[str], List[str], int]:
current_set = set(current)
previous_set = set(previous)
# Use lists instead of sets to maintain order
extras = [x for x in current if x not in previous]
deletes = [x for x in previous if x not in current]
extras = [x for x in current if x not in previous_set]
deletes = [x for x in previous if x not in current_set]
changed_count = len(extras) + len(deletes)
return extras, deletes, changed_count

Expand Down
22 changes: 22 additions & 0 deletions src/olympia/blocklist/tests/test_mlbf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
MLBFDataBaseLoader,
MLBFDataType,
MLBFStorageLoader,
ordered_diff_lists,
)


Expand All @@ -45,6 +46,27 @@ def _block_version(self, block, version, block_type=BlockType.BLOCKED):
)


class TestOrderedDiffLists(TestCase):
def test_return_added(self):
assert ordered_diff_lists(['a', 'b'], ['a', 'b', 'c']) == (['c'], [], 1)

def test_return_removed(self):
assert ordered_diff_lists(['a', 'b', 'c'], ['a', 'b']) == ([], ['c'], 1)

def test_return_added_and_removed(self):
assert ordered_diff_lists(['a', 'b', 'c'], ['b', 'c', 'd']) == (['d'], ['a'], 2)

def test_large_diff(self):
size = 2_000_000
even_items = [i for i in range(size) if i % 2 == 0]
odd_items = [i for i in range(size) if i % 2 == 1]
assert ordered_diff_lists(even_items, odd_items) == (
odd_items,
even_items,
size,
)


class TestBaseMLBFLoader(_MLBFBase):
class TestStaticLoader(BaseMLBFLoader):
@cached_property
Expand Down

0 comments on commit 9220565

Please sign in to comment.