Skip to content

Commit 16cf2d5

Browse files
committed
common: rule preprocessing for better performance
1 parent 9faa3f9 commit 16cf2d5

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

packages/indexer-common/src/indexer-management/allocations.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
uniqueAllocationID,
3434
upsertIndexingRule,
3535
} from '@graphprotocol/indexer-common'
36+
import { preprocessRules } from '../subgraphs'
3637

3738
import {
3839
BigNumber,
@@ -1029,8 +1030,16 @@ export class AllocationManager {
10291030
`SHOULD BE UNREACHABLE: No matching subgraphDeployment (${subgraphDeploymentID.ipfsHash}) found on the network`,
10301031
)
10311032
}
1032-
return isDeploymentWorthAllocatingTowards(logger, subgraphDeployment, indexingRules)
1033-
.toAllocate
1033+
1034+
const { deploymentRulesMap, globalRule } = preprocessRules(indexingRules)
1035+
1036+
return isDeploymentWorthAllocatingTowards(
1037+
logger,
1038+
subgraphDeployment,
1039+
indexingRules,
1040+
deploymentRulesMap,
1041+
globalRule
1042+
).toAllocate
10341043
}
10351044

10361045
// Calculates the balance (GRT delta) of a single Action.

packages/indexer-common/src/subgraphs.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,29 +192,45 @@ export class AllocationDecision {
192192
}
193193
}
194194

195+
export interface PreprocessedRules {
196+
deploymentRulesMap: Map<string, IndexingRuleAttributes>
197+
globalRule: IndexingRuleAttributes | undefined
198+
}
199+
200+
export function preprocessRules(rules: IndexingRuleAttributes[]): PreprocessedRules {
201+
const globalRule = rules.find((rule) => rule.identifier === INDEXING_RULE_GLOBAL)
202+
const deploymentRulesMap = new Map<string, IndexingRuleAttributes>()
203+
204+
rules.forEach(rule => {
205+
if (rule.identifierType === SubgraphIdentifierType.DEPLOYMENT) {
206+
deploymentRulesMap.set(rule.identifier, rule)
207+
}
208+
})
209+
210+
return { deploymentRulesMap, globalRule }
211+
}
212+
195213
export function evaluateDeployments(
196214
logger: Logger,
197215
networkDeployments: SubgraphDeployment[],
198216
rules: IndexingRuleAttributes[],
199217
): AllocationDecision[] {
218+
const { deploymentRulesMap, globalRule } = preprocessRules(rules)
219+
200220
return networkDeployments.map((deployment) =>
201-
isDeploymentWorthAllocatingTowards(logger, deployment, rules),
221+
isDeploymentWorthAllocatingTowards(logger, deployment, rules, deploymentRulesMap, globalRule),
202222
)
203223
}
204224

205225
export function isDeploymentWorthAllocatingTowards(
206226
logger: Logger,
207227
deployment: SubgraphDeployment,
208228
rules: IndexingRuleAttributes[],
229+
deploymentRulesMap: Map<string, IndexingRuleAttributes>,
230+
globalRule: IndexingRuleAttributes | undefined,
209231
): AllocationDecision {
210-
const globalRule = rules.find((rule) => rule.identifier === INDEXING_RULE_GLOBAL)
211-
const deploymentRule =
212-
rules
213-
.filter((rule) => rule.identifierType == SubgraphIdentifierType.DEPLOYMENT)
214-
.find(
215-
(rule) =>
216-
new SubgraphDeploymentID(rule.identifier).bytes32 === deployment.id.bytes32,
217-
) || globalRule
232+
// Use the pre-processed map for O(1) lookup
233+
const deploymentRule = deploymentRulesMap.get(deployment.id.ipfsHash) || globalRule
218234

219235
logger.trace('Evaluating whether subgraphDeployment is worth allocating towards', {
220236
deployment,

0 commit comments

Comments
 (0)