Skip to content

Commit 9220565

Browse files
committed
More efficient diff function removing quadratic iterable (#22859)
* More efficient diff function removing quadratic iterable * fix format
1 parent 32072e0 commit 9220565

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/olympia/blocklist/mlbf.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
def ordered_diff_lists(
2323
previous: List[str], current: List[str]
2424
) -> Tuple[List[str], List[str], int]:
25+
current_set = set(current)
26+
previous_set = set(previous)
2527
# Use lists instead of sets to maintain order
26-
extras = [x for x in current if x not in previous]
27-
deletes = [x for x in previous if x not in current]
28+
extras = [x for x in current if x not in previous_set]
29+
deletes = [x for x in previous if x not in current_set]
2830
changed_count = len(extras) + len(deletes)
2931
return extras, deletes, changed_count
3032

src/olympia/blocklist/tests/test_mlbf.py

+22
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
MLBFDataBaseLoader,
2020
MLBFDataType,
2121
MLBFStorageLoader,
22+
ordered_diff_lists,
2223
)
2324

2425

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

4748

49+
class TestOrderedDiffLists(TestCase):
50+
def test_return_added(self):
51+
assert ordered_diff_lists(['a', 'b'], ['a', 'b', 'c']) == (['c'], [], 1)
52+
53+
def test_return_removed(self):
54+
assert ordered_diff_lists(['a', 'b', 'c'], ['a', 'b']) == ([], ['c'], 1)
55+
56+
def test_return_added_and_removed(self):
57+
assert ordered_diff_lists(['a', 'b', 'c'], ['b', 'c', 'd']) == (['d'], ['a'], 2)
58+
59+
def test_large_diff(self):
60+
size = 2_000_000
61+
even_items = [i for i in range(size) if i % 2 == 0]
62+
odd_items = [i for i in range(size) if i % 2 == 1]
63+
assert ordered_diff_lists(even_items, odd_items) == (
64+
odd_items,
65+
even_items,
66+
size,
67+
)
68+
69+
4870
class TestBaseMLBFLoader(_MLBFBase):
4971
class TestStaticLoader(BaseMLBFLoader):
5072
@cached_property

0 commit comments

Comments
 (0)