Skip to content

common: rule preprocessing for performance #1114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions packages/indexer-common/src/indexer-management/allocations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
uniqueAllocationID,
upsertIndexingRule,
} from '@graphprotocol/indexer-common'
import { preprocessRules } from '../subgraphs'

import {
BigNumber,
Expand Down Expand Up @@ -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.
Expand Down
40 changes: 31 additions & 9 deletions packages/indexer-common/src/subgraphs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,29 +192,51 @@ export class AllocationDecision {
}
}

export interface PreprocessedRules {
deploymentRulesMap: Map<string, IndexingRuleAttributes>
globalRule: IndexingRuleAttributes | undefined
}

export function preprocessRules(rules: IndexingRuleAttributes[]): PreprocessedRules {
const globalRule = rules.find((rule) => rule.identifier === INDEXING_RULE_GLOBAL)
const deploymentRulesMap = new Map<string, IndexingRuleAttributes>()

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,
),
)
}

export function isDeploymentWorthAllocatingTowards(
logger: Logger,
deployment: SubgraphDeployment,
rules: IndexingRuleAttributes[],
deploymentRulesMap: Map<string, IndexingRuleAttributes>,
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,
Expand Down
Loading