synapsecns / sanguine

Synapse Monorepo
MIT License
38 stars 29 forks source link

Speed up GetCommittableBalance #2962

Open trajan0x opened 1 month ago

trajan0x commented 1 month ago

image

As I was going through a trace to look for speedup opps I've realized 90% of our latency exists in a single sql query:

SELECT * FROMrequest_for_quotesWHERE status IN (5,6,7)

This query is called in GetCommittableBalances which is called in HasSufficientGas which in turn is called twice sequentially (#2960).

Amazingly, it's then called a third time in forward-Seen:

image

Moving on to forward-ComimttedPending we call it 2x more sequentially

image

Then again in 2x more in forward-CommittedConfirmed:

image
greptile-apps[bot] commented 1 month ago

To speed up GetCommittableBalance, follow these steps:

  1. Optimize SQL Query: Modify the SQL query to use indexes. Ensure status column in request_for_quotes table is indexed.
CREATE INDEX idx_status ON request_for_quotes(status);
  1. Refactor GetCommittableBalances: Cache the results of GetCommittableBalances to avoid redundant database calls.

  2. Update HasSufficientGas: Refactor HasSufficientGas to use the cached results.

  3. Refactor Forward Functions: Update forward-Seen, forward-CommittedPending, and forward-CommittedConfirmed to use the cached results.

Files to Update:

Example Code:

// Cache structure
var committableBalanceCache map[string]Balance

// GetCommittableBalances with caching
func GetCommittableBalances() Balance {
    if balance, found := committableBalanceCache["key"]; found {
        return balance
    }
    // Original SQL query
    balance := queryDatabase()
    committableBalanceCache["key"] = balance
    return balance
}

// HasSufficientGas using cached balance
func HasSufficientGas() bool {
    balance := GetCommittableBalances()
    // Logic to check gas
}

This approach reduces redundant database calls and improves performance.

References

/services/rfq/relayer/inventory/manager.go /docs/bridge/docs/rfq/API/upsert-quote.api.mdx /agents/contracts/test/attestationharness/generate.go /services/rfq/relayer/service/handlers.go /packages/solidity-devops/src/SynapseScript06.sol /agents/contracts/test/snapshotharness/generate.go /services/rfq/relayer/quoter/quoter.go /services/rfq/README.md /agents/testutil/agentstestcontract/agentstestcontract.contractinfo.json /services/rfq /contrib/opbot/botmd/commands.go /services/rfq/api/docs/swagger.yaml /packages/solidity-devops/src/SynapseScript.sol /agents/contracts/summit/generate.go /packages/sdk-router/src/sdk.test.ts /packages/contracts-rfq/deployments/op_sepolia /agents/contracts/test/summitharness/summitharness.metadata.go /agents/contracts/test/summitharness/doc.go /agents/contracts/test/gasdata/generate.go /contrib/screener-api/docs/swagger.json /ethergo/submitter/README.md /services/rfq/contracts/fastbridge/fastbridge.metadata.go /agents/contracts/test/summitharness /services/omnirpc/proxy/response_test.go /docs/bridge/docs/rfq/API/upsert-quotes.api.mdx

#### About Greptile This response provides a starting point for your research, not a precise solution. Help us improve! Please leave a ๐Ÿ‘ if this is helpful and ๐Ÿ‘Ž if it is irrelevant. [Ask Greptile](https://app.greptile.com/chat/github/synapsecns/sanguine/master) ยท [Edit Issue Bot Settings](https://app.greptile.com/apps/github)