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

Dont use kotlin reflection (#1544) #1851

Open
wants to merge 2 commits into
base: community
Choose a base branch
from
Open
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
30 changes: 30 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@

## 3.0.1 (YYYY-MM-DD)

### Breaking Changes
* None.

### Enhancements
* None.

### Fixed
* Stopped using kotlin reflection, which caused ANR on some devices (Issue [#1544](https://github.com/realm/realm-kotlin/issues/1544)).

### Compatibility
* File format: Generates Realms with file format v24 (reads and upgrades file format v10 or later).
* Realm Studio 15.0.0 or above is required to open Realms created by this version.
* This release is compatible with the following Kotlin releases:
* Kotlin 2.0.20 and above. Support for experimental K2-compilation with `kotlin.experimental.tryK2=true`.
* Ktor 2.1.2 and above.
* Coroutines 1.7.0 and above.
* AtomicFu 0.18.3 and above.
* The new memory model only. See https://github.com/realm/realm-kotlin#kotlin-memory-model-and-coroutine-compatibility
* Minimum Kbson 0.4.0.
* Minimum Gradle version: 7.2.
* Minimum Android Gradle Plugin version: 7.1.3.
* Minimum Android SDK: 16.
* Minimum R8: 8.0.34.

### Internal
* None.


## 3.0.0 (2024-10-03)

### Breaking Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,36 @@ package io.realm.kotlin.internal.platform
import io.realm.kotlin.internal.RealmObjectCompanion
import io.realm.kotlin.types.BaseRealmObject
import kotlin.reflect.KClass
import kotlin.reflect.full.companionObjectInstance


// TODO OPTIMIZE Can we eliminate the reflective approach? Maybe by embedding the information
// through the compiler plugin or something similar to the Native findAssociatedObject
@PublishedApi
internal actual fun <T : Any> realmObjectCompanionOrNull(clazz: KClass<T>): RealmObjectCompanion? =
if (clazz.companionObjectInstance is RealmObjectCompanion) {
clazz.companionObjectInstance as RealmObjectCompanion
} else null
internal actual fun <T : Any> realmObjectCompanionOrNull(clazz: KClass<T>): RealmObjectCompanion? {
val cachedClass = reflectionCache[clazz]
if (cachedClass != null) {
return cachedClass
}

val companion = try {
Class.forName("${clazz.java.name}\$Companion").kotlin
} catch (thr: Throwable) {
try {
// For Parcelable classes
Class.forName("${clazz.java.name}\$CREATOR").kotlin
} catch (thr: Throwable) {
null
}
}?.objectInstance as? RealmObjectCompanion

if (companion != null) {
reflectionCache[clazz] = companion
}

return companion
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We tested this fix in our project. The fix makes realm configuration creating much faster, but it slows down some queries. If realmObjectCompanionOrNull called with Double::class then this function works much slower (more than on 50%). Most likely because of Class.forName() is called two times on every realmObjectCompanionOrNull function call.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double::class implements Parcelable in your case?) Twice call can be only for classes that implements Parcelable and only once when warm up the cache


private val reflectionCache = mutableMapOf<KClass<*>, RealmObjectCompanion>()

@PublishedApi
internal actual fun <T : BaseRealmObject> realmObjectCompanionOrThrow(clazz: KClass<T>): RealmObjectCompanion =
Expand Down
Loading