diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..0d995b8 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2026-09-05 - O(1) Indexing for In-Memory Storage Lookups +**Learning:** `MemStorage` in-memory repositories stored records in `Map` keyed by primary key ID, but query methods like `getReputation(userId)` performed linear array conversions and scans (`Array.from(map.values()).find(...)`), turning high-frequency hot path lookups into O(N) operations. +**Action:** Always maintain secondary lookup indexes (`Map`) alongside primary key maps in in-memory storage implementations for direct O(1) retrieval on hot paths. diff --git a/server/storage.ts b/server/storage.ts index eae2d1b..eb6a8ef 100644 --- a/server/storage.ts +++ b/server/storage.ts @@ -289,6 +289,8 @@ export class MemStorage implements IStorage { private users: Map; private verifications: Map; private reputations: Map; + // O(1) secondary index mapping userId directly to Reputation for fast hot-path lookups + private reputationsByUserId: Map; private transactions: Map; private riskAssessments: Map; private claims: Map; @@ -374,6 +376,7 @@ export class MemStorage implements IStorage { this.users = new Map(); this.verifications = new Map(); this.reputations = new Map(); + this.reputationsByUserId = new Map(); this.transactions = new Map(); this.riskAssessments = new Map(); this.claims = new Map(); @@ -658,9 +661,8 @@ export class MemStorage implements IStorage { // Reputation system async getReputation(userId: number): Promise { - return Array.from(this.reputations.values()).find( - (reputation) => reputation.userId === userId - ); + // ⚡ Bolt Optimization: Use secondary index for O(1) lookup instead of O(N) Array.from scan + return this.reputationsByUserId.get(userId); } async createReputation(reputation: InsertReputation): Promise { @@ -672,6 +674,8 @@ export class MemStorage implements IStorage { updatedAt: now }; this.reputations.set(id, newReputation); + // Maintain secondary index for O(1) lookups by userId + this.reputationsByUserId.set(reputation.userId, newReputation); return newReputation; } @@ -685,6 +689,8 @@ export class MemStorage implements IStorage { updatedAt: new Date() }; this.reputations.set(reputation.id, updatedReputation); + // Maintain secondary index for O(1) lookups by userId + this.reputationsByUserId.set(userId, updatedReputation); return updatedReputation; }