Skip to content

Commit

Permalink
Reduce overhead of cache of spaces in SpaceRegistrar (#1234)
Browse files Browse the repository at this point in the history
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

Use lru cache and limit to 100 in memory at any one time
  • Loading branch information
texuf authored Oct 13, 2024
1 parent 244e8f3 commit 4e2c559
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/web3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
24 changes: 15 additions & 9 deletions packages/web3/src/v3/SpaceRegistrar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<string, Space>

constructor(config: BaseChainConfig, provider: ethers.providers.Provider) {
this.spaces = new LRUCache<string, Space>({
max: 100,
})
this.config = config
this.provider = provider
this.spaceArchitect = new ISpaceArchitectShim(config.addresses.spaceFactory, provider)
Expand All @@ -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
}
}
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 4e2c559

Please sign in to comment.