Skip to content

Commit e973920

Browse files
perf: compress synthetic-project.json to save disk space.
Partial resolution of #1604.
1 parent 21a6b9d commit e973920

7 files changed

Lines changed: 38 additions & 19 deletions

File tree

src/main/kotlin/com/autonomousapps/internal/utils/moshi.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,21 @@ public inline fun <reified T> File.bufferWriteJsonSet(
172172
* By default, the output is compacted.
173173
*
174174
* @param obj The object to write to file
175+
* @param compress Whether to compress output with gzip.
175176
* @param indent The indent to control how the result is formatted
176177
*/
177-
public inline fun <reified T> File.bufferWriteJson(obj: T, indent: String = noJsonIndent) {
178-
JsonWriter.of(sink().buffer()).use { writer ->
178+
public inline fun <reified T> File.bufferWriteJson(
179+
obj: T,
180+
compress: Boolean = false,
181+
indent: String = noJsonIndent,
182+
) {
183+
val buffer = if (compress) {
184+
GzipSink(sink()).buffer()
185+
} else {
186+
sink().buffer()
187+
}
188+
189+
JsonWriter.of(buffer).use { writer ->
179190
getJsonAdapter<T>().indent(indent).toJson(writer, obj)
180191
}
181192
}

src/main/kotlin/com/autonomousapps/internal/utils/utils.kt

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,7 @@ internal inline fun <reified T> RegularFile.fromJsonSet(
6565
internal inline fun <reified T> File.fromJsonSet(
6666
compressed: Boolean = false,
6767
): Set<T> {
68-
val source = if (compressed) {
69-
GzipSource(source()).buffer()
70-
} else {
71-
bufferRead()
72-
}
73-
74-
return source.use { getJsonSetAdapter<T>().fromJson(it)!! }
68+
return bufferRead(compressed).use { getJsonSetAdapter<T>().fromJson(it)!! }
7569
}
7670

7771
/** Buffers reads of the RegularFileProperty from disk to the set. */
@@ -122,14 +116,20 @@ internal inline fun <reified K, reified V> File.fromJsonMap(): Map<K, V> {
122116
}
123117

124118
/** Buffer reads of the RegularFileProperty from disk to the set. */
125-
internal inline fun <reified T> RegularFileProperty.fromJson(): T = get().fromJson()
119+
internal inline fun <reified T> RegularFileProperty.fromJson(
120+
compressed: Boolean = false,
121+
): T = get().fromJson(compressed)
126122

127123
/** Buffer reads of the RegularFile from disk to the set. */
128-
internal inline fun <reified T> RegularFile.fromJson(): T = asFile.fromJson()
124+
internal inline fun <reified T> RegularFile.fromJson(
125+
compressed: Boolean = false,
126+
): T = asFile.fromJson(compressed)
129127

130128
/** Buffer reads of the File from disk to the set. */
131-
internal inline fun <reified T> File.fromJson(): T {
132-
return bufferRead().use { reader ->
129+
internal inline fun <reified T> File.fromJson(
130+
compressed: Boolean = false,
131+
): T {
132+
return bufferRead(compressed).use { reader ->
133133
getJsonAdapter<T>().fromJson(reader)!!
134134
}
135135
}
@@ -140,7 +140,15 @@ internal fun RegularFile.readLines(): List<String> = asFile.readLines()
140140

141141
internal fun RegularFileProperty.readText(): String = get().asFile.readText()
142142

143-
private fun File.bufferRead(): BufferedSource = source().buffer()
143+
private fun File.bufferRead(
144+
compressed: Boolean = false,
145+
): BufferedSource {
146+
return if (compressed) {
147+
GzipSource(source()).buffer()
148+
} else {
149+
source().buffer()
150+
}
151+
}
144152

145153
internal fun String.capitalizeSafely(): String {
146154
return replaceFirstChar(Char::uppercase)

src/main/kotlin/com/autonomousapps/tasks/AndroidScoreTask.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public abstract class AndroidScoreTask @Inject constructor(
5353

5454
public abstract class Action : WorkAction<Parameters> {
5555

56-
private val project = parameters.syntheticProject.fromJson<ProjectVariant>()
56+
private val project = parameters.syntheticProject.fromJson<ProjectVariant>(compressed = true)
5757
private val dependencies = project.dependencies(parameters.dependencies.get())
5858

5959
override fun execute() {

src/main/kotlin/com/autonomousapps/tasks/ComputeTypeUsageTask.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public abstract class ComputeTypeUsageTask @Inject constructor(
9292
val output = parameters.output.getAndDelete()
9393

9494
// 1. Load data
95-
val project = parameters.syntheticProject.fromJson<ProjectVariant>()
95+
val project = parameters.syntheticProject.fromJson<ProjectVariant>(compressed = true)
9696
val dependencies = project.dependencies(parameters.dependencies.get())
9797
val classToCoords = buildClassIndex()
9898

src/main/kotlin/com/autonomousapps/tasks/ComputeUsagesTask.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public abstract class ComputeUsagesTask @Inject constructor(
119119
private val graph = parameters.graph.fromJson<DependencyGraphView>()
120120
private val graphRuntime = parameters.graphRuntime.fromJson<DependencyGraphView>()
121121
private val declarations = parameters.declarations.fromJsonSet<Declaration>()
122-
private val project = parameters.syntheticProject.fromJson<ProjectVariant>()
122+
private val project = parameters.syntheticProject.fromJson<ProjectVariant>(compressed = true)
123123
private val dependencies = project.dependencies(parameters.dependencies.get())
124124
private val duplicateClasses =
125125
parameters.duplicateClassesReports.get().asSequence()

src/main/kotlin/com/autonomousapps/tasks/DiscoverClasspathDuplicationTask.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public abstract class DiscoverClasspathDuplicationTask : DefaultTask() {
5353
@TaskAction public fun action() {
5454
val output = output.getAndDelete()
5555

56-
val project = syntheticProject.fromJson<ProjectVariant>()
56+
val project = syntheticProject.fromJson<ProjectVariant>(compressed = true)
5757
val duplicates = ClasspathAnalyzer(project, classpathName.get(), classpath).duplicates()
5858

5959
output.writeText(duplicates.toJson())

src/main/kotlin/com/autonomousapps/tasks/SynthesizeProjectViewTask.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ public abstract class SynthesizeProjectViewTask @Inject constructor(
264264
excludedIdentifiers = excludedIdentifiers,
265265
)
266266

267-
output.bufferWriteJson(projectVariant)
267+
output.bufferWriteJson(projectVariant, compress = true)
268268
}
269269

270270
private fun CodeSource.excludeUsages(usagesExclusions: UsagesExclusions): CodeSource {

0 commit comments

Comments
 (0)