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): optimize StreamRecordBatchList to reduce object creation #903

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
package com.automq.stream.s3.model;

import com.automq.stream.s3.StreamRecordBatchCodec;
import com.automq.stream.utils.biniarysearch.ComparableItem;
import io.netty.buffer.ByteBuf;

public class StreamRecordBatch implements Comparable<StreamRecordBatch> {
public class StreamRecordBatch implements Comparable<StreamRecordBatch>, ComparableItem<Long> {
private final long streamId;
private final long epoch;
private final long baseOffset;
Expand Down Expand Up @@ -113,4 +114,14 @@ public String toString() {
", count=" + count +
", size=" + size() + '}';
}

@Override
public boolean isLessThan(Long value) {
return getLastOffset() <= value;
}

@Override
public boolean isGreaterThan(Long value) {
return getBaseOffset() > value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,16 @@
package com.automq.stream.utils.biniarysearch;

import com.automq.stream.s3.model.StreamRecordBatch;
import java.util.ArrayList;

import java.util.List;
import java.util.Objects;

public class StreamRecordBatchList extends AbstractOrderedCollection<Long> {

private final List<ComparableStreamRecordBatch> records;
private final List<StreamRecordBatch> records;
private final int size;

public StreamRecordBatchList(List<StreamRecordBatch> records) {
this.records = new ArrayList<>(records.size());
for (StreamRecordBatch record : records) {
this.records.add(new ComparableStreamRecordBatch(record));
}
this.records = records;
this.size = records.size();
}

Expand All @@ -45,47 +41,4 @@ protected ComparableItem<Long> get(int index) {
return records.get(index);
}

private static final class ComparableStreamRecordBatch implements ComparableItem<Long> {
private final StreamRecordBatch recordBatch;

private ComparableStreamRecordBatch(StreamRecordBatch recordBatch) {
this.recordBatch = recordBatch;
}

@Override
public boolean isLessThan(Long value) {
return recordBatch.getLastOffset() <= value;
}

@Override
public boolean isGreaterThan(Long value) {
return recordBatch.getBaseOffset() > value;
}

public StreamRecordBatch recordBatch() {
return recordBatch;
}

@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (obj == null || obj.getClass() != this.getClass())
return false;
var that = (ComparableStreamRecordBatch) obj;
return Objects.equals(this.recordBatch, that.recordBatch);
}

@Override
public int hashCode() {
return Objects.hash(recordBatch);
}

@Override
public String toString() {
return "ComparableStreamRecordBatch[" +
"recordBatch=" + recordBatch + ']';
}

}
}
Loading