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

Avoid singletons check when gc cleanup #440

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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ class GenerationService(

override fun generateSingleton(singletonClass: EnrichedClass): FileSpec {
val singletonTypeName = singletonClass.getTypeClassName()
val baseClass = singletonClass.inherits ?: GodotKotlinJvmTypes.obj
val classTypeBuilder = TypeSpec
.objectBuilder(singletonTypeName.className)
.superclass(GODOT_OBJECT)
.superclass(ClassName(godotApiPackage, baseClass))

classTypeBuilder.generateSingletonConstructor(singletonClass.engineClassDBIndexName)

Expand Down Expand Up @@ -696,8 +697,7 @@ class GenerationService(
.addParameter("scriptIndex", Int::class)
.returns(Boolean::class)
.addStatement(
"rawPtr = %T.getSingleton(%M)",
TRANSFER_CONTEXT,
"getSingleton(%M)",
MemberName(godotApiPackage, classIndexName),
)
.addStatement(
Expand Down
17 changes: 14 additions & 3 deletions kt/godot-library/src/main/kotlin/godot/core/KtObject.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ abstract class KtObject {
//Native object already exists, so we know the id and ptr without going back to the other side.
rawPtr = config.ptr
id = config.id
//Singletons are never initialized from here.
//Singletons are never initialized here as we force their initialization on JVM side at engine start
if (config.needBind) {
piiertho marked this conversation as resolved.
Show resolved Hide resolved
GarbageCollector.registerObjectAndBind(this)
} else {
Expand All @@ -59,8 +59,10 @@ abstract class KtObject {
//Native object doesn't exist yet, we have to create it.
val scriptIndex = TypeManager.userTypeToId[this::class] ?: -1
//If the class is a script, the ScriptInstance is going to be created at the same time as the native object.
if (new(scriptIndex)) {
//Singletons return false and shouldn't be registered
val isSingleton = !new(scriptIndex)
if (isSingleton) {
GarbageCollector.registerSingleton(this)
} else {
GarbageCollector.registerObject(this)
}
}
Expand All @@ -70,6 +72,15 @@ abstract class KtObject {

internal inline fun callConstructor(classIndex: Int, scriptIndex: Int): Unit {
TransferContext.createNativeObject(classIndex, this, this::class.java.classLoader, scriptIndex)
readPtrAndIdFromBuffer()
}

internal inline fun getSingleton(classIndex: Int) {
TransferContext.getSingleton(classIndex)
readPtrAndIdFromBuffer()
}

private inline fun readPtrAndIdFromBuffer() {
val buffer = TransferContext.buffer
rawPtr = buffer.long
id = ObjectID(buffer.long)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ internal object GarbageCollector {
/** Pointers to Godot objects.*/
private val ObjectDB = Array<GodotWeakReference?>(OBJECTDB_SIZE) { null }

/** Indexes of singletons in [ObjectDB] */
private val singletonIndexes = mutableListOf<Int>()

/** Pointers to NativeCoreType.*/
private val nativeCoreTypeMap = ConcurrentHashMap<VoidPtr, NativeCoreWeakReference>(CHECK_NUMBER)

Expand Down Expand Up @@ -82,6 +85,14 @@ internal object GarbageCollector {
}
}

fun registerSingleton(instance: KtObject) {
synchronized(ObjectDB) {
val index = instance.id.index
ObjectDB[index] = GodotWeakReference(instance, refReferenceQueue, instance.id)
singletonIndexes.add(index)
}
}

fun registerObjectAndBind(instance: KtObject) {
synchronized(ObjectDB) {
val index = instance.id.index
Expand Down Expand Up @@ -234,6 +245,10 @@ internal object GarbageCollector {

@Suppress("unused")
fun cleanUp() {
for (singletonIndex in singletonIndexes) {
ObjectDB[singletonIndex] = null
}

while (staticInstances.size > 0) {
val iterator = staticInstances.iterator()
staticInstances = mutableSetOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ internal object TransferContext {
}

external fun createNativeObject(classIndex: Int, instance: KtObject, classLoader: ClassLoader, scriptIndex: Int)
external fun getSingleton(classIndex: Int): VoidPtr
external fun getSingleton(classIndex: Int)
external fun freeObject(rawPtr: VoidPtr)

private external fun icall(ptr: VoidPtr, methodIndex: Int, expectedReturnType: Int)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions kt/godot-library/src/main/kotlin/godot/gen/godot/AStar2D.kt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions kt/godot-library/src/main/kotlin/godot/gen/godot/AStar3D.kt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading