feat: add direct write #564
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add direct write for Writer
This PR adds direct write capability to the
Writerstruct, allowing data to be written directly to the underlyingio.Writerwithout buffering. This feature provides performance benefits by reducing memory copies and enabling real-time output for streaming scenarios.The main changes include:
WithDirectWrite(direct bool)option to enable/disable direct write modedirectWritefield toWriterstruct to track the current modeWriteBool,WriteInt,WriteLong,WriteFloat,WriteDouble,WriteBytes,WriteString) to support direct writingFlushmethod to handle direct write mode efficientlyWriteBlockCBto properly handle direct write mode transitionsKey Benefits:
Usage:
How did I test it?
I thoroughly tested the direct write functionality through multiple approaches:
1. Unit Tests
Added 9 new test functions specifically for direct write mode:
TestWriter_WriteBool_DirectWriteTestWriter_WriteInt_DirectWriteTestWriter_WriteLong_DirectWriteTestWriter_WriteFloat_DirectWriteTestWriter_WriteDouble_DirectWriteTestWriter_WriteBytes_DirectWriteTestWriter_WriteString_DirectWriteTestWriter_WriteBlockHeader_DirectWriteTestWriter_WriteBlockCB_DirectWriteAll tests verify that:
2. Existing Test Suite
Writertests to ensure no regression3. Edge Case Testing
WriteBlockCBwith direct write mode to ensure proper data handlingFlushoptimization works correctly4. Integration Testing
io.Writerimplementations5. Performance Verification
Flushmethod correctly handles empty buffers in direct write modeAll tests pass successfully, confirming that the direct write functionality works correctly while maintaining full backward compatibility with existing code.