Skip to content

Commit

Permalink
fix: Performance Issues on Buffer allocation
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 4 changed files with 296 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.xmtp.android.library.messages.PrivateKey
import org.xmtp.android.library.messages.PrivateKeyBuilder
import org.xmtp.android.library.messages.walletAddress

@RunWith(AndroidJUnit4::class)
Expand Down Expand Up @@ -69,4 +71,23 @@ class ContactsTest {
result = contacts.isDenied(fixtures.alice.walletAddress)
assert(result)
}

@Test
fun testHehehe() {
val privateKeyData = listOf(0x08, 0x36, 0x20, 0x0f, 0xfa, 0xfa, 0x17, 0xa3, 0xcb, 0x8b, 0x54, 0xf2, 0x2d, 0x6a, 0xfa, 0x60, 0xb1, 0x3d, 0xa4, 0x87, 0x26, 0x54, 0x32, 0x41, 0xad, 0xc5, 0xc2, 0x50, 0xdb, 0xb0, 0xe0, 0xcd)
.map { it.toByte() }
.toByteArray()
// Use hardcoded privateKey
val privateKey = PrivateKeyBuilder.buildFromPrivateKeyData(privateKeyData)
val privateKeyBuilder = PrivateKeyBuilder(privateKey)
val options = ClientOptions()
val client = Client().create(account = privateKeyBuilder, options = options)
val startTime = System.currentTimeMillis()

runBlocking { client.contacts.refreshConsentList()}
val endTime = System.currentTimeMillis()
val duration = endTime - startTime
println("Time to execute block: $duration ms")

}
}
8 changes: 5 additions & 3 deletions library/src/main/java/org/xmtp/android/library/Contacts.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.xmtp.android.library

import RustBufferPool
import com.google.protobuf.kotlin.toByteStringUtf8
import kotlinx.coroutines.runBlocking
import org.xmtp.android.library.messages.ContactBundle
import org.xmtp.android.library.messages.ContactBundleBuilder
import org.xmtp.android.library.messages.EnvelopeBuilder
import org.xmtp.android.library.messages.Pagination
import org.xmtp.android.library.messages.PagingInfoSortDirection
import org.xmtp.android.library.messages.Topic
import org.xmtp.android.library.messages.walletAddress
import org.xmtp.proto.message.api.v1.MessageApiOuterClass
Expand Down Expand Up @@ -65,6 +67,7 @@ class ConsentList(
@OptIn(ExperimentalUnsignedTypes::class)
suspend fun load(): List<ConsentListEntry> {
val newDate = Date()

val envelopes =
client.apiClient.envelopes(
Topic.preferenceList(identifier).description,
Expand All @@ -76,22 +79,22 @@ class ConsentList(
)

lastFetched = newDate
val bufferPool = RustBufferPool(bufferSize = 36, poolSize = envelopes.size)
val preferences: MutableList<PrivatePreferencesAction> = mutableListOf()
for (envelope in envelopes) {
val payload =
uniffi.xmtpv3.userPreferencesDecrypt(
publicKey.toByteArray(),
privateKey.toByteArray(),
envelope.message.toByteArray(),
bufferPool
)

preferences.add(
PrivatePreferencesAction.parseFrom(
payload.toUByteArray().toByteArray(),
)
)
}

preferences.iterator().forEach { preference ->
preference.allowAddress?.walletAddressesList?.forEach { address ->
allow(address)
Expand All @@ -106,7 +109,6 @@ class ConsentList(
denyGroup(groupId.toByteArray())
}
}

return entries.values.toList()
}

Expand Down
25 changes: 25 additions & 0 deletions library/src/main/java/org/xmtp/android/library/RustBufferPool.kt
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
}
}
Loading

0 comments on commit 491e025

Please sign in to comment.