[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.CodedInputStream
|
||||||
import org.jetbrains.kotlin.protobuf.CodedOutputStream
|
import org.jetbrains.kotlin.protobuf.CodedOutputStream
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileInputStream
|
|
||||||
import java.io.FileOutputStream
|
|
||||||
import java.io.OutputStream
|
import java.io.OutputStream
|
||||||
|
|
||||||
internal inline fun <T> File.ifExists(f: File.() -> T): T? = if (exists()) f() else null
|
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 {
|
internal inline fun <T> File.useCodedInputIfExists(f: CodedInputStream.() -> T) = ifExists {
|
||||||
FileInputStream(this).use {
|
inputStream().use {
|
||||||
CodedInputStream.newInstance(it).f()
|
CodedInputStream.newInstance(it).f()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -36,8 +25,8 @@ internal inline fun OutputStream.useCodedOutput(f: CodedOutputStream.() -> Unit)
|
|||||||
}
|
}
|
||||||
|
|
||||||
internal inline fun File.useCodedOutput(f: CodedOutputStream.() -> Unit) {
|
internal inline fun File.useCodedOutput(f: CodedOutputStream.() -> Unit) {
|
||||||
recreate()
|
parentFile?.mkdirs()
|
||||||
FileOutputStream(this).useCodedOutput(f)
|
outputStream().useCodedOutput(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun icError(what: String, libFile: KotlinLibraryFile? = null, srcFile: KotlinSourceFile? = null): Nothing {
|
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()
|
abstract fun commitMetadata()
|
||||||
|
|
||||||
fun commitBinaryAst(fragment: JsIrProgramFragment) {
|
fun commitBinaryAst(fragment: JsIrProgramFragment) {
|
||||||
binaryAstFile.recreate()
|
binaryAstFile.parentFile?.mkdirs()
|
||||||
BufferedOutputStream(binaryAstFile.outputStream()).use {
|
BufferedOutputStream(binaryAstFile.outputStream()).use {
|
||||||
fragment.serializeTo(it)
|
fragment.serializeTo(it)
|
||||||
}
|
}
|
||||||
@@ -32,7 +32,7 @@ internal sealed class SourceFileCacheArtifact(val srcFile: KotlinSourceFile, val
|
|||||||
private val encodedMetadata: ByteArray
|
private val encodedMetadata: ByteArray
|
||||||
) : SourceFileCacheArtifact(srcFile, binaryAstFile) {
|
) : SourceFileCacheArtifact(srcFile, binaryAstFile) {
|
||||||
override fun commitMetadata() {
|
override fun commitMetadata() {
|
||||||
metadataFile.recreate()
|
metadataFile.parentFile?.mkdirs()
|
||||||
metadataFile.writeBytes(encodedMetadata)
|
metadataFile.writeBytes(encodedMetadata)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -91,10 +91,10 @@ class JsMultiModuleCache(private val moduleArtifacts: List<ModuleArtifact>) {
|
|||||||
|
|
||||||
private fun File.writeIfNotNull(data: String?) {
|
private fun File.writeIfNotNull(data: String?) {
|
||||||
if (data != null) {
|
if (data != null) {
|
||||||
recreate()
|
parentFile?.mkdirs()
|
||||||
writeText(data)
|
writeText(data)
|
||||||
} else {
|
} else {
|
||||||
ifExists { delete() }
|
delete()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user