Skip to content
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

op-batcher: stateful span batches & blind compressor #9954

Merged
merged 17 commits into from Mar 28, 2024

Conversation

axelKingsley
Copy link
Contributor

@axelKingsley axelKingsley commented Mar 22, 2024

Two Changes are included in order to optimize Span Batch Performance

Stateful Span Batches

Span Batches now build themselves iteratively instead of building from scratch every time toRawSpanBatch is called. This removes time spent building and rebuilding bit maps and lists.

Blind Compressor

Blind compressor is added to serve the Span Channel Out. Blind Compressor is like the Ratio Compressor, but without estimations. Callers must Flush the compressor to know if the compressor is full.

Comparison

Before Changes:

BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=1,_BatchCount=10,_Compressor=NonCompressor-12         	      10	     72629 ns/op	   35912 B/op	     392 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=1,_BatchCount=100,_Compressor=NonCompressor-12        	      10	    199987 ns/op	  616099 B/op	    3281 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=1,_BatchCount=1000,_Compressor=NonCompressor-12       	      10	   1497238 ns/op	 3312844 B/op	   31868 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=100,_BatchCount=10,_Compressor=NonCompressor-12       	      10	   1549633 ns/op	 4397882 B/op	   31804 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=100,_BatchCount=100,_Compressor=NonCompressor-12      	      10	  15682733 ns/op	40358673 B/op	  316851 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=1,_BatchCount=10,_Compressor=RatioCompressor-12       	      10	    103667 ns/op	   35912 B/op	     392 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=1,_BatchCount=100,_Compressor=RatioCompressor-12      	      10	   1653912 ns/op	  361111 B/op	    3276 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=1,_BatchCount=1000,_Compressor=RatioCompressor-12     	      10	   9723325 ns/op	 3265711 B/op	   31861 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=100,_BatchCount=10,_Compressor=RatioCompressor-12     	      10	   9571167 ns/op	 4402563 B/op	   31803 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=100,_BatchCount=100,_Compressor=RatioCompressor-12    	      10	  96015729 ns/op	38023342 B/op	  316849 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=1,_BatchCount=10,_Compressor=ShadowCompressor-12      	      10	    118800 ns/op	   35912 B/op	     392 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=1,_BatchCount=100,_Compressor=ShadowCompressor-12     	      10	   2373708 ns/op	  325576 B/op	    3276 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=1,_BatchCount=1000,_Compressor=ShadowCompressor-12    	      10	  17172354 ns/op	 3108343 B/op	   31860 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=100,_BatchCount=10,_Compressor=ShadowCompressor-12    	      10	  17768442 ns/op	 4392812 B/op	   31796 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=100,_BatchCount=100,_Compressor=ShadowCompressor-12   	      10	 177566733 ns/op	34166494 B/op	  316835 allocs/op

After Changes

BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=1,_BatchCount=10,_Compressor=NonCompressor-12         	      10	     13379 ns/op	   10600 B/op	      72 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=1,_BatchCount=100,_Compressor=NonCompressor-12        	      10	     77058 ns/op	  364849 B/op	     300 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=1,_BatchCount=1000,_Compressor=NonCompressor-12       	      10	    298479 ns/op	  700832 B/op	    2537 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=100,_BatchCount=10,_Compressor=NonCompressor-12       	      10	    526675 ns/op	 2141930 B/op	    5509 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=100,_BatchCount=100,_Compressor=NonCompressor-12      	      10	   3286850 ns/op	 9841617 B/op	   28013 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=1,_BatchCount=10,_Compressor=RatioCompressor-12       	      10	     94933 ns/op	   10600 B/op	      72 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=1,_BatchCount=100,_Compressor=RatioCompressor-12      	      10	    842804 ns/op	   84048 B/op	     297 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=1,_BatchCount=1000,_Compressor=RatioCompressor-12     	      10	   8397129 ns/op	  700520 B/op	    2537 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=100,_BatchCount=10,_Compressor=RatioCompressor-12     	      10	   8497275 ns/op	 2073552 B/op	    5508 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=100,_BatchCount=100,_Compressor=RatioCompressor-12    	      10	  88201888 ns/op	 7100180 B/op	   28012 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=1,_BatchCount=10,_Compressor=ShadowCompressor-12      	      10	    103913 ns/op	   10600 B/op	      72 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=1,_BatchCount=100,_Compressor=ShadowCompressor-12     	      10	   1584337 ns/op	  106166 B/op	     297 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=1,_BatchCount=1000,_Compressor=ShadowCompressor-12    	      10	  15740621 ns/op	  700520 B/op	    2537 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=100,_BatchCount=10,_Compressor=ShadowCompressor-12    	      10	  16704433 ns/op	 2215837 B/op	    5511 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=100,_BatchCount=100,_Compressor=ShadowCompressor-12   	      10	 162612196 ns/op	 8970277 B/op	   28014 allocs/op


BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=1,_BatchCount=10,_Compressor=BlindCompressor-12       	      10	     86162 ns/op	   10600 B/op	      72 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=1,_BatchCount=100,_Compressor=BlindCompressor-12      	      10	    829129 ns/op	  106166 B/op	     297 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=1,_BatchCount=1000,_Compressor=BlindCompressor-12     	      10	   8124104 ns/op	  700520 B/op	    2537 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=100,_BatchCount=10,_Compressor=BlindCompressor-12     	      10	   8481208 ns/op	 1993936 B/op	    5508 allocs/op
BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=100,_BatchCount=100,_Compressor=BlindCompressor-12    	      10	  82571750 ns/op	 7099562 B/op	   28011 allocs/op

These benchmarks test the time it takes to add The FINAL batch to a Span Batch, after already adding N-1 batches. You can draw the following conclusions from this data:

  • These improvements positively affect all configurations of Span Batches
  • Most affected is the NonCompressor, which does not incur compression time
  • Allocations are down an order of magnitude for all cases
  • Most critically, comparing Shadow Compressor to the new Blind Compressor shows a 2x speed improvement:
`develop` - BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=1,_BatchCount=1000,_Compressor=ShadowCompressor-12    	      10	  17172354 ns/op	 3108343 B/op	   31860 allocs/op

`This PR` - BenchmarkFinalBatchChannelOut/BatchType=Span,_txPerBatch=1,_BatchCount=1000,_Compressor=BlindCompressor-12     	      10	   8124104 ns/op	  700520 B/op	    2537 allocs/op

8554515284 vs 4171307808 = +111% faster on the final batch submission

As the size of the compressing structure grows, gains from the Stateful Span Batch fall off. This is because of the unavoidable cost of re-compressing this non-streaming structure from scratch every time. While we discussed avoiding compression to help the average case, the compression must be done when the channel is finished, and you must compress to know if the channel is full. Therefore, we currently aren't targeting compression avoidance

Copy link

semgrep-app bot commented Mar 22, 2024

Semgrep found 1 math-random-used finding:

  • op-node/rollup/derive/batch_test_utils.go

Do not use math/rand. Use crypto/rand instead.

Ignore this finding from math-random-used.

@axelKingsley axelKingsley changed the title Refactor SpanBatchBuilder into SpanBatch Refactor: Span Batches Mar 22, 2024
@axelKingsley axelKingsley force-pushed the axel/StatefulSpanBatches-mk2 branch 3 times, most recently from 4fe15e2 to 8eeaca5 Compare March 22, 2024 23:55
@axelKingsley axelKingsley force-pushed the axel/StatefulSpanBatches-mk2 branch 5 times, most recently from e8af2c8 to b5c334d Compare March 26, 2024 20:03
@axelKingsley axelKingsley force-pushed the axel/StatefulSpanBatches-mk2 branch 3 times, most recently from 15ca49f to f8f2263 Compare March 27, 2024 23:57
Base automatically changed from axel/BatcherBenchmarking to develop March 28, 2024 01:05
Copy link

codecov bot commented Mar 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 0.60%. Comparing base (b250918) to head (01f3741).

❗ Current head 01f3741 differs from pull request most recent head e8aae5d. Consider uploading reports for the commit e8aae5d to get more accurate results

Additional details and impacted files
@@             Coverage Diff             @@
##           develop   #9954       +/-   ##
===========================================
- Coverage    28.27%   0.60%   -27.67%     
===========================================
  Files          165      92       -73     
  Lines         7297    2481     -4816     
  Branches      1335     576      -759     
===========================================
- Hits          2063      15     -2048     
+ Misses        5128    2466     -2662     
+ Partials       106       0      -106     
Flag Coverage Δ
cannon-go-tests ?
chain-mon-tests ?
common-ts-tests ?
contracts-bedrock-tests 0.60% <ø> (ø)
contracts-ts-tests ?
core-utils-tests ?
sdk-next-tests ?
sdk-tests ?

Flags with carried forward coverage won't be shown. Click here to find out more.

see 73 files with indirect coverage changes

Copy link
Contributor

coderabbitai bot commented Mar 28, 2024

Walkthrough

Walkthrough

The system update introduces the BlindCompressor as a new compression strategy, replacing the previous RatioCompressor, enhancing efficiency. Additionally, improvements to the SpanBatch data structure streamline initialization and usage. Time tracking is integrated into block generation processes, improving data processing and testing clarity.

Changes

Files Change Summary
op-batcher/batcher/channel_builder.go Replaced spanBatchBuilder with spanBatch and updated initialization.
op-batcher/batcher/channel_builder_test.go,
op-service/testutils/random.go
Added time tracking in block generation and introduced RandomHeaderWithTime function.
op-batcher/compressor/blind_compressor.go,
op-batcher/compressor/blind_compressor_test.go,
op-node/benchmarks/batchbuilding_test.go
Introduced BlindCompressor, added tests, and updated compressor configurations.
op-e2e/actions/l2_batcher.go,
op-e2e/actions/sync_test.go
Switched to BlindCompressor from RatioCompressor in buffer method and tests for compressor configurations.
op-node/rollup/derive/... (multiple files) Enhanced SpanBatch structure and usage, including constructor updates and test enhancements.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@sebastianst sebastianst changed the title Refactor: Span Batches op-batcher: stateful span batches & blind compressor Mar 28, 2024
op-node/rollup/derive/span_channel_out.go Outdated Show resolved Hide resolved
op-e2e/actions/sync_test.go Outdated Show resolved Hide resolved
op-node/benchmarks/batchbuilding_test.go Show resolved Hide resolved
op-node/rollup/derive/span_batch_test.go Outdated Show resolved Hide resolved
op-node/rollup/derive/span_channel_out.go Show resolved Hide resolved
op-node/rollup/derive/test/random.go Show resolved Hide resolved
@sebastianst sebastianst added this pull request to the merge queue Mar 28, 2024
Merged via the queue into develop with commit c9677e4 Mar 28, 2024
70 checks passed
@sebastianst sebastianst deleted the axel/StatefulSpanBatches-mk2 branch March 28, 2024 21:11
0xfuturistic pushed a commit that referenced this pull request Apr 24, 2024
* Add Benchmark for AddSingularBatch

* update compressor configs ; address PR comments

* Add b.N

* Export RandomSingularBatch through batch_test_util.go

* measure only the final batch ; other organizational improvements

* Add Benchmark for ToRawSpanBatch

* update tests

* minor fixup

* Add Benchmark for adding *All* Span Batches

* comment fixups

* narrow tests to only test span batches that won't exceed RLP limit

* Stateful Span Batches

* Blind Compressor

* final fixes

* add peek helper function

* Address PR Comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants