chore(sds): optimise lookups#2745
chore(sds): optimise lookups#2745danisharora099 wants to merge 20 commits intofeat/persistent_historyfrom
Conversation
…istentHistory` to use composition over inheritance for history management.
…e key generation.
…ifying history storage initialization to default to `localStorage`
…mproved compatibility.
…in message channels
…d refactor `MemLocalHistory` to support optional persistent storage.
…ingDependencies`
size-limit report 📦
|
|
This PR should go before the persistent storage one. Because here, you are refining the interface to implement, and in persistent storage, you are adding a second implementation. |
| @@ -20,33 +25,13 @@ export const DEFAULT_MAX_LENGTH = 10_000; | |||
| * at next push. | |||
| */ | |||
| export interface ILocalHistory { | |||
There was a problem hiding this comment.
MessageChannel is the one to define the interface, hence this should be in message_channel.ts.
| addMessages(...messages: ContentMessage[]): void; | ||
| hasMessage(messageId: string): boolean; | ||
| getMessage(messageId: string): ContentMessage | undefined; | ||
| getRecentMessages(count: number): ContentMessage[]; |
There was a problem hiding this comment.
I assume the expectation is for this to be ordered? if so, I'd add it to the name.
| } | ||
|
|
||
| public get length(): number { | ||
| public get size(): number { |
| private items: ContentMessage[] = []; | ||
| private messageIndex: Map<string, ContentMessage> = new Map(); |
There was a problem hiding this comment.
| private items: ContentMessage[] = []; | |
| private messageIndex: Map<string, ContentMessage> = new Map(); | |
| private orderedMessageArray: ContentMessage[] = []; | |
| private messageMap: Map<string, ContentMessage> = new Map(); |
I don't think a Map qualify as an index because it cotnains both keys (index) and values (not a ref)
| // Remove duplicates by messageId while maintaining order | ||
| this.items = _.uniqBy(combinedItems, "messageId"); | ||
|
|
||
| this.rebuildIndex(); |
There was a problem hiding this comment.
You are rebuilding the index, and then you are removing items from the array, and then you are removing the removed items from the index.
I think there is a better way to do this.
75e8e12 to
e396c3b
Compare
|
is PR ready for review? |
Problem / Description
ILocalHistorywas designed as an array-like interface (push,find,some,slice) which requiredO(N × M)array scans for dependency checking, which resulted in expensive lookup timesSolution
ILocalHistoryto implement a domain-specific interface:messageIndex) behindILocalHistoryforO(1)lookups for missing dependencyO(N+M)Notes
Checklist