Skip to content

Commit

Permalink
Eliminate some redundant work relating to JarExploder.
Browse files Browse the repository at this point in the history
  • Loading branch information
autonomousapps committed Nov 19, 2022
1 parent 3f4ab24 commit 8373dde
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 33 deletions.
16 changes: 6 additions & 10 deletions src/main/kotlin/com/autonomousapps/internal/JarExploder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.autonomousapps.internal
import com.autonomousapps.internal.asm.ClassReader
import com.autonomousapps.internal.utils.asSequenceOfClassFiles
import com.autonomousapps.internal.utils.getLogger
import com.autonomousapps.internal.utils.mapToOrderedSet
import com.autonomousapps.model.KtFile
import com.autonomousapps.model.PhysicalArtifact
import com.autonomousapps.model.intermediates.AndroidLinterDependency
Expand All @@ -22,35 +21,33 @@ internal class JarExploder(
private val logger = getLogger<ExplodeJarTask>()

fun explodedJars(): Set<ExplodedJar> {
return artifacts
return artifacts.asSequence()
.filter { it.file.name.endsWith(".jar") }
.toExplodedJars()
}

private fun Iterable<PhysicalArtifact>.toExplodedJars(): Set<ExplodedJar> =
mapToOrderedSet { artifact ->
private fun Sequence<PhysicalArtifact>.toExplodedJars(): Set<ExplodedJar> =
map { artifact ->
val explodedJar = explodeJar(artifact)
ExplodedJar(
artifact = artifact,
exploding = explodedJar
)
}
}.toSortedSet()

/**
* Analyzes bytecode in order to extract class names and some basic structural information from
* the jar ([PhysicalArtifact.file]).
*/
private fun explodeJar(artifact: PhysicalArtifact): ExplodingJar {
val zip = ZipFile(artifact.file)

val alreadyExplodingJar: ExplodingJar? = inMemoryCache.explodedJar(artifact.file.absolutePath)
if (alreadyExplodingJar != null) {
return alreadyExplodingJar
}

inMemoryCache.updateJars(zip.name)

val zip = ZipFile(artifact.file)
val ktFiles = KtFile.fromZip(zip).toSet()

val analyzedClasses = zip.asSequenceOfClassFiles()
.map { classEntry ->
ClassNameAndAnnotationsVisitor(logger).apply {
Expand All @@ -63,7 +60,6 @@ internal class JarExploder(
// Filter out `java` packages, but not `javax`
it.className.startsWith("java.")
}
.onEach { inMemoryCache.updateClasses(it.className) }
.toSortedSet()

return ExplodingJar(
Expand Down
29 changes: 6 additions & 23 deletions src/main/kotlin/com/autonomousapps/services/InMemoryCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.services.BuildService
import org.gradle.api.services.BuildServiceParameters
import java.util.concurrent.ConcurrentSkipListMap

abstract class InMemoryCache : BuildService<InMemoryCache.Params> {

Expand All @@ -22,22 +21,6 @@ abstract class InMemoryCache : BuildService<InMemoryCache.Params> {
}

private val cacheSize = parameters.cacheSize.get()
private val jars: MutableMap<String, Int> = ConcurrentSkipListMap()
private val classes: MutableMap<String, Int> = ConcurrentSkipListMap()

internal fun updateJars(jarName: String) {
jars.merge(jarName, 1) { oldValue, increment -> oldValue + increment }
}

internal fun updateClasses(className: String) {
classes.merge(className, 1) { oldValue, increment -> oldValue + increment }
}

internal fun classes(): Map<String, Int> = classes

/*
* Caches.
*/

private inline fun <reified K, reified V> newCache(maxSize: Long = cacheSize): Cache<K, V> {
val builder = Caffeine.newBuilder()
Expand All @@ -46,23 +29,23 @@ abstract class InMemoryCache : BuildService<InMemoryCache.Params> {
}

private val explodingJars: Cache<String, ExplodingJar> = newCache()
private val inlineMembers2: Cache<String, Set<InlineMemberCapability.InlineMember>> = newCache()
private val procs2: Cache<String, AnnotationProcessorDependency> = newCache()
private val inlineMembers: Cache<String, Set<InlineMemberCapability.InlineMember>> = newCache()
private val procs: Cache<String, AnnotationProcessorDependency> = newCache()

internal fun explodedJar(name: String): ExplodingJar? = explodingJars.asMap()[name]
internal fun explodedJars(name: String, explodingJar: ExplodingJar) {
explodingJars.asMap().putIfAbsent(name, explodingJar)
}

internal fun inlineMember(name: String): Set<InlineMemberCapability.InlineMember>? = inlineMembers2.asMap()[name]
internal fun inlineMember(name: String): Set<InlineMemberCapability.InlineMember>? = inlineMembers.asMap()[name]

internal fun inlineMembers(name: String, members: Set<InlineMemberCapability.InlineMember>) {
inlineMembers2.asMap().putIfAbsent(name, members)
inlineMembers.asMap().putIfAbsent(name, members)
}

internal fun proc(procName: String): AnnotationProcessorDependency? = procs2.asMap()[procName]
internal fun proc(procName: String): AnnotationProcessorDependency? = procs.asMap()[procName]
internal fun procs(procName: String, proc: AnnotationProcessorDependency) {
procs2.asMap().putIfAbsent(procName, proc)
procs.asMap().putIfAbsent(procName, proc)
}

companion object {
Expand Down

0 comments on commit 8373dde

Please sign in to comment.