[JS IR IC] Do not recreate out files
Optimization: FileOutputStream truncates the existing files, therefore no need to delete and create a new file each time.
This commit is contained in:
committed by
Space Team
parent
033ad33322
commit
bd295df7d0
@@ -8,23 +8,12 @@ package org.jetbrains.kotlin.ir.backend.js.ic
|
||||
import org.jetbrains.kotlin.protobuf.CodedInputStream
|
||||
import org.jetbrains.kotlin.protobuf.CodedOutputStream
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileOutputStream
|
||||
import java.io.OutputStream
|
||||
|
||||
internal inline fun <T> File.ifExists(f: File.() -> T): T? = if (exists()) f() else null
|
||||
|
||||
internal fun File.recreate() {
|
||||
if (exists()) {
|
||||
delete()
|
||||
} else {
|
||||
parentFile?.mkdirs()
|
||||
}
|
||||
createNewFile()
|
||||
}
|
||||
|
||||
internal inline fun <T> File.useCodedInputIfExists(f: CodedInputStream.() -> T) = ifExists {
|
||||
FileInputStream(this).use {
|
||||
inputStream().use {
|
||||
CodedInputStream.newInstance(it).f()
|
||||
}
|
||||
}
|
||||
@@ -36,8 +25,8 @@ internal inline fun OutputStream.useCodedOutput(f: CodedOutputStream.() -> Unit)
|
||||
}
|
||||
|
||||
internal inline fun File.useCodedOutput(f: CodedOutputStream.() -> Unit) {
|
||||
recreate()
|
||||
FileOutputStream(this).useCodedOutput(f)
|
||||
parentFile?.mkdirs()
|
||||
outputStream().useCodedOutput(f)
|
||||
}
|
||||
|
||||
internal fun icError(what: String, libFile: KotlinLibraryFile? = null, srcFile: KotlinSourceFile? = null): Nothing {
|
||||
|
||||
+2
-2
@@ -15,7 +15,7 @@ internal sealed class SourceFileCacheArtifact(val srcFile: KotlinSourceFile, val
|
||||
abstract fun commitMetadata()
|
||||
|
||||
fun commitBinaryAst(fragment: JsIrProgramFragment) {
|
||||
binaryAstFile.recreate()
|
||||
binaryAstFile.parentFile?.mkdirs()
|
||||
BufferedOutputStream(binaryAstFile.outputStream()).use {
|
||||
fragment.serializeTo(it)
|
||||
}
|
||||
@@ -32,7 +32,7 @@ internal sealed class SourceFileCacheArtifact(val srcFile: KotlinSourceFile, val
|
||||
private val encodedMetadata: ByteArray
|
||||
) : SourceFileCacheArtifact(srcFile, binaryAstFile) {
|
||||
override fun commitMetadata() {
|
||||
metadataFile.recreate()
|
||||
metadataFile.parentFile?.mkdirs()
|
||||
metadataFile.writeBytes(encodedMetadata)
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -91,10 +91,10 @@ class JsMultiModuleCache(private val moduleArtifacts: List<ModuleArtifact>) {
|
||||
|
||||
private fun File.writeIfNotNull(data: String?) {
|
||||
if (data != null) {
|
||||
recreate()
|
||||
parentFile?.mkdirs()
|
||||
writeText(data)
|
||||
} else {
|
||||
ifExists { delete() }
|
||||
delete()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user