More efficient diff function removing quadratic iterable (#22859)
* More efficient diff function removing quadratic iterable * fix format
This commit is contained in:
Родитель
03fae8133e
Коммит
ed0cce2628
|
@ -23,9 +23,11 @@ log = olympia.core.logger.getLogger('z.amo.blocklist')
|
|||
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
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@ from ..mlbf import (
|
|||
MLBFDataBaseLoader,
|
||||
MLBFDataType,
|
||||
MLBFStorageLoader,
|
||||
ordered_diff_lists,
|
||||
)
|
||||
|
||||
|
||||
|
@ -47,6 +48,27 @@ class _MLBFBase(TestCase):
|
|||
)
|
||||
|
||||
|
||||
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
|
||||
|
|
Загрузка…
Ссылка в новой задаче