@@ -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+
195213export 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
205225export 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