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

perf: improve ProteusClientCoreCrypto performance [WPB-10000] #2974

Merged
merged 4 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -98,7 +98,10 @@ class ProteusClientCoreCryptoImpl private constructor(private val coreCrypto: Co

override suspend fun encryptBatched(message: ByteArray, sessionIds: List<CryptoSessionId>): Map<CryptoSessionId, ByteArray> {
return wrapException {
coreCrypto.proteusEncryptBatched(sessionIds.map { it.value }, toUByteList((message))).mapNotNull { entry ->
coreCrypto.proteusEncryptBatched(
sessionId = sessionIds.map { it.value },
plaintext = toUByteList((message))
).mapNotNull { entry ->
CryptoSessionId.fromEncodedString(entry.key)?.let { sessionId ->
sessionId to toByteArray(entry.value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@ import com.wire.crypto.client.toByteArray
import com.wire.kalium.cryptography.exceptions.ProteusException
import io.ktor.util.decodeBase64Bytes
import io.ktor.util.encodeBase64
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import java.io.File

@Suppress("TooManyFunctions")
class ProteusClientCoreCryptoImpl private constructor(
private val coreCrypto: CoreCrypto,
) : ProteusClient {

private val mutex = Mutex()
private val existingSessionsCache = mutableSetOf<CryptoSessionId>()

override suspend fun close() {
coreCrypto.close()
}
Expand All @@ -46,6 +51,7 @@ class ProteusClientCoreCryptoImpl private constructor(
override suspend fun remoteFingerPrint(sessionId: CryptoSessionId): ByteArray = wrapException {
coreCrypto.proteusFingerprintRemote(sessionId.value).toByteArray()
}

override suspend fun getFingerprintFromPreKey(preKey: PreKeyCrypto): ByteArray = wrapException {
coreCrypto.proteusFingerprintPrekeybundle(preKey.encodedData.decodeBase64Bytes()).toByteArray()
}
Expand All @@ -62,9 +68,16 @@ class ProteusClientCoreCryptoImpl private constructor(
return wrapException { toPreKey(coreCrypto.proteusLastResortPrekeyId().toInt(), coreCrypto.proteusLastResortPrekey()) }
}

override suspend fun doesSessionExist(sessionId: CryptoSessionId): Boolean {
return wrapException {
override suspend fun doesSessionExist(sessionId: CryptoSessionId): Boolean = mutex.withLock {
if (existingSessionsCache.contains(sessionId)) {
return@withLock true
}
wrapException {
coreCrypto.proteusSessionExists(sessionId.value)
}.also { exists ->
if (exists) {
existingSessionsCache.add(sessionId)
}
}
}

Expand All @@ -77,13 +90,9 @@ class ProteusClientCoreCryptoImpl private constructor(

return wrapException {
if (sessionExists) {
val decryptedMessage = coreCrypto.proteusDecrypt(sessionId.value, message)
coreCrypto.proteusSessionSave(sessionId.value)
decryptedMessage
coreCrypto.proteusDecrypt(sessionId.value, message)
} else {
val decryptedMessage = coreCrypto.proteusSessionFromMessage(sessionId.value, message)
coreCrypto.proteusSessionSave(sessionId.value)
decryptedMessage
coreCrypto.proteusSessionFromMessage(sessionId.value, message)
}
}
}
Expand Down Expand Up @@ -119,7 +128,8 @@ class ProteusClientCoreCryptoImpl private constructor(
}
}

override suspend fun deleteSession(sessionId: CryptoSessionId) {
override suspend fun deleteSession(sessionId: CryptoSessionId) = mutex.withLock {
existingSessionsCache.remove(sessionId)
wrapException {
coreCrypto.proteusSessionDelete(sessionId.value)
}
Expand Down
3 changes: 3 additions & 0 deletions docs/notebooks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
A place to store [Jupyter Notebook](https://jupyter.org/) files of ongoing studies.

You can use Kotlin and run it directly in the IDE, thanks to [Kotlin Notebook](https://kotlinlang.org/docs/kotlin-notebook-overview.html).
Loading
Loading