diff --git a/packages/web3/package.json b/packages/web3/package.json index 67ac8a463..4f546be11 100644 --- a/packages/web3/package.json +++ b/packages/web3/package.json @@ -25,6 +25,7 @@ "debug": "^4.3.4", "ethers": "^5.7.2", "lodash": "^4.17.21", + "lru-cache": "^11.0.1", "nanoid": "^4.0.0", "viem": "^1.18.2", "zod": "^3.21.4" diff --git a/packages/web3/src/v3/SpaceRegistrar.ts b/packages/web3/src/v3/SpaceRegistrar.ts index 6c68ae428..7614fbaea 100644 --- a/packages/web3/src/v3/SpaceRegistrar.ts +++ b/packages/web3/src/v3/SpaceRegistrar.ts @@ -5,10 +5,7 @@ import { ILegacySpaceArchitectShim } from './ILegacySpaceArchitectShim' import { Space } from './Space' import { ethers } from 'ethers' - -interface SpaceMap { - [spaceId: string]: Space -} +import { LRUCache } from 'lru-cache' /** * A class to manage the creation of space stubs @@ -20,9 +17,12 @@ export class SpaceRegistrar { private readonly provider: ethers.providers.Provider private readonly spaceArchitect: ISpaceArchitectShim private readonly legacySpaceArchitect: ILegacySpaceArchitectShim - private readonly spaces: SpaceMap = {} + private readonly spaces: LRUCache constructor(config: BaseChainConfig, provider: ethers.providers.Provider) { + this.spaces = new LRUCache({ + max: 100, + }) this.config = config this.provider = provider this.spaceArchitect = new ISpaceArchitectShim(config.addresses.spaceFactory, provider) @@ -41,13 +41,19 @@ export class SpaceRegistrar { } public getSpace(spaceId: string): Space | undefined { - if (this.spaces[spaceId] === undefined) { + // aellis 10/2024 we don't really need to cache spaces, but they instantiate a lot of objects + // for the contracts and it's worth not wasting memory if we need to access the same space multiple times + // this code is also used on the server so we don't want to cache spaces for too long + const space = this.spaces.get(spaceId) + if (!space) { const spaceAddress = SpaceAddressFromSpaceId(spaceId) if (!spaceAddress || spaceAddress === ethers.constants.AddressZero) { - return undefined // space is not found + return undefined } - this.spaces[spaceId] = new Space(spaceAddress, spaceId, this.config, this.provider) + const space = new Space(spaceAddress, spaceId, this.config, this.provider) + this.spaces.set(spaceId, space) + return space } - return this.spaces[spaceId] + return space } } diff --git a/yarn.lock b/yarn.lock index 399afe631..e8f8478b8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4619,6 +4619,7 @@ __metadata: jest: ^29.6.2 jest-extended: ^4.0.1 lodash: ^4.17.21 + lru-cache: ^11.0.1 nanoid: ^4.0.0 ts-jest: ^29.1.1 ts-node: ^10.9.1 @@ -15053,6 +15054,13 @@ __metadata: languageName: node linkType: hard +"lru-cache@npm:^11.0.1": + version: 11.0.1 + resolution: "lru-cache@npm:11.0.1" + checksum: 6056230a99fb399234e82368b99586bd4740079e80649102f681b19337b7d8c6bc8dd7f8b8c59377c31d26deb89f548b717ae932e139b4b795879d920fccf820 + languageName: node + linkType: hard + "lru-cache@npm:^5.1.1": version: 5.1.1 resolution: "lru-cache@npm:5.1.1"