Conversation
* Add new cachedRmnContract field to OnRamp struct * Cache it once per OnRamp initialization * Add Benchmark before and after caching
core/services/ocr2/plugins/ccip/internal/ccipdata/v1_5_0/onramp_test.go
Outdated
Show resolved
Hide resolved
core/services/ocr2/plugins/ccip/internal/ccipdata/v1_5_0/onramp.go
Outdated
Show resolved
Hide resolved
core/services/ocr2/plugins/ccip/internal/ccipdata/v1_5_0/onramp.go
Outdated
Show resolved
Hide resolved
Static config was being called with every call to `IsSourceCursed` Needed to wrap the function call in `cache.CallOnceOnNoError` to activate the cache.
48e82c1 to
d7a2360
Compare
|
Go solidity wrappers are out-of-date, regenerate them via the |
d7a2360 to
eecc977
Compare
core/services/ocr2/plugins/ccip/internal/ccipdata/v1_5_0/onramp_test.go
Outdated
Show resolved
Hide resolved
| []common.Hash{configSetEventSig}, | ||
| onRampAddress, | ||
| ), | ||
| cachedStaticConfig: cachedStaticConfig, |
There was a problem hiding this comment.
only difference is that it's not loaded on init?
There was a problem hiding this comment.
only difference is that it's not loaded on init?
Yes. When doing the benchmarks I found out that it was being called every time. Needed to wrap it within CallOnceOnNoError to actually cache it.
| cachedStaticConfig: cache.CallOnceOnNoError(cachedStaticConfig), | ||
| cachedRmnContract: cache.CallOnceOnNoError(cachedRmnContract), |
There was a problem hiding this comment.
Are these called upon creating a new CCIP job? The static config one makes an rpc call, if it errors, what happens? We want to avoid a job not starting up due to intermittent rpc errors, c.f #894
There was a problem hiding this comment.
Good question! I asked it myself when I started working on it initially as well. As you can see it's wrapped within CallOnceOnNoError, so in case of errors it will not actually cache. And as this is a function definition and not actually calling while starting, it is going to be called lazily when IsSourceCursed is called. That's why I think it shouldn't affect the job starting.
There was a problem hiding this comment.
Ah I see, good note, so these aren't called here on these lines, but they will be called when IsSourceCursed is called, and only cached if there's no error. You're right, that doesn't block job startup.
## Motivation Every call to arm_contract.NewARMContract deserialized ARM contract abi under the hood. Considering that OnRamp.IsSourceCursed is called very often by the background worker it creates a huge CPU overhead. Please initialize ARMContract only once and cache it instead of doing it with every method call ## Solution * Add new cachedRmnContract field to OnRamp struct * Cache it once per OnRamp initialization * fix: Use correct cache function for StaticConfig * Add Benchmark before and after caching Note: I did benchmark the caching function against using a map with the address as key and the function caching was a bit faster. Also using the caching is more consistent with the current implementation like `StaticConfig`. Benchmark results: ``` BenchmarkIsSourceCursedWithCache-14 90266 12526 ns/op BenchmarkIsSourceCursedWithCache-14 88867 12411 ns/op BenchmarkIsSourceCursedWithCache-14 90930 12379 ns/op BenchmarkIsSourceCursedWithCache-14 91108 12259 ns/op BenchmarkIsSourceCursedWithCache-14 90915 12306 ns/op BenchmarkIsSourceCursedOriginal-14 3002 397356 ns/op BenchmarkIsSourceCursedOriginal-14 2996 402220 ns/op BenchmarkIsSourceCursedOriginal-14 3009 399806 ns/op BenchmarkIsSourceCursedOriginal-14 2575 397963 ns/op BenchmarkIsSourceCursedOriginal-14 2596 396812 ns/op ```
## Motivation Every call to arm_contract.NewARMContract deserialized ARM contract abi under the hood. Considering that OnRamp.IsSourceCursed is called very often by the background worker it creates a huge CPU overhead. Please initialize ARMContract only once and cache it instead of doing it with every method call ## Solution * Add new cachedRmnContract field to OnRamp struct * Cache it once per OnRamp initialization * fix: Use correct cache function for StaticConfig * Add Benchmark before and after caching Note: I did benchmark the caching function against using a map with the address as key and the function caching was a bit faster. Also using the caching is more consistent with the current implementation like `StaticConfig`. Benchmark results: ``` BenchmarkIsSourceCursedWithCache-14 90266 12526 ns/op BenchmarkIsSourceCursedWithCache-14 88867 12411 ns/op BenchmarkIsSourceCursedWithCache-14 90930 12379 ns/op BenchmarkIsSourceCursedWithCache-14 91108 12259 ns/op BenchmarkIsSourceCursedWithCache-14 90915 12306 ns/op BenchmarkIsSourceCursedOriginal-14 3002 397356 ns/op BenchmarkIsSourceCursedOriginal-14 2996 402220 ns/op BenchmarkIsSourceCursedOriginal-14 3009 399806 ns/op BenchmarkIsSourceCursedOriginal-14 2575 397963 ns/op BenchmarkIsSourceCursedOriginal-14 2596 396812 ns/op ```
Motivation
Every call to arm_contract.NewARMContract deserialized ARM contract abi under the hood. Considering that OnRamp.IsSourceCursed is called very often by the background worker it creates a huge CPU overhead. Please initialize ARMContract only once and cache it instead of doing it with every method call
Solution
Note: I did benchmark the caching function against using a map with the address as key and the function caching was a bit faster. Also using the caching is more consistent with the current implementation like
StaticConfig.Benchmark results: