Skip to content

Commit

Permalink
Merge pull request #522 from xmtp/np/performance-inbox-id
Browse files Browse the repository at this point in the history
V3 Performance Improvements
  • Loading branch information
nplasterer authored Nov 4, 2024
2 parents 794fd86 + ab69ea7 commit 971ea61
Show file tree
Hide file tree
Showing 16 changed files with 107 additions and 157 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ repositories {
dependencies {
implementation project(':expo-modules-core')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${getKotlinVersion()}"
implementation "org.xmtp:android:0.16.0"
implementation "org.xmtp:android:0.16.1"
implementation 'com.google.code.gson:gson:2.10.1'
implementation 'com.facebook.react:react-native:0.71.3'
implementation "com.daveanthonythomas.moshipack:moshipack:1.0.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,7 @@ class XMTPModule : Module() {
val conversation = client.findConversation(dmId)
?: throw XMTPException("no conversation found for $dmId")
val dm = (conversation as Conversation.Dm).dm
dm.peerInboxId()
dm.peerInboxId
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ class DmWrapper {
put("createdAt", dm.createdAt.time)
put("version", "DM")
put("topic", dm.topic)
put("peerInboxId", dm.peerInboxId())
if (dmParams.members) {
put("members", dm.members().map { MemberWrapper.encode(it) })
}
if (dmParams.creatorInboxId) put("creatorInboxId", dm.creatorInboxId())
put("peerInboxId", dm.peerInboxId)
if (dmParams.consentState) {
put("consentState", consentStateToString(dm.consentState()))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ class GroupWrapper {
put("createdAt", group.createdAt.time)
put("version", "GROUP")
put("topic", group.topic)
if (groupParams.members) {
put("members", group.members().map { MemberWrapper.encode(it) })
}
if (groupParams.creatorInboxId) put("creatorInboxId", group.creatorInboxId())
if (groupParams.isActive) put("isActive", group.isActive())
if (groupParams.addedByInboxId) put("addedByInboxId", group.addedByInboxId())
if (groupParams.name) put("name", group.name)
Expand Down Expand Up @@ -54,8 +50,6 @@ class GroupWrapper {
}

class ConversationParamsWrapper(
val members: Boolean = true,
val creatorInboxId: Boolean = true,
val isActive: Boolean = true,
val addedByInboxId: Boolean = true,
val name: Boolean = true,
Expand All @@ -69,8 +63,6 @@ class ConversationParamsWrapper(
if (conversationParams.isEmpty()) return ConversationParamsWrapper()
val jsonOptions = JsonParser.parseString(conversationParams).asJsonObject
return ConversationParamsWrapper(
if (jsonOptions.has("members")) jsonOptions.get("members").asBoolean else true,
if (jsonOptions.has("creatorInboxId")) jsonOptions.get("creatorInboxId").asBoolean else true,
if (jsonOptions.has("isActive")) jsonOptions.get("isActive").asBoolean else true,
if (jsonOptions.has("addedByInboxId")) jsonOptions.get("addedByInboxId").asBoolean else true,
if (jsonOptions.has("name")) jsonOptions.get("name").asBoolean else true,
Expand Down
40 changes: 34 additions & 6 deletions example/src/tests/groupPerformanceTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ let boClient: Client
let davonV3Client: Client
let initialPeers: Client[]
let initialGroups: Group[]
let initialV3Peers: Client[]
// let initialDms: Dm[]
// let initialV2Convos: Conversation<DefaultContentTypes>[]

Expand All @@ -86,7 +87,7 @@ async function beforeAll(
;[davonV3Client] = await createV3Clients(1)

initialPeers = await createClients(peersSize)
const initialV3Peers = await createV3Clients(peersSize)
initialV3Peers = await createV3Clients(peersSize)
boClient = initialPeers[0]

initialGroups = await createGroups(
Expand Down Expand Up @@ -116,16 +117,47 @@ test('test compare V2 and V3 dms', async () => {
v2Convos = await alixClient.conversations.list()
end = Date.now()
console.log(`Alix 2nd loaded ${v2Convos.length} v2Convos in ${end - start}ms`)
const v2Load = end - start

start = Date.now()
await davonV3Client.conversations.syncConversations()
end = Date.now()
console.log(`Davon synced ${v2Convos.length} Dms in ${end - start}ms`)

start = Date.now()
const dms = await davonV3Client.conversations.listConversations()
let dms = await davonV3Client.conversations.listConversations()
end = Date.now()
console.log(`Davon loaded ${dms.length} Dms in ${end - start}ms`)
const v3Load = end - start

await createDms(davonV3Client, await createV3Clients(5), 1)

await createV2Convos(alixClient, await createClients(5), 1)

start = Date.now()
v2Convos = await alixClient.conversations.list()
end = Date.now()
console.log(`Alix loaded ${v2Convos.length} v2Convos in ${end - start}ms`)

start = Date.now()
v2Convos = await alixClient.conversations.list()
end = Date.now()
console.log(`Alix 2nd loaded ${v2Convos.length} v2Convos in ${end - start}ms`)

start = Date.now()
await davonV3Client.conversations.syncConversations()
end = Date.now()
console.log(`Davon synced ${v2Convos.length} Dms in ${end - start}ms`)

start = Date.now()
dms = await davonV3Client.conversations.listConversations()
end = Date.now()
console.log(`Davon loaded ${dms.length} Dms in ${end - start}ms`)

assert(
v3Load < v2Load,
'v3 conversations should load faster than v2 conversations'
)

return true
})
Expand All @@ -148,10 +180,8 @@ test('testing large group listings with ordering', async () => {
let start2 = Date.now()
let groups2 = await alixClient.conversations.listGroups(
{
members: false,
consentState: false,
description: false,
creatorInboxId: false,
addedByInboxId: false,
isActive: false,
lastMessage: true,
Expand Down Expand Up @@ -196,10 +226,8 @@ test('testing large group listings with ordering', async () => {
start2 = Date.now()
groups2 = await boClient.conversations.listGroups(
{
members: false,
consentState: false,
description: false,
creatorInboxId: false,
addedByInboxId: false,
isActive: false,
lastMessage: true,
Expand Down
28 changes: 17 additions & 11 deletions example/src/tests/groupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ test('can get members of a group', async () => {
const [alixClient, boClient] = await createClients(2)
const group = await alixClient.conversations.newGroup([boClient.address])

const members = group.members
const members = await group.members()

assert(members.length === 2, `Should be 2 members but was ${members.length}`)

Expand Down Expand Up @@ -1009,7 +1009,7 @@ test('can stream groups', async () => {
throw Error('Unexpected num groups (should be 1): ' + groups.length)
}

assert(groups[0].members.length === 2, 'should be 2')
assert((await groups[0].members()).length === 2, 'should be 2')

// bo creates a group with alix so a stream callback is fired
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down Expand Up @@ -1936,7 +1936,7 @@ test('can allow and deny a inbox id', async () => {

await bo.contacts.allowInboxes([alix.inboxId])

let alixMember = (await boGroup.membersList()).find(
let alixMember = (await boGroup.members()).find(
(member) => member.inboxId === alix.inboxId
)
assert(
Expand Down Expand Up @@ -1968,7 +1968,7 @@ test('can allow and deny a inbox id', async () => {

await bo.contacts.denyInboxes([alix.inboxId])

alixMember = (await boGroup.membersList()).find(
alixMember = (await boGroup.members()).find(
(member) => member.inboxId === alix.inboxId
)
assert(
Expand Down Expand Up @@ -2266,11 +2266,16 @@ test('can create new installation without breaking group', async () => {
await client1Group?.sync()
await client2Group?.sync()

assert(client1Group?.members?.length === 2, `client 1 should see 2 members`)
const members1 = await client1Group?.members()
assert(
members1?.length === 2,
`client 1 should see 2 members but was ${members1?.length}`
)

const members2 = await client2Group?.members()
assert(
(await client2Group?.membersList())?.length === 2,
`client 2 should see 2 members`
members2?.length === 2,
`client 2 should see 2 members but was ${members2?.length}`
)

await client2.deleteLocalDatabase()
Expand All @@ -2284,9 +2289,10 @@ test('can create new installation without breaking group', async () => {
})

await client1Group?.send('This message will break the group')
const members3 = await client1Group?.members()
assert(
client1Group?.members?.length === 2,
`client 1 should still see the 2 members`
members3?.length === 2,
`client 1 should still see the 2 members but was ${members3?.length}`
)

return true
Expand All @@ -2297,13 +2303,13 @@ test('can list many groups members in parallel', async () => {
const groups: Group[] = await createGroups(alix, [bo], 20)

try {
await Promise.all(groups.slice(0, 10).map((g) => g.membersList()))
await Promise.all(groups.slice(0, 10).map((g) => g.members()))
} catch (e) {
throw new Error(`Failed listing 10 groups members with ${e}`)
}

try {
await Promise.all(groups.slice(0, 20).map((g) => g.membersList()))
await Promise.all(groups.slice(0, 20).map((g) => g.members()))
} catch (e) {
throw new Error(`Failed listing 20 groups members with ${e}`)
}
Expand Down
8 changes: 1 addition & 7 deletions ios/Wrappers/DmWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ struct DmWrapper {
"topic": dm.topic,
"peerInboxId": try await dm.peerInboxId
]

if conversationParams.members {
result["members"] = try await dm.members.compactMap { member in return try MemberWrapper.encode(member) }
}
if conversationParams.creatorInboxId {
result["creatorInboxId"] = try dm.creatorInboxId()
}

if conversationParams.consentState {
result["consentState"] = ConsentWrapper.consentStateToString(state: try dm.consentState())
}
Expand Down
16 changes: 1 addition & 15 deletions ios/Wrappers/GroupWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ struct GroupWrapper {
"version": "GROUP",
"topic": group.topic
]

if conversationParams.members {
result["members"] = try await group.members.compactMap { member in return try MemberWrapper.encode(member) }
}
if conversationParams.creatorInboxId {
result["creatorInboxId"] = try group.creatorInboxId()
}

if conversationParams.isActive {
result["isActive"] = try group.isActive()
}
Expand Down Expand Up @@ -63,8 +57,6 @@ struct GroupWrapper {
}

struct ConversationParamsWrapper {
let members: Bool
let creatorInboxId: Bool
let isActive: Bool
let addedByInboxId: Bool
let name: Bool
Expand All @@ -74,8 +66,6 @@ struct ConversationParamsWrapper {
let lastMessage: Bool

init(
members: Bool = true,
creatorInboxId: Bool = true,
isActive: Bool = true,
addedByInboxId: Bool = true,
name: Bool = true,
Expand All @@ -84,8 +74,6 @@ struct ConversationParamsWrapper {
consentState: Bool = true,
lastMessage: Bool = false
) {
self.members = members
self.creatorInboxId = creatorInboxId
self.isActive = isActive
self.addedByInboxId = addedByInboxId
self.name = name
Expand All @@ -103,8 +91,6 @@ struct ConversationParamsWrapper {
}

return ConversationParamsWrapper(
members: jsonDict["members"] as? Bool ?? true,
creatorInboxId: jsonDict["creatorInboxId"] as? Bool ?? true,
isActive: jsonDict["isActive"] as? Bool ?? true,
addedByInboxId: jsonDict["addedByInboxId"] as? Bool ?? true,
name: jsonDict["name"] as? Bool ?? true,
Expand Down
3 changes: 1 addition & 2 deletions ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -652,13 +652,12 @@ public class XMTPModule: Module {
let params = ConversationParamsWrapper.conversationParamsFromJson(conversationParams ?? "")
let order = getConversationSortOrder(order: sortOrder ?? "")
let conversations = try await client.conversations.listConversations(limit: limit, order: order)

var results: [String] = []
for conversation in conversations {
let encodedConversationContainer = try await ConversationContainerWrapper.encode(conversation, client: client)
results.append(encodedConversationContainer)
}

return results
}

Expand Down
Loading

0 comments on commit 971ea61

Please sign in to comment.