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

feat: memoize account names #129

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions lib/data/memo.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Memoizer<K, V> {
final V Function(K) compute;

final Map<K, V> _cache = {};

Memoizer({required this.compute});

V get(K key) {
if (!_cache.containsKey(key)) {
_cache[key] = compute(key);
}
return _cache[key] = compute(key);
}

void remove(K key) {
_cache.remove(key);
}
}
17 changes: 11 additions & 6 deletions lib/objectbox/actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:developer';
import 'dart:math' as math;

import 'package:flow/data/flow_analytics.dart';
import 'package:flow/data/memo.dart';
import 'package:flow/data/money_flow.dart';
import 'package:flow/data/prefs/frecency_group.dart';
import 'package:flow/entity/account.dart';
Expand Down Expand Up @@ -370,13 +371,17 @@ extension TransactionListActions on Iterable<Transaction> {
}

extension AccountActions on Account {
/// I'm super sleepy and practically a zombie r.n.
///
/// This is probably better of as Singleton somewhere with memoization as
/// account names won't get changed that frequently (I hope)
///
/// TODO refactor this to be more efficient
static Memoizer<String, String?>? accountNameToUuid;

static String nameByUuid(String uuid) {
accountNameToUuid ??= Memoizer(
compute: _nameByUuid,
);

return accountNameToUuid!.get(uuid) ?? "???";
}

static String _nameByUuid(String uuid) {
final query =
ObjectBox().box<Account>().query(Account_.uuid.equals(uuid)).build();

Expand Down