Skip to content

Commit

Permalink
Micro-optimizations to speed up preprocessing by ~2x
Browse files Browse the repository at this point in the history
  • Loading branch information
ankurdave committed Nov 2, 2023
1 parent e790b64 commit 0dcb68f
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions beancount_import/matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,14 @@ def _date_currency_key(self, entry: Transaction, mp: MatchablePosting) -> DateCu
currency = pw.currency
return (_date_key(entry, mp), currency)

def _search_posting(self, entry: Transaction, mp: MatchablePosting):
def _search_posting(self, key: SourcePostingIds, entry: Transaction, mp: MatchablePosting):
pw = get_posting_weight(mp.posting)
number = None
if pw is not None:
number = pw.number
return SearchPosting(
number=number,
key=_entry_and_posting_ids_key(entry, mp),
key=key,
entry=entry,
mp=mp)

Expand All @@ -296,8 +296,9 @@ def add_posting(self, entry: Transaction, mp: MatchablePosting):
if weight is None:
return

sp = self._search_posting(source_posting_ids, entry, mp)
for dc in self.fuzz_date_currency_key(self._date_currency_key(entry, mp)):
self._date_currency[dc].append(self._search_posting(entry, mp))
self._date_currency[dc].append(sp)
self._date_currency_dirty[dc] = True

def get_date_currency_postings(self, key: DateCurrencyKey) -> List[SearchPosting]:
Expand Down Expand Up @@ -390,9 +391,9 @@ def remove_posting(self, entry: Transaction, mp: MatchablePosting):
if group is not None:
group.pop(source_posting_ids, None)

for dc in self.fuzz_date_currency_key(
self._date_currency_key(entry, mp)):
self._date_currency[dc].remove(self._search_posting(entry, mp))
sp = self._search_posting(source_posting_ids, entry, mp)
for dc in self.fuzz_date_currency_key(self._date_currency_key(entry, mp)):
self._date_currency[dc].remove(sp)

def remove_transaction(self, transaction: Transaction):
for mp in get_matchable_postings_from_transaction(
Expand Down Expand Up @@ -456,8 +457,7 @@ def _get_matches(
if posting_date and posting_date != date:
continue

key = _entry_and_posting_ids_key(sp.entry, sp.mp)
matches[key] = (sp.entry, sp.mp)
matches[sp.key] = (sp.entry, sp.mp)
return matches

def _get_weight_matches(
Expand Down

0 comments on commit 0dcb68f

Please sign in to comment.