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

perf(s3stream): cache parsed DataBlockIndex for IndexBlock #916

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion s3stream/src/main/java/com/automq/stream/s3/ObjectReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,14 @@ public static class IndexBlock {
private final ByteBuf buf;
private final int size;
private final int count;
private DataBlockIndex[] dataBlockIndices;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

T he main considerations for parsing from buf are:

  • Parsing the data block index is not frequent,
  • Parsing from buf has a low cost.
  • Saving heap memory consumption.

If this becomes a CPU bottleneck, further optimization can be done.


public IndexBlock(S3ObjectMetadata s3ObjectMetadata, ByteBuf buf) {
this.s3ObjectMetadata = s3ObjectMetadata;
this.buf = buf;
this.size = buf.readableBytes();
this.count = buf.readableBytes() / INDEX_BLOCK_UNIT_SIZE;
this.dataBlockIndices = new DataBlockIndex[count];
}

public Iterator<DataBlockIndex> iterator() {
Expand All @@ -227,14 +229,25 @@ public DataBlockIndex get(int index) {
if (index < 0 || index >= count) {
throw new IllegalArgumentException("index" + index + " is out of range [0, " + count + ")");
}

// the buf is readonly so just check if the entry have been parsed.
if (dataBlockIndices[index] != null) {
return dataBlockIndices[index];
}

int base = index * INDEX_BLOCK_UNIT_SIZE;
long streamId = buf.getLong(base);
long startOffset = buf.getLong(base + 8);
int endOffsetDelta = buf.getInt(base + 16);
int recordCount = buf.getInt(base + 20);
long blockPosition = buf.getLong(base + 24);
int blockSize = buf.getInt(base + 32);
return new DataBlockIndex(streamId, startOffset, endOffsetDelta, recordCount, blockPosition, blockSize);

DataBlockIndex idx =
new DataBlockIndex(streamId, startOffset, endOffsetDelta, recordCount, blockPosition, blockSize);
dataBlockIndices[index] = idx;

return idx;
}

public FindIndexResult find(long streamId, long startOffset, long endOffset) {
Expand Down
Loading