From 39e018dddc9fa84075ba3c95db7842fe73d224af Mon Sep 17 00:00:00 2001 From: madumas Date: Thu, 17 Apr 2025 12:52:20 -0400 Subject: [PATCH 1/3] common: rule preprocessing for better performance --- .../src/indexer-management/allocations.ts | 13 +++++-- packages/indexer-common/src/subgraphs.ts | 34 ++++++++++++++----- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/packages/indexer-common/src/indexer-management/allocations.ts b/packages/indexer-common/src/indexer-management/allocations.ts index fcc614f07..381e09120 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..7c373875c 100644 --- a/packages/indexer-common/src/subgraphs.ts +++ b/packages/indexer-common/src/subgraphs.ts @@ -192,13 +192,33 @@ 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 +226,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, From c3dfff7f263de43a0f268e86315e1880f829e643 Mon Sep 17 00:00:00 2001 From: madumas Date: Thu, 17 Apr 2025 13:21:54 -0400 Subject: [PATCH 2/3] lint --- .../src/indexer-management/allocations.ts | 6 +++--- packages/indexer-common/src/subgraphs.ts | 10 ++++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/indexer-common/src/indexer-management/allocations.ts b/packages/indexer-common/src/indexer-management/allocations.ts index 381e09120..9a0e77f2a 100644 --- a/packages/indexer-common/src/indexer-management/allocations.ts +++ b/packages/indexer-common/src/indexer-management/allocations.ts @@ -1042,11 +1042,11 @@ export class AllocationManager { const { deploymentRulesMap, globalRule } = preprocessRules(indexingRules) return isDeploymentWorthAllocatingTowards( - logger, - subgraphDeployment, + logger, + subgraphDeployment, indexingRules, deploymentRulesMap, - globalRule + globalRule, ).toAllocate } diff --git a/packages/indexer-common/src/subgraphs.ts b/packages/indexer-common/src/subgraphs.ts index 7c373875c..72e96a5f1 100644 --- a/packages/indexer-common/src/subgraphs.ts +++ b/packages/indexer-common/src/subgraphs.ts @@ -201,7 +201,7 @@ export function preprocessRules(rules: IndexingRuleAttributes[]): PreprocessedRu const globalRule = rules.find((rule) => rule.identifier === INDEXING_RULE_GLOBAL) const deploymentRulesMap = new Map() - rules.forEach(rule => { + rules.forEach((rule) => { if (rule.identifierType === SubgraphIdentifierType.DEPLOYMENT) { deploymentRulesMap.set(rule.identifier, rule) } @@ -218,7 +218,13 @@ export function evaluateDeployments( const { deploymentRulesMap, globalRule } = preprocessRules(rules) return networkDeployments.map((deployment) => - isDeploymentWorthAllocatingTowards(logger, deployment, rules, deploymentRulesMap, globalRule), + isDeploymentWorthAllocatingTowards( + logger, + deployment, + rules, + deploymentRulesMap, + globalRule, + ), ) } From 42d94b70ccaa2f0a0c54063e067620e42f7a32eb Mon Sep 17 00:00:00 2001 From: madumas Date: Thu, 17 Apr 2025 13:23:54 -0400 Subject: [PATCH 3/3] line break --- packages/indexer-common/src/subgraphs.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/indexer-common/src/subgraphs.ts b/packages/indexer-common/src/subgraphs.ts index 72e96a5f1..b5dd68a5c 100644 --- a/packages/indexer-common/src/subgraphs.ts +++ b/packages/indexer-common/src/subgraphs.ts @@ -200,7 +200,7 @@ export interface PreprocessedRules { 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)