forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Intern module extension repo mapping entries via a
SkyFunction
Since 74aadb2, the entries of `RepositoryMapping`s are interned to prevent quadratic memory usage for module extensions that create many repos. By using a dedicated `SkyFunction` instead of an interner, the CPU time spent on computing the identical repository mappings for extension repos, which showed up in profiles, is saved in addition to the memory usage. Closes bazelbuild#20095. PiperOrigin-RevId: 581177545 Change-Id: Ied5682ba90f5f771fedffbea1491e7b2f6c27145
- Loading branch information
1 parent
e2c0276
commit 07a571f
Showing
16 changed files
with
203 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...com/google/devtools/build/lib/bazel/bzlmod/ModuleExtensionRepoMappingEntriesFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright 2023 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
package com.google.devtools.build.lib.bazel.bzlmod; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.google.devtools.build.lib.cmdline.RepositoryMapping; | ||
import com.google.devtools.build.lib.cmdline.RepositoryName; | ||
import com.google.devtools.build.skyframe.SkyFunction; | ||
import com.google.devtools.build.skyframe.SkyKey; | ||
import com.google.devtools.build.skyframe.SkyValue; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* A SkyFunction that computes the {@link RepositoryMapping#entries()} for the repos generated by a | ||
* module extension. While the repos differ in {@link RepositoryMapping#ownerRepo()}, the entries | ||
* are identical and sharing the instance ensures that memory and CPU usage are only linear in the | ||
* number of repos. | ||
*/ | ||
public class ModuleExtensionRepoMappingEntriesFunction implements SkyFunction { | ||
|
||
@Override | ||
@Nullable | ||
public SkyValue compute(SkyKey skyKey, Environment env) throws InterruptedException { | ||
BazelDepGraphValue bazelDepGraphValue = | ||
(BazelDepGraphValue) env.getValue(BazelDepGraphValue.KEY); | ||
if (bazelDepGraphValue == null) { | ||
return null; | ||
} | ||
|
||
var moduleExtensionId = ((ModuleExtensionRepoMappingEntriesValue.Key) skyKey).argument(); | ||
var extensionEvalValue = | ||
(SingleExtensionEvalValue) env.getValue(SingleExtensionEvalValue.key(moduleExtensionId)); | ||
if (extensionEvalValue == null) { | ||
return null; | ||
} | ||
|
||
return computeRepoMappingEntries(moduleExtensionId, extensionEvalValue, bazelDepGraphValue); | ||
} | ||
|
||
/** | ||
* Calculates repo mappings for a repo generated from a module extension. Such a repo can see all | ||
* repos generated by the same module extension, as well as all repos that the Bazel module | ||
* hosting the extension can see (see above). | ||
*/ | ||
private ModuleExtensionRepoMappingEntriesValue computeRepoMappingEntries( | ||
ModuleExtensionId extensionId, | ||
SingleExtensionEvalValue extensionEvalValue, | ||
BazelDepGraphValue bazelDepGraphValue) { | ||
// Find the key of the module containing this extension. This will be used to compute additional | ||
// mappings -- any repo generated by an extension contained in the module "foo" can additionally | ||
// see all repos that "foo" can see. | ||
ModuleKey moduleKey = | ||
bazelDepGraphValue | ||
.getCanonicalRepoNameLookup() | ||
.get(extensionId.getBzlFileLabel().getRepository()); | ||
// NOTE(wyv): This means that if "foo" has a bazel_dep with the repo name "bar", and the | ||
// extension generates an internal repo name "bar", then within a repo generated by the | ||
// extension, "bar" will refer to the latter. We should explore a way to differentiate between | ||
// the two to avoid any surprises. | ||
ImmutableMap.Builder<String, RepositoryName> entries = ImmutableMap.builder(); | ||
entries.putAll(bazelDepGraphValue.getFullRepoMapping(moduleKey).entries()); | ||
entries.putAll(extensionEvalValue.getCanonicalRepoNameToInternalNames().inverse()); | ||
return ModuleExtensionRepoMappingEntriesValue.create(entries.buildKeepingLast(), moduleKey); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...va/com/google/devtools/build/lib/bazel/bzlmod/ModuleExtensionRepoMappingEntriesValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2023 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
package com.google.devtools.build.lib.bazel.bzlmod; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.devtools.build.lib.cmdline.RepositoryName; | ||
import com.google.devtools.build.lib.skyframe.SkyFunctions; | ||
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; | ||
import com.google.devtools.build.skyframe.AbstractSkyKey; | ||
import com.google.devtools.build.skyframe.SkyFunctionName; | ||
import com.google.devtools.build.skyframe.SkyKey; | ||
import com.google.devtools.build.skyframe.SkyValue; | ||
|
||
/** The value for {@link ModuleExtensionRepoMappingEntriesFunction}. */ | ||
@AutoCodec | ||
@AutoValue | ||
public abstract class ModuleExtensionRepoMappingEntriesValue implements SkyValue { | ||
|
||
public abstract ImmutableMap<String, RepositoryName> getEntries(); | ||
|
||
public abstract ModuleKey getModuleKey(); | ||
|
||
@AutoCodec.Instantiator | ||
public static ModuleExtensionRepoMappingEntriesValue create( | ||
ImmutableMap<String, RepositoryName> entries, ModuleKey moduleKey) { | ||
return new AutoValue_ModuleExtensionRepoMappingEntriesValue(entries, moduleKey); | ||
} | ||
|
||
public static ModuleExtensionRepoMappingEntriesValue.Key key(ModuleExtensionId id) { | ||
return ModuleExtensionRepoMappingEntriesValue.Key.create(id); | ||
} | ||
|
||
/** | ||
* The {@link com.google.devtools.build.skyframe.SkyKey} of a {@link | ||
* ModuleExtensionRepoMappingEntriesValue}. | ||
*/ | ||
@AutoCodec | ||
public static class Key extends AbstractSkyKey<ModuleExtensionId> { | ||
|
||
private static final SkyKeyInterner<ModuleExtensionRepoMappingEntriesValue.Key> interner = | ||
SkyKey.newInterner(); | ||
|
||
protected Key(ModuleExtensionId arg) { | ||
super(arg); | ||
} | ||
|
||
@AutoCodec.Instantiator | ||
static ModuleExtensionRepoMappingEntriesValue.Key create(ModuleExtensionId arg) { | ||
return interner.intern(new ModuleExtensionRepoMappingEntriesValue.Key(arg)); | ||
} | ||
|
||
@Override | ||
public SkyFunctionName functionName() { | ||
return SkyFunctions.MODULE_EXTENSION_REPO_MAPPING_ENTRIES; | ||
} | ||
|
||
@Override | ||
public SkyKeyInterner<ModuleExtensionRepoMappingEntriesValue.Key> getSkyKeyInterner() { | ||
return interner; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.