Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimizations for GoogleEventSet, speeding up merging 20+% #68

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions google_takeout_parser/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

from itertools import chain
from typing import Set, Tuple, List, Any, Optional
from typing import Set, Tuple, List, Any, Optional, Type


from cachew import cachew
Expand Down Expand Up @@ -76,8 +76,11 @@ def merge_events(*sources: CacheResults) -> CacheResults:
)


def _create_key(e: BaseEvent) -> Tuple[str, Any]:
return (type(e).__name__, e.key)
Key = Tuple[Type[Any], Any]


def _create_key(e: BaseEvent) -> Key:
return (type(e), e.key)


# This is so that its easier to use this logic in other
Expand All @@ -88,7 +91,7 @@ class GoogleEventSet:
"""

def __init__(self) -> None:
self.keys: Set[Tuple[str, Any]] = set()
self.keys: Set[Key] = set()

def __contains__(self, other: BaseEvent) -> bool:
return _create_key(other) in self.keys
Expand All @@ -98,3 +101,14 @@ def __len__(self) -> int:

def add(self, other: BaseEvent) -> None:
self.keys.add(_create_key(other))

def add_if_not_present(self, other: BaseEvent) -> bool:
"""
Returns False if element already existed, True if it didn't and we added it.
More efficient than checking membership and adding separately, since we only compute key once.
"""
key = _create_key(other)
if key in self.keys:
return False
self.keys.add(key)
return True
Loading