Skip to content

Commit

Permalink
Some potential performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
RetGal committed Nov 18, 2024
1 parent d9814b9 commit 0127239
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
36 changes: 16 additions & 20 deletions src/main/java/mpo/dayon/common/buffer/MemByteBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public class MemByteBuffer extends OutputStream {
private static final int DEFAULT_INITIAL_CAPACITY = 32;

private byte[] buffer;

private int count;

public MemByteBuffer() {
Expand All @@ -26,16 +25,16 @@ private MemByteBuffer(int capacity) {
* the newly created buffer is adopting that byte array (!)
*/
public MemByteBuffer(byte[] data) {
buffer = data.clone();
count = data.length;
buffer = Arrays.copyOf(data, count);
}

public int size() {
return count;
}

public byte[] getInternal() {
return buffer.clone();
return Arrays.copyOf(buffer, count);
}

public int mark() {
Expand All @@ -53,34 +52,33 @@ private void resetToMark(int mark) {
* <code>b</code>. The 24 high-order bits of <code>b</code> are ignored.
*/
@Override
public void write(int val) {
increaseBuffer(count + 1);
public void write(int val) {
ensureCapacity(count + 1);
buffer[count++] = (byte) val;
}

/**
* @see #write(int)
*/
private void write(int val1, int val2) {
increaseBuffer(count + 2);
ensureCapacity(count + 2);
buffer[count++] = (byte) val1;
buffer[count++] = (byte) val2;
}

@Override
public void write(byte[] buffer) {
public void write(byte[] buffer) {
write(buffer, 0, buffer.length);
}

@Override
public void write(byte[] buffer, int off, int len) {
public void write(byte[] buffer, int off, int len) {
if (len == 0) {
return;
}
final int newCount = count + len;
increaseBuffer(newCount);
ensureCapacity(count + len);
System.arraycopy(buffer, off, this.buffer, count, len);
count = newCount;
count += len;
}

/**
Expand All @@ -107,22 +105,20 @@ public void writeLenAsShort(int mark) {
}

public void fill(int len, int val) {
final int newCount = count + len;
increaseBuffer(newCount);
Arrays.fill(buffer, count, newCount, (byte) val);
count = newCount;
ensureCapacity(count + len);
Arrays.fill(buffer, count, count + len, (byte) val);
count += len;
}

public void arraycopy(byte[] in, int start, int len) {
final int newCount = count + len;
increaseBuffer(newCount);
ensureCapacity(count + len);
System.arraycopy(in, start, buffer, count, len);
count = newCount;
count += len;
}

private void increaseBuffer(int newCount) {
private void ensureCapacity(int newCount) {
if (newCount > buffer.length) {
buffer = Arrays.copyOf(buffer, Math.max(buffer.length << 1, newCount));
}
}
}
}
6 changes: 2 additions & 4 deletions src/main/java/mpo/dayon/common/capture/Capture.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public void mergeDirtyTiles(Capture[] olders) {
}
skipped.addAndGet(xskipped);
merged.set(1 + xmerged);
Log.warn(String.format("Merged [id:%d] [count:%d] [skipped:%d][merged:%d]", id, olders.length, skipped.get(), merged.get()));
Log.warn(String.format("Merged [id:%d][count:%d][skipped:%d][merged:%d]", id, olders.length, skipped.get(), merged.get()));
}

/**
Expand Down Expand Up @@ -163,9 +163,7 @@ private void doMergeDirtyTiles(Capture older) {
*/
public AbstractMap.SimpleEntry<BufferedImage, byte[]> createBufferedImage(byte[] prevBuffer, int prevWidth, int prevHeight) {
final boolean isGray = stream(dirty)
.parallel()
.filter(Objects::nonNull)
.anyMatch(tile -> tile.getCapture().size() == tile.getWidth() * tile.getHeight());
.anyMatch(tile -> tile != null && tile.getCapture().size() == tile.getWidth() * tile.getHeight());
return isGray ? createBufferedMonochromeImage(prevBuffer, prevWidth, prevHeight) : createBufferedColorImage(prevBuffer, prevWidth, prevHeight);
}

Expand Down

0 comments on commit 0127239

Please sign in to comment.