-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #125 from cryptomator/feature/bytebuffer-pooling
Use `BufferPool` for cleartext as well as ciphertext chunks
- Loading branch information
Showing
15 changed files
with
456 additions
and
498 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.cryptomator.cryptofs.fh; | ||
|
||
import org.cryptomator.cryptofs.CryptoFileSystemScoped; | ||
import org.cryptomator.cryptolib.api.Cryptor; | ||
|
||
import javax.inject.Inject; | ||
import java.lang.ref.WeakReference; | ||
import java.nio.ByteBuffer; | ||
import java.util.Optional; | ||
import java.util.Queue; | ||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
|
||
/** | ||
* A pool of ByteBuffers for cleartext and ciphertext chunks to avoid on-heap allocation. | ||
*/ | ||
@CryptoFileSystemScoped | ||
public class BufferPool { | ||
|
||
private final int ciphertextChunkSize; | ||
private final int cleartextChunkSize; | ||
private final Queue<WeakReference<ByteBuffer>> ciphertextBuffers = new ConcurrentLinkedQueue<>(); | ||
private final Queue<WeakReference<ByteBuffer>> cleartextBuffers = new ConcurrentLinkedQueue<>(); | ||
|
||
@Inject | ||
public BufferPool(Cryptor cryptor) { | ||
this.ciphertextChunkSize = cryptor.fileContentCryptor().ciphertextChunkSize(); | ||
this.cleartextChunkSize = cryptor.fileContentCryptor().cleartextChunkSize(); | ||
} | ||
|
||
private Optional<ByteBuffer> dequeueFrom(Queue<WeakReference<ByteBuffer>> queue) { | ||
WeakReference<ByteBuffer> ref; | ||
while ((ref = queue.poll()) != null) { | ||
ByteBuffer cached = ref.get(); | ||
if (cached != null) { | ||
cached.clear(); | ||
return Optional.of(cached); | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
public ByteBuffer getCiphertextBuffer() { | ||
return dequeueFrom(ciphertextBuffers).orElseGet(() -> ByteBuffer.allocate(ciphertextChunkSize)); | ||
} | ||
|
||
public ByteBuffer getCleartextBuffer() { | ||
return dequeueFrom(cleartextBuffers).orElseGet(() -> ByteBuffer.allocate(cleartextChunkSize)); | ||
} | ||
|
||
public void recycle(ByteBuffer buffer) { | ||
if (buffer.capacity() == ciphertextChunkSize) { | ||
ciphertextBuffers.add(new WeakReference<>(buffer)); | ||
} else if (buffer.capacity() == cleartextChunkSize) { | ||
cleartextBuffers.add(new WeakReference<>(buffer)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2016 Sebastian Stenzel and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the accompanying LICENSE.txt. | ||
* | ||
* Contributors: | ||
* Sebastian Stenzel - initial API and implementation | ||
*******************************************************************************/ | ||
package org.cryptomator.cryptofs.fh; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import static java.lang.String.format; | ||
|
||
/** | ||
* A chunk of plaintext data. It has these rules: | ||
* <ol> | ||
* <li>Capacity of {@code data} is always the cleartext chunk size</li> | ||
* <li>During creation, {@code data}'s limit is the chunk's size (last chunk of a file may be smaller)</li> | ||
* <li>Writes need to adjust the limit and mark this chunk dirty</li> | ||
* <li>Reads need to respect the limit and must not change it</li> | ||
* <li>When no longer used, the cleartext ByteBuffer may be recycled</li> | ||
* </ol> | ||
*/ | ||
public record Chunk(ByteBuffer data, AtomicBoolean dirty) { | ||
|
||
public Chunk(ByteBuffer data, boolean dirty) { | ||
this(data, new AtomicBoolean(dirty)); | ||
} | ||
|
||
public boolean isDirty() { | ||
return dirty.get(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 0 additions & 101 deletions
101
src/main/java/org/cryptomator/cryptofs/fh/ChunkData.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.