[interop] Optimized unmanaged memory alloc/free

This commit is contained in:
Igor Chevdar
2020-11-12 19:10:49 +05:00
committed by Stanislav Erokhin
parent e7ab525213
commit e5f7cef594
8 changed files with 166 additions and 13 deletions
@@ -46,9 +46,9 @@ import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.symbols.impl.IrFieldSymbolImpl
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.konan.library.KonanLibraryLayout
import org.jetbrains.kotlin.konan.util.disposeNativeMemoryAllocator
import org.jetbrains.kotlin.library.SerializedIrModule
import org.jetbrains.kotlin.resolve.descriptorUtil.isEffectivelyExternal
import org.jetbrains.kotlin.utils.addToStdlib.getOrPut
/**
* Offset for synthetic elements created by lowerings and not attributable to other places in the source code.
@@ -347,6 +347,14 @@ internal class Context(config: KonanConfig) : KonanBackendContext(config) {
llvmDisposed = true
}
private var nativeMemFreed = false
fun freeNativeMem() {
if (nativeMemFreed) return
disposeNativeMemoryAllocator()
nativeMemFreed = true
}
val cStubsManager = CStubsManager(config.target)
val coverage = CoverageManager(this)
@@ -28,7 +28,11 @@ fun runTopLevelPhases(konanConfig: KonanConfig, environment: KotlinCoreEnvironme
try {
toplevelPhase.cast<CompilerPhase<Context, Unit, Unit>>().invokeToplevel(context.phaseConfig, context, Unit)
} finally {
context.disposeLlvm()
try {
context.disposeLlvm()
} finally {
context.freeNativeMem()
}
}
}
@@ -438,7 +438,8 @@ val toplevelPhase: CompilerPhase<*, Unit, Unit> = namedUnitPhase(
unitSink()
) then
objectFilesPhase then
linkerPhase
linkerPhase then
freeNativeMemPhase
)
internal fun PhaseConfig.disableIf(phase: AnyNamedPhase, condition: Boolean) {
@@ -74,6 +74,16 @@ internal val disposeLLVMPhase = namedUnitPhase(
}
)
internal val freeNativeMemPhase = namedUnitPhase(
name = "FreeNativeMem",
description = "Free native memory used by interop",
lower = object : CompilerPhase<Context, Unit, Unit> {
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<Unit>, context: Context, input: Unit) {
context.freeNativeMem()
}
}
)
internal val RTTIPhase = makeKonanModuleOpPhase(
name = "RTTI",
description = "RTTI generation",