-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Performance Issues on Buffer allocation
Fix buffer allocation/deallocation causing slow downs Added buffer pool and ability to pass buffer
- Loading branch information
Alex Risch
authored and
Alex Risch
committed
Apr 16, 2024
1 parent
f0f702f
commit 491e025
Showing
4 changed files
with
296 additions
and
12 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
25 changes: 25 additions & 0 deletions
25
library/src/main/java/org/xmtp/android/library/RustBufferPool.kt
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,25 @@ | ||
import uniffi.xmtpv3.RustBuffer | ||
|
||
class RustBufferPool(private val bufferSize: Int, private val poolSize: Int) { | ||
private val buffers = Array(poolSize) { RustBuffer.alloc(bufferSize) } | ||
private val available = BooleanArray(poolSize) { true } | ||
|
||
@Synchronized | ||
fun borrowBuffer(): RustBuffer.ByValue { | ||
val index = available.indexOfFirst { it } | ||
if (index == -1) { | ||
throw RuntimeException("No available buffers in the pool") | ||
} | ||
available[index] = false | ||
return buffers[index] | ||
} | ||
|
||
@Synchronized | ||
fun returnBuffer(buffer: RustBuffer.ByValue) { | ||
val index = buffers.indexOf(buffer) | ||
if (index == -1) { | ||
throw IllegalArgumentException("Buffer not from this pool") | ||
} | ||
available[index] = true | ||
} | ||
} |
Oops, something went wrong.