Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
30f3607
ADFA-6114: Store the dependency graph flat instead of nested
itsaky-adfa Sep 17, 2026
8a22902
ADFA-6114: Cover dependency-graph flattening
itsaky-adfa Sep 17, 2026
f2bab07
ADFA-6114: Discard sync files written by an older schema version
itsaky-adfa Sep 18, 2026
71a436b
ADFA-6114: Cover the sync-file schema version gate
itsaky-adfa Sep 18, 2026
3a8e60f
ADFA-6114: Gate the cache on the schema version at the parse site
itsaky-adfa Sep 18, 2026
b8c8a11
ADFA-6114: Drop the redundant key table and correct the node docs
itsaky-adfa Sep 18, 2026
91548b1
ADFA-6114: Cover the version gate and report failed sync-file deletes
itsaky-adfa Sep 18, 2026
0186afd
ADFA-6114: Fix the stale collectLibraries docs and inline the single-…
itsaky-adfa Sep 18, 2026
287ebd0
ADFA-6114: Keep a failed sync-file discard from crashing the project …
itsaky-adfa Sep 18, 2026
9c40761
ADFA-6114: Recheck sync-file staleness under the lock before discarding
itsaky-adfa Sep 18, 2026
d495d0e
ADFA-6114: Keep an unchecked parse failure from escaping the sync-fil…
itsaky-adfa Sep 18, 2026
1349d75
ADFA-6114: Put the widened catch on the discard path, not the lock ac…
itsaky-adfa Sep 18, 2026
3db6c9f
ADFA-6114: Serialise sync-lock acquisition within the process
itsaky-adfa Sep 18, 2026
1512ab2
ADFA-6114: Skip out-of-range graph indices instead of throwing
itsaky-adfa Sep 18, 2026
d63b1aa
ADFA-6114: Cover coordinate interning and the transitive graph walk
itsaky-adfa Sep 18, 2026
ac1123d
ADFA-6114: Return the sync-lock permit on every failed acquire
itsaky-adfa Sep 18, 2026
d99c368
ADFA-6114: Write the sync metadata atomically
itsaky-adfa Sep 18, 2026
a3a8526
ADFA-6114: Cover the out-of-range graph index skip
itsaky-adfa Sep 18, 2026
cf8a365
ADFA-6114: Cover the server-side schema version gate
itsaky-adfa Sep 18, 2026
3f23ffc
ADFA-6114: Unmock the singletons these server tests replace
itsaky-adfa Sep 18, 2026
b554610
ADFA-6114: Always attempt the file lock once before giving up
itsaky-adfa Sep 18, 2026
fc06de1
ADFA-6114: Report a damaged dependency graph instead of skipping quietly
itsaky-adfa Sep 18, 2026
85a5299
ADFA-6114: Cover the out-of-range key index too
itsaky-adfa Sep 18, 2026
b787220
ADFA-6114: Log a dangling node index at both graph sites
itsaky-adfa Sep 18, 2026
eb22311
ADFA-6114: Truncate the temp file when publishing a model atomically
itsaky-adfa Sep 18, 2026
637a0fb
ADFA-6114: Report damaged graph indices once per traversal
itsaky-adfa Sep 18, 2026
7ed8633
ADFA-6114: Clear the temp file when an atomic publish fails
itsaky-adfa Sep 18, 2026
c16b817
ADFA-6114: Assert the sync lock key on the path, not the acquire result
itsaky-adfa Sep 18, 2026
72942be
ADFA-6114: Reset the damage flag only for a traversal that runs
itsaky-adfa Sep 18, 2026
e2dd170
ADFA-6114: Leak the descriptor rather than drop another holder's lock
itsaky-adfa Sep 18, 2026
94665a5
ADFA-6114: Name the index that is actually out of range
itsaky-adfa Sep 18, 2026
7642af9
ADFA-6114: Answer null when the sync lock directory cannot be created
itsaky-adfa Sep 18, 2026
c0d4963
ADFA-6114: State the real reason each consumer is unaffected
itsaky-adfa Sep 18, 2026
da16b1b
ADFA-6114: Record why differing children per occurrence cannot happen
itsaky-adfa Sep 18, 2026
d036d09
ADFA-6114: Restore the missing-checksum diagnostic
itsaky-adfa Sep 18, 2026
110877c
ADFA-6114: Stub the overload the server calls and treat empty coordin…
itsaky-adfa Sep 18, 2026
a6cd84f
Merge branch 'stage' into feat/ADFA-6114-dependency-graph
itsaky-adfa Sep 22, 2026
1507f87
Merge branch 'stage' into feat/ADFA-6114-dependency-graph
itsaky-adfa Sep 23, 2026
bb05a42
ADFA-6114: Force the model to disk before publishing it
itsaky-adfa Sep 23, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 56 additions & 15 deletions subprojects/project-models/src/main/proto/android.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,64 @@ enum ProjectType {
* A node in a dependency graph, representing a direct or transitive dependency.
*
* This does not directly contain artifact information, instead it focuses on the graph
* information (transitive dependencies) as well as the usage of this particular dependency
* in this node of the graph (ie what are its modifiers: what version was originally requested.)
* information (transitive dependencies).
*
* Nodes are deduplicated by key, so there is one node per resolved artifact rather than one per
* usage. Per-usage detail is therefore no longer representable: see [requestedCoordinatesId].
*/
message GraphItem {
message GraphNode {

/**
* A Unique key representing the library, and allowing to match it with a [Library] instance
* Index into [DependencyGraph.key] of this node's unique library key, which matches it with a
* [Library] instance.
*/
string key = 1;
uint32 keyId = 1;

/**
* Returns this library's Maven coordinates, as requested in the build file.
* Index into [DependencyGraph.requestedCoordinates] of the Maven coordinates this library was
* requested with in the build file.
*
* Requested coordinates are a property of a usage, not of the resolved artifact, so where several
* dependents request the same artifact at different coordinates this records only the occurrence
* the flattening reached first.
*
* Check presence before reading: unset reads back as 0, which is also a valid index into
* [DependencyGraph.requestedCoordinates].
*/
optional string requestedCoordinates = 2;
optional uint32 requestedCoordinatesId = 2;

/**
* Return the direct dependency of this node.
* Indices into [DependencyGraph.node] of this node's direct dependencies.
*/
repeated GraphItem dependency = 3;
repeated uint32 dependency = 3;
}

/**
* A dependency graph, stored flat.
*
* The previous representation nested each node's children inline, so a dependency DAG serialised as
* a tree and every path to a shared artifact duplicated that artifact's entire subtree. Measured on
* an 86-module project that produced 786,554 nodes holding 1,011,910 key strings for 57,517 distinct
* values -- a 17.6x duplication, with one kotlin-stdlib key repeated 180,738 times.
*
* Nodes are deduplicated by key and strings interned, so each artifact and each string appears once.
* The compile-classpath consumer already expands each key at most once via a visited set, over the
* same pre-order walk, so it sees the same graph. The module-dependency consumer reads only the
* roots, whose order and multiplicity are unchanged.
*/
message DependencyGraph {

/** Interned node keys, indexed by [GraphNode.keyId]. */
repeated string key = 1;

/** Interned requested coordinates, indexed by [GraphNode.requestedCoordinatesId]. */
repeated string requestedCoordinates = 2;

/** Every distinct node, deduplicated by key. */
repeated GraphNode node = 3;

/** Indices into [node] of the graph's direct dependencies. */
repeated uint32 root = 4;
}

// An unresolved dependency.
Expand All @@ -53,15 +91,18 @@ message UnresolvedDependency {

// The dependencies information for a given artifact.
//
// It contains the compile graph, always, and optionally the runtime graph.
// It contains the compile graph. The runtime graph is omitted for now.
//
// Each graph is fairly lightweight, with each artifact node being mostly an address, children,
// and modifiers that are specific to this particular usage of the artifact rather than
// artifact properties.
// The graph is stored flat: nodes are deduplicated by key and strings interned, so each artifact
// appears once and carries an address plus its children. Per-usage modifiers do not survive that
// deduplication -- see GraphNode.
message ArtifactDependencies {

// The compile dependency graph.
repeated GraphItem compileDependency = 1;
// The compile dependency graph, stored flat. See DependencyGraph.
DependencyGraph compileGraph = 3;

reserved 1;
reserved "compileDependency";

// NOTE: We omit runtime dependencies for now.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ open class AndroidModule(
return emptySet()
}

// After the guard: a cyclic re-entry must not clear the flag while the outer walk is running.
reportedGraphDamage = false

val result = mutableSetOf<File>()
if (excludeSourceGeneratedClassPath) {
// TODO: The mainArtifact.classJars are technically generated from source files
Expand All @@ -163,9 +166,11 @@ open class AndroidModule(
} else {
result.addAll(getModuleClasspaths())
}
val graph = variantDependencies.mainArtifact.compileGraph
collectLibraries(
root = project,
libraries = variantDependencies.mainArtifact?.compileDependencyList ?: emptyList(),
graph = graph,
nodeIds = graph.rootList,
result = result,
excludeSourceGeneratedClassPath = excludeSourceGeneratedClassPath,
visited = HashSet(),
Expand Down Expand Up @@ -251,11 +256,46 @@ open class AndroidModule(
}

/**
* Recursively collect the compile classpath entries contributed by the given dependency-graph
* [libraries] into [result], guarding against cycles.
* Whether the current traversal has already reported a damaged dependency-graph index.
*
* Deliberately unsynchronised: two concurrent classpath computations can cost an extra log line
* or swallow one, neither of which is worth a lock on this path.
*/
@Volatile
private var reportedGraphDamage = false

/**
* Report the first out-of-range graph index seen in the current traversal.
*
* A truncated node table makes every index dangle, so warning per index would put tens of
* thousands of lines in the log on every classpath refresh. One line per traversal says the
* same thing.
*/
private fun logLostGraphEntry(
nodeId: Int,
keyId: Int?,
) {
if (reportedGraphDamage) {
return
}

reportedGraphDamage = true
val entry = if (keyId == null) "node index $nodeId" else "key index $keyId of node $nodeId"
log.warn(
"Dependency graph {} in module {} is out of range; the project cache looks damaged." +
" Re-sync the project if symbols fail to resolve.",
entry,
path,
)
}

/**
* Recursively collect the compile classpath entries contributed by the [graph] nodes at
* [nodeIds] into [result], guarding against cycles.
*
* @param root The workspace used to resolve project dependencies by path.
* @param libraries The dependency-graph nodes to expand at this level.
* @param graph The flat dependency graph the node indices address.
* @param nodeIds Indices into [graph] of the nodes to expand at this level.
* @param result The accumulating set of classpath files.
* @param excludeSourceGeneratedClassPath Whether to exclude source-generated classpath entries.
* @param visited Keys of dependency-graph nodes already expanded within this module; each node is
Expand All @@ -265,20 +305,34 @@ open class AndroidModule(
*/
private fun collectLibraries(
root: Workspace,
libraries: List<AndroidModels.GraphItem>,
graph: AndroidModels.DependencyGraph,
nodeIds: List<Int>,
result: MutableSet<File>,
excludeSourceGeneratedClassPath: Boolean,
visited: MutableSet<String>,
moduleVisited: MutableSet<String>,
) {
val libraryMap = variantDependencies.librariesMap
for (library in libraries) {
for (nodeId in nodeIds) {
/*
* Indices come off disk. The nested form carried each key inline, so a damaged cache
* cost a classpath entry; addressing by index would turn the same damage into an
* IndexOutOfBoundsException out of a classpath getter, so skip instead. A silently
* short classpath reads as phantom unresolved symbols, so say so in the log.
*/
val node = graph.nodeList.getOrNull(nodeId)
val key = node?.let { graph.keyList.getOrNull(it.keyId) }
if (key == null) {
logLostGraphEntry(nodeId, node?.keyId)
continue
}

// Guard against cyclic dependency graphs within this module: expand each graph node once.
if (!visited.add(library.key)) {
if (!visited.add(key)) {
continue
}

val lib = libraryMap[library.key] ?: continue
val lib = libraryMap[key] ?: continue
when {
lib.type == AndroidModels.LibraryType.Project -> {
val module = root.findByPath(lib.projectInfo!!.projectPath) ?: continue
Expand All @@ -301,7 +355,8 @@ open class AndroidModule(

collectLibraries(
root = root,
libraries = library.dependencyList,
graph = graph,
nodeIds = node.dependencyList,
result = result,
excludeSourceGeneratedClassPath = excludeSourceGeneratedClassPath,
visited = visited,
Expand All @@ -328,14 +383,24 @@ open class AndroidModule(
return emptyList()
}

// After the guard: a cyclic re-entry must not clear the flag while the outer walk is running.
reportedGraphDamage = false

recursionPath.addLast(path)
try {
val result = mutableListOf<ModuleProject>()

val libraries = variantDependencies.mainArtifact.compileDependencyList
val graph = variantDependencies.mainArtifact.compileGraph
val libraryMap = variantDependencies.librariesMap
for (library in libraries) {
val lib = libraryMap[library.key] ?: continue
for (nodeId in graph.rootList) {
val node = graph.nodeList.getOrNull(nodeId)
val key = node?.let { graph.keyList.getOrNull(it.keyId) }
if (key == null) {
logLostGraphEntry(nodeId, node?.keyId)
continue
}

val lib = libraryMap[key] ?: continue
if (lib.type != AndroidModels.LibraryType.Project) {
continue
}
Expand Down
Loading
Loading