Skip to content

Commit

Permalink
[KLIB Tool] Extract size details from KLIB files
Browse files Browse the repository at this point in the history
Previously, the KLIB tool was only capable of extracting
the size details if a KLIB was represented as a directory.
And if a KLIB was an ZIP archive file, then it just printed
the archive file size without detalization.

Now, with this commit, the KLIB tool is able to show the detalization
for KLIB archives as well as for directories.
  • Loading branch information
ddolovov authored and Space Team committed Feb 12, 2025
1 parent 073357b commit 1c10991
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@

package org.jetbrains.kotlin.cli.klib

import org.jetbrains.kotlin.konan.file.file
import org.jetbrains.kotlin.konan.file.withZipFileSystem
import org.jetbrains.kotlin.konan.library.KLIB_TARGETS_FOLDER_NAME
import org.jetbrains.kotlin.library.IrKotlinLibraryLayout
import org.jetbrains.kotlin.library.KLIB_IR_FOLDER_NAME
import org.jetbrains.kotlin.library.KLIB_METADATA_FOLDER_NAME
import org.jetbrains.kotlin.library.KLIB_MANIFEST_FILE_NAME
import org.jetbrains.kotlin.library.KotlinLibrary
import java.util.LinkedList
import org.jetbrains.kotlin.konan.file.File as KFile

/**
Expand All @@ -24,17 +27,41 @@ internal class KlibElementWithSize private constructor(val name: String, val siz
internal fun KotlinLibrary.loadSizeInfo(irInfo: KlibIrInfo?): KlibElementWithSize? {
val libraryFile = libraryFile.absoluteFile

if (libraryFile.isFile) return buildElement("KLIB file cumulative size", libraryFile)
if (!libraryFile.isDirectory) return null
return when {
libraryFile.isFile -> KlibElementWithSize(
"KLIB file cumulative size",
libraryFile.withZipFileSystem { fs -> fs.file("/").collectTopLevelElements(irInfo) }
)

val topLevelEntries = libraryFile.entries.let { entries ->
entries.singleOrNull()?.let { singleEntry ->
// If the single library entry is a "default" directory, skip it and move on.
if (singleEntry.name == "default" && singleEntry.isDirectory) singleEntry.entries else entries
} ?: entries
!libraryFile.isDirectory -> null

else -> KlibElementWithSize(
"KLIB directory cumulative size",
libraryFile.collectTopLevelElements(irInfo)
)
}
}

private fun KFile.collectTopLevelElements(irInfo: KlibIrInfo?): List<KlibElementWithSize> {
var defaultEntry: KFile? = null
val otherTopLevelEntries = ArrayList<KFile>()

val topLevelElements = topLevelEntries.map { topLevelEntry ->
for (entry in entries) {
// Expand the contents of the "default" directory, don't show the directory itself.
if (entry.name == "default" && entry.isDirectory) {
defaultEntry = entry
} else {
otherTopLevelEntries += entry
}
}

// The contents of the "default" entry go the first, then everything else.
val topLevelEntries = buildList<KFile> {
this += defaultEntry?.entries?.sortedBy(KFile::name).orEmpty()
this += otherTopLevelEntries.sortedBy(KFile::name)
}

return topLevelEntries.map { topLevelEntry ->
when (val topLevelEntryName = topLevelEntry.name) {
KLIB_IR_FOLDER_NAME -> buildIrElement(topLevelEntry, irInfo)
KLIB_METADATA_FOLDER_NAME -> buildElement(name = "Metadata", topLevelEntry)
Expand All @@ -46,8 +73,6 @@ internal fun KotlinLibrary.loadSizeInfo(irInfo: KlibIrInfo?): KlibElementWithSiz
)
}
}

return KlibElementWithSize("KLIB directory cumulative size", topLevelElements)
}

private val KFile.entries: List<KFile> get() = listFiles
Expand Down Expand Up @@ -85,5 +110,5 @@ private fun buildIrElement(entry: KFile, irInfo: KlibIrInfo?): KlibElementWithSi
nestedElements += KlibElementWithSize("IR bodies (inline functions only)", estimationOfInlineBodiesSizes)
}

return KlibElementWithSize("IR", nestedElements)
return KlibElementWithSize("IR", nestedElements.sortedBy { it.name })
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ internal class Info(output: KlibToolOutput, args: KlibToolArguments) : KlibToolC

private fun KlibElementWithSize.renderTo(appendable: Appendable, indent: Int = 0) {
appendable.appendLine(" ".repeat(indent) + name + ": " + prettySize())
children.sortedBy { it.name }.forEach { it.renderTo(appendable, indent + 1) }
children.forEach { it.renderTo(appendable, indent + 1) }
}

private fun KlibElementWithSize.prettySize(): String {
Expand Down

0 comments on commit 1c10991

Please sign in to comment.