improve long to bytes with benchmarks#29
Conversation
Signed-off-by: NeatGuyCoding <15627489+NeatGuyCoding@users.noreply.github.com>
WalkthroughThe changes introduce a performance optimization to the Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes The core algorithm change in Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull Request Overview
This PR improves the longToBytes method in PacketEncoder by fixing a bug with zero values and optimizing performance, while adding comprehensive benchmarking capabilities to measure the improvements.
- Fixed the zero-value bug that caused
NegativeArraySizeExceptionwhenMath.log10(0)returned negative infinity - Replaced
Math.log10with iterative length calculation for better performance - Added JMH benchmark infrastructure and comprehensive performance testing
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| PacketEncoder.java | Fixed zero-value bug and optimized longToBytes method implementation |
| PacketEncoderTest.java | Updated test to verify zero-value fix works correctly |
| LongToBytesBenchmark.java | Added JMH benchmark class for performance comparison |
| pom.xml | Added JMH dependencies for benchmark testing |
| PerformanceTestRunner.java | Reorganized imports for better code organization |
| NettySocketIOSslConfigProperties.java | Removed unused import |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
netty-socketio-core/src/test/java/com/corundumstudio/socketio/benchmark/LongToBytesBenchmark.java (1)
42-61: Clarify the "original" implementation comment.The
longToBytesOriginalmethod comment (line 44) states "This is the old version that had issues with zero values," but the implementation includes a zero-handling fix (lines 48-50). While this is appropriate for a fair performance comparison, the comment could be more precise.Consider rewording to:
/** * Original Math.log10-based implementation for performance comparison. * Zero handling has been added to ensure a fair benchmark comparison. */
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
netty-socketio-core/pom.xml(1 hunks)netty-socketio-core/src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java(1 hunks)netty-socketio-core/src/test/java/com/corundumstudio/socketio/benchmark/LongToBytesBenchmark.java(1 hunks)netty-socketio-core/src/test/java/com/corundumstudio/socketio/protocol/PacketEncoderTest.java(1 hunks)netty-socketio-smoke-test/src/main/java/com/corundumstudio/socketio/smoketest/PerformanceTestRunner.java(1 hunks)netty-socketio-spring-boot-starter/src/main/java/com/corundumstudio/socketio/spring/boot/starter/config/NettySocketIOSslConfigProperties.java(0 hunks)
💤 Files with no reviewable changes (1)
- netty-socketio-spring-boot-starter/src/main/java/com/corundumstudio/socketio/spring/boot/starter/config/NettySocketIOSslConfigProperties.java
🧰 Additional context used
🧬 Code graph analysis (2)
netty-socketio-core/src/test/java/com/corundumstudio/socketio/protocol/PacketEncoderTest.java (1)
netty-socketio-core/src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java (1)
PacketEncoder(33-399)
netty-socketio-core/src/test/java/com/corundumstudio/socketio/benchmark/LongToBytesBenchmark.java (1)
netty-socketio-core/src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java (1)
PacketEncoder(33-399)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: build (17) / build
- GitHub Check: build (21) / build
🔇 Additional comments (5)
netty-socketio-smoke-test/src/main/java/com/corundumstudio/socketio/smoketest/PerformanceTestRunner.java (1)
27-33: LGTM! Import reorganization only.The changes are limited to import statement reorganization with no functional impact.
netty-socketio-core/pom.xml (1)
133-144: LGTM! JMH dependencies added correctly.The JMH 1.37 dependencies (core and annotation processor) are properly configured with test scope to support the new benchmark infrastructure. Version 1.37 is current and stable.
netty-socketio-core/src/test/java/com/corundumstudio/socketio/protocol/PacketEncoderTest.java (1)
548-553: LGTM! Test correctly validates zero handling.The test now properly validates that
longToBytes(0L)returns a single-element array containing the numeric byte0, which aligns with the corrected zero-safe implementation.netty-socketio-core/src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java (1)
235-259: LGTM! Clean zero-safe implementation.The revised
longToBytesimplementation correctly:
- Handles the zero case explicitly (returns
{0})- Calculates length via division loop, avoiding
Math.log10for better performance- Extracts digits in proper order using modulo and division
The approach is straightforward and eliminates the previous zero-handling gap.
netty-socketio-core/src/test/java/com/corundumstudio/socketio/benchmark/LongToBytesBenchmark.java (1)
31-82: LGTM! Well-structured JMH benchmark.The benchmark configuration and setup are sound:
- Standard JMH annotations with appropriate warmup/measurement iterations
- Good parameter coverage including edge cases (zero, single digit, large values)
- Clean separation between optimized and original implementations
- Main method provides convenient execution entry point
Summary by CodeRabbit
Bug Fixes
Tests
Chores