realm / realm-kotlin

Kotlin Multiplatform and Android SDK for the Realm Mobile Database: Build Better Apps Faster.
Apache License 2.0
919 stars 53 forks source link

Internal refactor wishlist #713

Open cmelchior opened 2 years ago

cmelchior commented 2 years ago

This issue is a meta issue for tracking and discussing the most important internal refactors we would like to do in the short-team:

So far I have compiled the following list:

Tracked here:

Tracked elsewhere:

(Christian - 1-3?) We need to figure out how to split cinterop so it supports both base and sync builds. Instead of right now where sync native code is getting shipped with the base library. The networking refactor in core, should in theory remove the need to depend on us shipping SSL, but it is unclear what their timeline is.

Feel free to edit or clarify if I'm missing something

rorbech commented 2 years ago

(Christian - 1-3?) We need to figure out how to split cinterop so it supports both base and sync builds. Instead of right now where sync native code is getting shipped with the base library. The networking refactor in core, should in theory remove the need to depend on us shipping SSL, but it is unclear what their timeline is.

  • Claus has some ideas on this.

According to my initial investigations (more that a year ago 🙈) it should be possible to achieve a concept like build variants through custom compilations: https://kotlinlang.org/docs/multiplatform-configure-compilations.html

With the current setup (different modules for library-base and library-sync) it should also be possible to configure that library-base-dependency of library-sync to override the cinterop-dependency to such a new cinterop-sync variant.

Maybe worth to re-investigate if there is a formal KMP-variant concept on its way.

rorbech commented 2 years ago

(Claus -1 ) Clean up construction of managed/linked objects. Should probably go hand-in-hand with moving RealmObjectInternal into a property of RealmObject instead of the current inheritance chain.

  • Will probably be fixed as part of the above
  • Claus will add a link to the problem.

The motivation for mentioning this is that we have quite many different callsite to the RealmObjectUtil.kt methods manage and link. All callsites constructs an unmanaged object and manage/link it afterwards. We should just group creation of the Kotlin class and linking into a single method to ensure consistency. I assume that this will be an obvious thing to do as part of moving RealmObjectInternal out of the class hierarchy of RealmObject as that would be touching the same codepaths.

Optimization: Combined with the potential rework of RealmObjectInternal we could let the internal reference to the native shared object be a lazy resolved property, as we technically don't need to resolve and maintain the native pointer until we need to access the object pointed to by the link (think this was where the differentiation of the methods actually came from).

rorbech commented 2 years ago
  • (Claus - 1) Bundle all managed properties into one composite object. Initial thought is to substitute RealmObject's inheritance from RealmObjectInteral to a composition of an managed internal property. It will significantly reduce the amount of code in our compiler plugin, help us enforce invariants and improve type-safety in our internal code. Could probably also do something similar with RealmObjectCompanion to keep public visible things at a minimum.

As mentioned in the meeting, I am not sure that the above is without complications. I would imagine the major obstacle being that it is no longer possible to return the RealmObjectInternal as an RealmObject or grab the actual instance type from ::class. So we should find and identify places where that is required early in the process and verify it that is a showstopper for the refactor. I guess we could just hold a KClass<out RealmObject> and a parent: out RealmObject in the new ManagedRealmObject to overcome that, but maybe that clutters the gain/perspective of the refactor itself.