Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improve performance list method #153

Merged
merged 6 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.xmtp.android.library.messages.createDeterministic
import org.xmtp.android.library.messages.getPublicKeyBundle
import org.xmtp.android.library.messages.toPublicKeyBundle
import org.xmtp.android.library.messages.walletAddress
import org.xmtp.proto.keystore.api.v1.Keystore
import java.lang.Thread.sleep
import java.util.Date

Expand Down Expand Up @@ -137,4 +138,80 @@ class ConversationsTest {

assertEquals(allMessages.size, 15)
}

@Test
fun testLoadConvos() {
kele-leanes marked this conversation as resolved.
Show resolved Hide resolved
// Build from [8,54,32,15,250,250,23,163,203,139,84,242,45,106,250,96,177,61,164,135,38,84,50,65,173,197,194,80,219,176,224,205]
// or in hex 0836200ffafa17a3cb8b54f22d6afa60b13da48726543241adc5c250dbb0e0cd
// aka 2k many convos test wallet
// Create a ByteArray with the 32 bytes above
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(api = ClientOptions.Api(env = XMTPEnvironment.DEV))
val client = Client().create(account = privateKeyBuilder, options = options)

val start = Date()
val conversations = client.conversations.list()
val end = Date()
println("Loaded ${conversations.size} conversations in ${(end.time - start.time) / 1000.0}s")

val start2 = Date()
val conversations2 = client.conversations.list()
val end2 = Date()
println("Second time loaded ${conversations2.size} conversations in ${(end2.time - start2.time) / 1000.0}s")

val last500Topics = conversations.takeLast(500).map { it.toTopicData().toByteString() }
val client2 = Client().create(account = privateKeyBuilder, options = options)
for (topic in last500Topics) {
client2.conversations.importTopicData(Keystore.TopicMap.TopicData.parseFrom(topic))
}

val start3 = Date()
val conversations3 = client2.conversations.list()
val end3 = Date()
println("Loaded ${conversations3.size} conversations in ${(end3.time - start3.time) / 1000.0}s")

val start4 = Date()
val conversations4 = client2.conversations.list()
val end4 = Date()
println("Second time loaded ${conversations4.size} conversations in ${(end4.time - start4.time) / 1000.0}s")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ sealed class Conversation {
get() {
return when (this) {
is V1 -> conversationV1.sentAt
is V2 -> conversationV2.createdAt
is V2 -> conversationV2.conversationCreatedAt
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ data class ConversationV2(
val context: Invitation.InvitationV1.Context,
val peerAddress: String,
val client: Client,
val createdAt: Long? = null,
kele-leanes marked this conversation as resolved.
Show resolved Hide resolved
private val header: SealedInvitationHeaderV1,
) {

Expand All @@ -50,12 +51,13 @@ data class ConversationV2(
context = invitation.context,
peerAddress = peerAddress,
client = client,
createdAt = header.createdNs,
header = header,
)
}
}

val createdAt: Date = Date(header.createdNs / 1_000_000)
val conversationCreatedAt: Date = Date((createdAt ?: 0L) / 1_000_000)
kele-leanes marked this conversation as resolved.
Show resolved Hide resolved

/**
* This lists messages sent to the [Conversation].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ data class Conversations(
context = data.invitation.context,
peerAddress = data.peerAddress,
client = client,
createdAt = data.createdNs,
header = Invitation.SealedInvitationHeaderV1.getDefaultInstance(),
),
)
Expand Down
Loading