Skip to content

perf: add early return optimization to LavaFormatLog#2155

Merged
nimrod-teich merged 1 commit into
mainfrom
fix/logs_early_return
Dec 30, 2025
Merged

perf: add early return optimization to LavaFormatLog#2155
nimrod-teich merged 1 commit into
mainfrom
fix/logs_early_return

Conversation

@NadavLevi

Copy link
Copy Markdown
Contributor
  • Check log level before expensive string formatting and attribute processing
  • Prevents wasted CPU cycles when logs would be filtered anyway
  • Critical for performance: debug logs at info level were doing ~2s of formatting
  • Early return skips all StrValueForLog calls, context parsing, and string building
  • Preserves original error return when skipping debug/trace logs

Description

Closes: #XXXX


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • read the contribution guide
  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the main branch
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

@NadavLevi NadavLevi force-pushed the fix/logs_early_return branch from e76f13f to 4021b8e Compare December 30, 2025 13:36
@github-actions

github-actions Bot commented Dec 30, 2025

Copy link
Copy Markdown

Test Results

    6 files   -   1    129 suites   - 2   34m 33s ⏱️ +10s
2 995 tests  - 182  2 986 ✅  - 190  1 💤 ±0  2 ❌ +2  6 🔥 +6 
3 075 runs   - 182  3 066 ✅  - 190  1 💤 ±0  2 ❌ +2  6 🔥 +6 

For more details on these failures and errors, see this check.

Results for commit 4021b8e. ± Comparison against base commit 62dfe8f.

This pull request removes 182 tests.
github.com/lavanet/lava/v5/ecosystem/cache ‑ TestCacheExpirationMultiplier
github.com/lavanet/lava/v5/ecosystem/cache ‑ TestCacheExpirationMultiplier/Multiplier_of_1
github.com/lavanet/lava/v5/ecosystem/cache ‑ TestCacheExpirationMultiplier/Multiplier_of_1.2
github.com/lavanet/lava/v5/ecosystem/cache ‑ TestCacheExpirationMultiplier/Multiplier_of_2
github.com/lavanet/lava/v5/ecosystem/cache ‑ TestCacheExpirationMultiplier/Multiplier_of_2.5
github.com/lavanet/lava/v5/ecosystem/cache ‑ TestCacheExpirationMultiplier/Multiplier_of_200
github.com/lavanet/lava/v5/ecosystem/cache ‑ TestCacheFailSetWithInvalidRequestBlock
github.com/lavanet/lava/v5/ecosystem/cache ‑ TestCacheFailSetWithInvalidRequestBlock/Finalized_After_delay_No_Hash
github.com/lavanet/lava/v5/ecosystem/cache ‑ TestCacheFailSetWithInvalidRequestBlock/Finalized_After_delay_No_Hash#01
github.com/lavanet/lava/v5/ecosystem/cache ‑ TestCacheFailSetWithInvalidRequestBlock/Finalized_After_delay_With_Hash
…

♻️ This comment has been updated with latest results.

@nimrod-teich nimrod-teich merged commit 77c514f into main Dec 30, 2025
50 of 57 checks passed
@nimrod-teich nimrod-teich deleted the fix/logs_early_return branch December 30, 2025 14:48
Tomelia1999 added a commit that referenced this pull request Dec 31, 2025
commit ac492c9
Author: Tomelia1999 <76162525+Tomelia1999@users.noreply.github.com>
Date:   Wed Dec 31 13:57:00 2025 +0200

    fix: Strict timeout statemachine (#2160)

    * update the state machine to check if global context timer is done

    * unit test

    * adding timeout verification in the begining of the stetMachine

    ---------

    Co-authored-by: Nim Rod <nimrod.teich@gmail.com>

commit 3c21210
Author: Nim Rod <nimrod.teich@gmail.com>
Date:   Wed Dec 31 13:56:47 2025 +0200

    fix: Hiesen tests failing randomly  (#2166)

    * fix: only add WebSocket URLs for API interfaces that support them

    REST API only supports HTTP/HTTPS, not WebSocket. Previously,
    CreateChainLibMocks was unconditionally adding ws:// URLs for all
    non-gRPC interfaces, causing test failures with:
      ERR URL scheme should be (http/https), got: ws apiInterface=rest

    Now WebSocket URLs are only added for JsonRPC and TendermintRPC
    interfaces which actually support WebSocket connections.

    * fix: set Enabled: true for test Endpoints to prevent provider blocking

    When creating Endpoint structs in tests without setting Enabled: true,
    the Go zero-value (false) causes all endpoints to be considered
    disabled, immediately blocking providers with:
      'purging provider after all endpoints are disabled'

    This fix adds Enabled: true to all Endpoint configurations in:
    - rpcconsumer_server_test.go
    - rpcsmartrouter_server_test.go
    - rpcsmartrouter_test.go

commit ae6d527
Author: Nim Rod <nimrod.teich@gmail.com>
Date:   Wed Dec 31 12:33:58 2025 +0200

    fix: add type assertion checks to satisfy forcetypeassert linter (#2164)

commit cf3ac0d
Author: Nim Rod <nimrod.teich@gmail.com>
Date:   Wed Dec 31 11:59:59 2025 +0200

    fix: use dynamic ports in tests to avoid address conflicts (#2162)

    Replace hardcoded port 54321 with dynamically allocated ports using
    net.Listen on :0 to prevent 'address already in use' errors when
    tests run in parallel or when ports are occupied.

    Changes:
    - Add getFreePort() helper function to both test files
    - Update createRpcConsumer() and createRpcSmartRouter() to use dynamic ports

commit f87a634
Author: NadavLevi <30565624+NadavLevi@users.noreply.github.com>
Date:   Wed Dec 31 11:59:49 2025 +0200

    feat(processingCtx): default processing ctx timeout can be set through flag (#2161)

commit 77c514f
Author: NadavLevi <30565624+NadavLevi@users.noreply.github.com>
Date:   Tue Dec 30 16:48:11 2025 +0200

    perf: add early return optimization to LavaFormatLog (#2155)

commit f3e59ef
Author: NadavLevi <30565624+NadavLevi@users.noreply.github.com>
Date:   Tue Dec 30 16:47:55 2025 +0200

    perf: implement double-check locking in cacheAddonAddresses (#2156)

    * perf: implement double-check locking in cacheAddonAddresses

    Replace simple write lock with double-check locking pattern to reduce
    lock contention in cacheAddonAddresses. This allows multiple concurrent
    readers on cache hits (fast path) while maintaining thread safety for
    cache misses (write path).

    Performance improvement:
    - Cache hits: RLock only (allows concurrent access)
    - Cache misses: RLock check → upgrade to Lock → double-check → populate

    This significantly reduces lock wait times when the cache is already
    populated, which is the common case during steady-state operation.

    * refactor: enhance cacheAddonAddresses with improved double-check locking

    Updated the cacheAddonAddresses method to utilize a more efficient double-check locking pattern. This change reduces lock contention by allowing concurrent reads on cache hits while ensuring thread safety during cache misses. The implementation includes cloning the extensions to prevent aliasing and optimizes the cache population process. This refactor aims to improve performance during steady-state operations.

    ---------

    Co-authored-by: avitenzer <tenzer@clara.co.uk>

commit f466762
Author: NadavLevi <30565624+NadavLevi@users.noreply.github.com>
Date:   Tue Dec 30 16:16:42 2025 +0200

    fix(smart-router): skip block gap warning when expectedBH is MaxInt64 (#2157)
nimrod-teich pushed a commit that referenced this pull request Dec 31, 2025
* Adding a fix for redundent retries when pairingListEmpty

* Squashed commit of the following:

commit ac492c9
Author: Tomelia1999 <76162525+Tomelia1999@users.noreply.github.com>
Date:   Wed Dec 31 13:57:00 2025 +0200

    fix: Strict timeout statemachine (#2160)

    * update the state machine to check if global context timer is done

    * unit test

    * adding timeout verification in the begining of the stetMachine

    ---------

    Co-authored-by: Nim Rod <nimrod.teich@gmail.com>

commit 3c21210
Author: Nim Rod <nimrod.teich@gmail.com>
Date:   Wed Dec 31 13:56:47 2025 +0200

    fix: Hiesen tests failing randomly  (#2166)

    * fix: only add WebSocket URLs for API interfaces that support them

    REST API only supports HTTP/HTTPS, not WebSocket. Previously,
    CreateChainLibMocks was unconditionally adding ws:// URLs for all
    non-gRPC interfaces, causing test failures with:
      ERR URL scheme should be (http/https), got: ws apiInterface=rest

    Now WebSocket URLs are only added for JsonRPC and TendermintRPC
    interfaces which actually support WebSocket connections.

    * fix: set Enabled: true for test Endpoints to prevent provider blocking

    When creating Endpoint structs in tests without setting Enabled: true,
    the Go zero-value (false) causes all endpoints to be considered
    disabled, immediately blocking providers with:
      'purging provider after all endpoints are disabled'

    This fix adds Enabled: true to all Endpoint configurations in:
    - rpcconsumer_server_test.go
    - rpcsmartrouter_server_test.go
    - rpcsmartrouter_test.go

commit ae6d527
Author: Nim Rod <nimrod.teich@gmail.com>
Date:   Wed Dec 31 12:33:58 2025 +0200

    fix: add type assertion checks to satisfy forcetypeassert linter (#2164)

commit cf3ac0d
Author: Nim Rod <nimrod.teich@gmail.com>
Date:   Wed Dec 31 11:59:59 2025 +0200

    fix: use dynamic ports in tests to avoid address conflicts (#2162)

    Replace hardcoded port 54321 with dynamically allocated ports using
    net.Listen on :0 to prevent 'address already in use' errors when
    tests run in parallel or when ports are occupied.

    Changes:
    - Add getFreePort() helper function to both test files
    - Update createRpcConsumer() and createRpcSmartRouter() to use dynamic ports

commit f87a634
Author: NadavLevi <30565624+NadavLevi@users.noreply.github.com>
Date:   Wed Dec 31 11:59:49 2025 +0200

    feat(processingCtx): default processing ctx timeout can be set through flag (#2161)

commit 77c514f
Author: NadavLevi <30565624+NadavLevi@users.noreply.github.com>
Date:   Tue Dec 30 16:48:11 2025 +0200

    perf: add early return optimization to LavaFormatLog (#2155)

commit f3e59ef
Author: NadavLevi <30565624+NadavLevi@users.noreply.github.com>
Date:   Tue Dec 30 16:47:55 2025 +0200

    perf: implement double-check locking in cacheAddonAddresses (#2156)

    * perf: implement double-check locking in cacheAddonAddresses

    Replace simple write lock with double-check locking pattern to reduce
    lock contention in cacheAddonAddresses. This allows multiple concurrent
    readers on cache hits (fast path) while maintaining thread safety for
    cache misses (write path).

    Performance improvement:
    - Cache hits: RLock only (allows concurrent access)
    - Cache misses: RLock check → upgrade to Lock → double-check → populate

    This significantly reduces lock wait times when the cache is already
    populated, which is the common case during steady-state operation.

    * refactor: enhance cacheAddonAddresses with improved double-check locking

    Updated the cacheAddonAddresses method to utilize a more efficient double-check locking pattern. This change reduces lock contention by allowing concurrent reads on cache hits while ensuring thread safety during cache misses. The implementation includes cloning the extensions to prevent aliasing and optimizes the cache population process. This refactor aims to improve performance during steady-state operations.

    ---------

    Co-authored-by: avitenzer <tenzer@clara.co.uk>

commit f466762
Author: NadavLevi <30565624+NadavLevi@users.noreply.github.com>
Date:   Tue Dec 30 16:16:42 2025 +0200

    fix(smart-router): skip block gap warning when expectedBH is MaxInt64 (#2157)

---------

Co-authored-by: avitenzer <tenzer@clara.co.uk>
nimrod-teich added a commit that referenced this pull request Dec 31, 2025
The early return optimization in #2155 broke the contract that LavaFormat*
functions always return an error. When log level was disabled and no original
error was passed, it returned nil instead of an error.

This caused issues like:
  return nil, utils.LavaFormatTrace("error message")
to return (nil, nil) instead of (nil, error), leading to nil pointer panics
in code that relied on the error return.

Changes:
- utils/lavalog.go: Return fmt.Errorf based on description when log level
  disabled and no original error
- protocol/parser/parser.go: Add defensive nil checks as safety net
nimrod-teich added a commit that referenced this pull request Dec 31, 2025
The early return optimization in #2155 broke the contract that LavaFormat*
functions always return an error. When log level was disabled and no original
error was passed, it returned nil instead of an error.

This caused issues like:
  return nil, utils.LavaFormatTrace("error message")
to return (nil, nil) instead of (nil, error), leading to nil pointer panics
in code that relied on the error return.

Changes:
- utils/lavalog.go: Return fmt.Errorf based on description when log level
  disabled and no original error
- protocol/parser/parser.go: Add defensive nil checks as safety net
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants