Refactor all ctor callsites of PathFragment to instead call a static 'create' method.
This paves the way for changing PathFragment to e.g. an abstract class with multiple subclasses. This way we can split out the windows-specific stuff into one of these concrete classes, making the code more readable and also saving memory (since the shallow heap size of the NonWindowsPathFragment subclass will hopefully be smaller than that of the current PathFragment).
This also lets us pursue gc churn optimizations. We can now do interning in PathFragment#create and can also get rid of unnecessary intermediate PathFragment allocations.
RELNOTES: None
PiperOrigin-RevId: 152145768
diff --git a/src/main/java/com/google/devtools/build/lib/packages/Package.java b/src/main/java/com/google/devtools/build/lib/packages/Package.java
index e4f7261..1556dcb14 100644
--- a/src/main/java/com/google/devtools/build/lib/packages/Package.java
+++ b/src/main/java/com/google/devtools/build/lib/packages/Package.java
@@ -526,7 +526,7 @@
// stat(2) is executed.
Path filename = getPackageDirectory().getRelative(targetName);
String suffix;
- if (!new PathFragment(targetName).isNormalized()) {
+ if (!PathFragment.create(targetName).isNormalized()) {
// Don't check for file existence in this case because the error message
// would be confusing and wrong. If the targetName is "foo/bar/.", and
// there is a directory "foo/bar", it doesn't mean that "//pkg:foo/bar/."
@@ -1242,7 +1242,7 @@
// Now, modify the package:
for (OutputFile outputFile : rule.getOutputFiles()) {
targets.put(outputFile.getName(), outputFile);
- PathFragment outputFileFragment = new PathFragment(outputFile.getName());
+ PathFragment outputFileFragment = PathFragment.create(outputFile.getName());
for (int i = 1; i < outputFileFragment.segmentCount(); i++) {
String prefix = outputFileFragment.subFragment(0, i).toString();
if (!outputFilePrefixes.containsKey(prefix)) {
@@ -1430,7 +1430,7 @@
}
// Check if a prefix of this output file matches an already existing one
- PathFragment outputFileFragment = new PathFragment(outputFileName);
+ PathFragment outputFileFragment = PathFragment.create(outputFileName);
for (int i = 1; i < outputFileFragment.segmentCount(); i++) {
String prefix = outputFileFragment.subFragment(0, i).toString();
if (outputFiles.containsKey(prefix)) {