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

V3 Performance Improvements #522

Merged
merged 11 commits into from
Nov 4, 2024
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
36 changes: 34 additions & 2 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 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
81 changes: 25 additions & 56 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,7 @@ export async function findOrCreateDm<
const dm = JSON.parse(
await XMTPModule.findOrCreateDm(client.inboxId, peerAddress)
)
const members = dm['members']?.map((mem: string) => {
return Member.from(mem)
})
return new Dm(client, dm, members)
return new Dm(client, dm)
}

export async function createGroup<
Expand Down Expand Up @@ -359,10 +356,7 @@ export async function createGroup<
)
)

const members = group['members']?.map((mem: string) => {
return Member.from(mem)
})
return new Group(client, group, members)
return new Group(client, group)
}

export async function createGroupCustomPermissions<
Expand Down Expand Up @@ -390,10 +384,8 @@ export async function createGroupCustomPermissions<
JSON.stringify(options)
)
)
const members = group['members']?.map((mem: string) => {
return Member.from(mem)
})
return new Group(client, group, members)

return new Group(client, group)
}

export async function listGroups<
Expand All @@ -413,13 +405,11 @@ export async function listGroups<
)
).map((json: string) => {
const group = JSON.parse(json)
const members = group['members']?.map((mem: string) => {
return Member.from(mem)
})

const lastMessage = group['lastMessage']
? DecodedMessage.from(group['lastMessage'], client)
: undefined
return new Group(client, group, members, lastMessage)
return new Group(client, group, lastMessage)
})
}

Expand All @@ -440,13 +430,15 @@ export async function listV3Conversations<
)
).map((json: string) => {
const jsonObj = JSON.parse(json)
const members = jsonObj.members.map((mem: string) => {
return Member.from(mem)
})

const lastMessage = jsonObj['lastMessage']
? DecodedMessage.from(jsonObj['lastMessage'], client)
: undefined

if (jsonObj.version === ConversationVersion.GROUP) {
return new Group(client, jsonObj, members)
return new Group(client, jsonObj, lastMessage)
} else {
return new Dm(client, jsonObj, members)
return new Dm(client, jsonObj, lastMessage)
}
})
}
Expand Down Expand Up @@ -544,11 +536,8 @@ export async function findGroup<
if (!group || Object.keys(group).length === 0) {
return undefined
}
const members =
group['members']?.map((mem: string) => {
return Member.from(mem)
}) || []
return new Group(client, group, members)

return new Group(client, group)
}

export async function findConversation<
Expand All @@ -562,15 +551,11 @@ export async function findConversation<
if (!conversation || Object.keys(conversation).length === 0) {
return undefined
}
const members =
conversation['members']?.map((mem: string) => {
return Member.from(mem)
}) || []

if (conversation.version === ConversationVersion.GROUP) {
return new Group(client, conversation, members)
return new Group(client, conversation)
} else {
return new Dm(client, conversation, members)
return new Dm(client, conversation)
}
}

Expand All @@ -585,15 +570,11 @@ export async function findConversationByTopic<
if (!conversation || Object.keys(conversation).length === 0) {
return undefined
}
const members =
conversation['members']?.map((mem: string) => {
return Member.from(mem)
}) || []

if (conversation.version === ConversationVersion.GROUP) {
return new Group(client, conversation, members)
return new Group(client, conversation)
} else {
return new Dm(client, conversation, members)
return new Dm(client, conversation)
}
}

Expand All @@ -608,11 +589,8 @@ export async function findDm<
if (!dm || Object.keys(dm).length === 0) {
return undefined
}
const members =
dm['members']?.map((mem: string) => {
return Member.from(mem)
}) || []
return new Dm(client, dm, members)

return new Dm(client, dm)
}

export async function findV3Message<
Expand Down Expand Up @@ -873,10 +851,7 @@ export async function listAll<
return list.map((json: string) => {
const jsonObj = JSON.parse(json)
if (jsonObj.version === ConversationVersion.GROUP) {
const members = jsonObj.members.map((mem: string) => {
return Member.from(mem)
})
return new Group(client, jsonObj, members)
return new Group(client, jsonObj)
} else {
return new Conversation(client, jsonObj)
}
Expand Down Expand Up @@ -1494,10 +1469,7 @@ export async function processWelcomeMessage<
encryptedMessage
)
const group = JSON.parse(json)
const members = group['members']?.map((mem: string) => {
return Member.from(mem)
})
return new Group(client, group, members)
return new Group(client, group)
}

export async function processConversationWelcomeMessage<
Expand All @@ -1511,14 +1483,11 @@ export async function processConversationWelcomeMessage<
encryptedMessage
)
const conversation = JSON.parse(json)
const members = conversation['members']?.map((mem: string) => {
return Member.from(mem)
})

if (conversation.version === ConversationVersion.GROUP) {
return new Group(client, conversation, members)
return new Group(client, conversation)
} else {
return new Dm(client, conversation, members)
return new Dm(client, conversation)
}
}

Expand Down
Loading
Loading