Skip to content

Commit

Permalink
add an optional inboxId for performance
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Dec 2, 2024
1 parent 592206c commit 99143a2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ class ClientTest {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val fakeWallet = PrivateKeyBuilder()
val start = Date()
runBlocking {
val client = runBlocking {
Client().create(
account = fakeWallet,
options = ClientOptions(
Expand All @@ -498,7 +498,7 @@ class ClientTest {
Log.d("PERF", "Created a client in ${time1 / 1000.0}s")

val start2 = Date()
runBlocking {
val buildClient1 = runBlocking {
Client().build(
fakeWallet.address,
options = ClientOptions(
Expand All @@ -512,6 +512,26 @@ class ClientTest {
val time2 = end2.time - start2.time
Log.d("PERF", "Built a client in ${time2 / 1000.0}s")

val start3 = Date()
val buildClient2 = runBlocking {
Client().build(
fakeWallet.address,
options = ClientOptions(
ClientOptions.Api(XMTPEnvironment.DEV, true),
appContext = context,
dbEncryptionKey = key
),
inboxId = client.inboxId
)
}
val end3 = Date()
val time3 = end3.time - start3.time
Log.d("PERF", "Built a client with inboxId in ${time3 / 1000.0}s")

assert(time2 < time1)
assert(time3 < time1)
assert(time3 < time2)
assertEquals(client.inboxId, buildClient1.inboxId)
assertEquals(client.inboxId, buildClient2.inboxId)
}
}
14 changes: 11 additions & 3 deletions library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,24 @@ class Client() {
address: String,
clientOptions: ClientOptions,
signingKey: SigningKey? = null,
inboxId: String? = null
): Client {
val accountAddress = address.lowercase()
val inboxId = generateInboxId(address.lowercase(), 0.toULong())
val start2 = Date()
val recoveredInboxId = inboxId ?: getOrCreateInboxId(clientOptions.api, accountAddress)
val end2 = Date()
Log.d("PERF", "Get or create inboxId in ${(end2.time - start2.time) / 1000.0}s")

val start = Date()
val (ffiClient, dbPath) = createFfiClient(
accountAddress,
inboxId,
recoveredInboxId,
clientOptions,
signingKey,
clientOptions.appContext,
)
val end = Date()
Log.d("PERF", "Create ffiClient with sigs ${(end.time - start.time) / 1000.0}s")

return Client(
accountAddress,
Expand Down Expand Up @@ -141,9 +148,10 @@ class Client() {
suspend fun build(
address: String,
options: ClientOptions,
inboxId: String? = null
): Client {
return try {
initializeV3Client(address, options)
initializeV3Client(address, options, inboxId = inboxId)
} catch (e: Exception) {
throw XMTPException("Error creating V3 client: ${e.message}", e)
}
Expand Down

0 comments on commit 99143a2

Please sign in to comment.