From 99143a2d7d21896d6b03297789855cb75429cd74 Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Mon, 2 Dec 2024 10:03:44 -0800 Subject: [PATCH] add an optional inboxId for performance --- .../org/xmtp/android/library/ClientTest.kt | 24 +++++++++++++++++-- .../java/org/xmtp/android/library/Client.kt | 14 ++++++++--- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt b/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt index a1de4e6f..f9eaa606 100644 --- a/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt +++ b/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt @@ -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( @@ -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( @@ -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) } } diff --git a/library/src/main/java/org/xmtp/android/library/Client.kt b/library/src/main/java/org/xmtp/android/library/Client.kt index 3f9c04e1..d9e3d94b 100644 --- a/library/src/main/java/org/xmtp/android/library/Client.kt +++ b/library/src/main/java/org/xmtp/android/library/Client.kt @@ -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, @@ -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) }