Skip to content
Permalink
Browse files
Merge pull request #127 from gianm/clear-via-put-bytes
Clear memory using putByteArray instead of fill.
  • Loading branch information
leerho committed Apr 14, 2021
2 parents 07f45e8 + fc2c3fc commit 88f7055b0c5da4fbce7327f0a2810fc1349cfde3
Showing 1 changed file with 12 additions and 3 deletions.
@@ -55,11 +55,14 @@
@SuppressWarnings({"restriction"})
abstract class BaseWritableMemoryImpl extends WritableMemory {

//1KB of empty bytes for speedy clear()
private final static byte[] EMPTY_BYTES;

//Static variable for cases where byteBuf/array/direct sizes are zero
final static BaseWritableMemoryImpl ZERO_SIZE_MEMORY;

static {
EMPTY_BYTES = new byte[1024];
ZERO_SIZE_MEMORY = new HeapWritableMemoryImpl(new byte[0], 0L, 0L, READONLY);
}

@@ -415,12 +418,18 @@ public final Object getArray() {

@Override
public final void clear() {
fill(0, getCapacity(), (byte) 0);
clear(0, getCapacity());
}

@Override
public final void clear(final long offsetBytes, final long lengthBytes) {
fill(offsetBytes, lengthBytes, (byte) 0);
public final void clear(final long offsetBytes, final long lengthBytes)
{
//No need to check bounds, since putByteArray calls checkValidAndBoundsForWrite

final long endBytes = offsetBytes + lengthBytes;
for (long i = offsetBytes; i < endBytes; i += EMPTY_BYTES.length) {
putByteArray(i, EMPTY_BYTES, 0, (int) Math.min(EMPTY_BYTES.length, endBytes - i));
}
}

@Override

0 comments on commit 88f7055

Please sign in to comment.