Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Enhancement] Optimize writeWithoutMmap write performance with page alignment #10015

Copy link
Copy link
@guyinyou

Description

@guyinyou
Issue body actions

Before Creating the Enhancement Request

  • I have confirmed that this should be classified as an enhancement rather than a bug/feature.

Summary

Optimize write performance in writeWithoutMmap mode by aligning write operations to page boundaries, avoiding expensive read-modify-write operations when pagecache is not available.

Motivation

When warmMappedFile is enabled, files are preloaded into pagecache. However, after system restarts or when pagecache is evicted, the file may not be in pagecache. In writeWithoutMmap mode, append writes become overwrite operations via FileChannel.write().

When write end positions are not aligned to page boundaries, the OS must perform read-modify-write operations:

  1. Read the entire page containing the write position
  2. Merge the new data with existing page data
  3. Write back the entire merged page

This is inefficient compared to direct page-aligned writes. By aligning writes to page boundaries, we can achieve pure write operations without read-modify-write overhead, significantly improving write performance in scenarios where pagecache is unavailable.

Describe the Solution You'd Like

Implement page alignment for writes in writeWithoutMmap mode:

  1. Calculate the write end position: endpos = currentPos + msgLen
  2. Calculate extra bytes needed for alignment: extraAppendSize = UNSAFE_PAGE_SIZE - endpos % UNSAFE_PAGE_SIZE
  3. Calculate aligned write size: actualAppendSize = msgLen + extraAppendSize
  4. If buffer capacity allows, write the aligned size; otherwise, write only the original message length

Implementation location: DefaultMappedFile.appendMessagesInner() method, specifically in the writeWithoutMmap path when using sharedByteBuffer.

The solution should:

  • Align write end positions to UNSAFE_PAGE_SIZE boundaries
  • Handle cases where buffer capacity is insufficient for full alignment
  • Maintain backward compatibility (commitlog can contain dirty data at the end, which is acceptable)

Describe Alternatives You've Considered

  1. Always keep files in pagecache: This is not always possible due to system memory pressure and OS pagecache eviction policies.

  2. Use mmap for all writes: The writeWithoutMmap mode exists for specific use cases where mmap is not desired, so this alternative doesn't address the requirement.

  3. Pre-allocate aligned buffers: While this could help, it doesn't solve the fundamental issue of unaligned write positions causing read-modify-write operations.

  4. Ignore alignment and accept read-modify-write overhead: This maintains current behavior but sacrifices performance, which is not ideal for high-throughput scenarios.

The proposed page alignment solution is the most effective approach as it directly addresses the root cause (unaligned writes) with minimal overhead and maintains compatibility with existing behavior.

Additional Context

No response

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.