diff --git a/packages/indexer-common/src/indexer-management/allocations.ts b/packages/indexer-common/src/indexer-management/allocations.ts index fcc614f07..9a0e77f2a 100644 --- a/packages/indexer-common/src/indexer-management/allocations.ts +++ b/packages/indexer-common/src/indexer-management/allocations.ts @@ -33,6 +33,7 @@ import { uniqueAllocationID, upsertIndexingRule, } from '@graphprotocol/indexer-common' +import { preprocessRules } from '../subgraphs' import { BigNumber, @@ -1037,8 +1038,16 @@ export class AllocationManager { `SHOULD BE UNREACHABLE: No matching subgraphDeployment (${subgraphDeploymentID.ipfsHash}) found on the network`, ) } - return isDeploymentWorthAllocatingTowards(logger, subgraphDeployment, indexingRules) - .toAllocate + + const { deploymentRulesMap, globalRule } = preprocessRules(indexingRules) + + return isDeploymentWorthAllocatingTowards( + logger, + subgraphDeployment, + indexingRules, + deploymentRulesMap, + globalRule, + ).toAllocate } // Calculates the balance (GRT delta) of a single Action. diff --git a/packages/indexer-common/src/subgraphs.ts b/packages/indexer-common/src/subgraphs.ts index 9ffb3fb06..b5dd68a5c 100644 --- a/packages/indexer-common/src/subgraphs.ts +++ b/packages/indexer-common/src/subgraphs.ts @@ -192,13 +192,39 @@ export class AllocationDecision { } } +export interface PreprocessedRules { + deploymentRulesMap: Map + globalRule: IndexingRuleAttributes | undefined +} + +export function preprocessRules(rules: IndexingRuleAttributes[]): PreprocessedRules { + const globalRule = rules.find((rule) => rule.identifier === INDEXING_RULE_GLOBAL) + const deploymentRulesMap = new Map() + + rules.forEach((rule) => { + if (rule.identifierType === SubgraphIdentifierType.DEPLOYMENT) { + deploymentRulesMap.set(rule.identifier, rule) + } + }) + + return { deploymentRulesMap, globalRule } +} + export function evaluateDeployments( logger: Logger, networkDeployments: SubgraphDeployment[], rules: IndexingRuleAttributes[], ): AllocationDecision[] { + const { deploymentRulesMap, globalRule } = preprocessRules(rules) + return networkDeployments.map((deployment) => - isDeploymentWorthAllocatingTowards(logger, deployment, rules), + isDeploymentWorthAllocatingTowards( + logger, + deployment, + rules, + deploymentRulesMap, + globalRule, + ), ) } @@ -206,15 +232,11 @@ export function isDeploymentWorthAllocatingTowards( logger: Logger, deployment: SubgraphDeployment, rules: IndexingRuleAttributes[], + deploymentRulesMap: Map, + globalRule: IndexingRuleAttributes | undefined, ): AllocationDecision { - const globalRule = rules.find((rule) => rule.identifier === INDEXING_RULE_GLOBAL) - const deploymentRule = - rules - .filter((rule) => rule.identifierType == SubgraphIdentifierType.DEPLOYMENT) - .find( - (rule) => - new SubgraphDeploymentID(rule.identifier).bytes32 === deployment.id.bytes32, - ) || globalRule + // Use the pre-processed map for O(1) lookup + const deploymentRule = deploymentRulesMap.get(deployment.id.ipfsHash) || globalRule logger.trace('Evaluating whether subgraphDeployment is worth allocating towards', { deployment,