mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
When looking for a Swift module on disk, we were scanning all module search paths if they contain the module we are searching for. In a setup where each module is contained in its own framework search path, this scaled quadratically with the number of modules being imported. E.g. a setup with 100 modules being imported form 100 module search paths could cause on the order of 10,000 checks of `FileSystem::exists`. While these checks are fairly fast (~10µs), they add up to ~100ms. To improve this, perform a first scan of all module search paths and list the files they contain. From this, create a lookup map that maps filenames to the search paths they can be found in. E.g. for ``` searchPath1/ Module1.framework searchPath2/ Module1.framework Module2.swiftmodule ``` we create the following lookup table ``` Module1.framework -> [searchPath1, searchPath2] Module2.swiftmodule -> [searchPath2] ```
80 lines
2.7 KiB
C++
80 lines
2.7 KiB
C++
//===--- PathRemapper.h - Transforms path prefixes --------------*- C++ -*-===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file defines a data structure that stores a string-to-string
|
|
// mapping used to transform file paths based on a prefix mapping. It
|
|
// is optimized for the common case, which is that there will be
|
|
// extremely few mappings (i.e., one or two).
|
|
//
|
|
// Remappings are stored such that they are applied in the order they
|
|
// are passed on the command line. This would only matter if one
|
|
// source mapping was a prefix of another.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SWIFT_BASIC_PATHREMAPPER_H
|
|
#define SWIFT_BASIC_PATHREMAPPER_H
|
|
|
|
#include "swift/Basic/LLVM.h"
|
|
#include "llvm/ADT/SmallVector.h"
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
namespace swift {
|
|
|
|
class PathRemapper {
|
|
SmallVector<std::pair<std::string, std::string>, 2> PathMappings;
|
|
|
|
public:
|
|
/// Adds a mapping such that any paths starting with `FromPrefix` have that
|
|
/// portion replaced with `ToPrefix`.
|
|
void addMapping(StringRef FromPrefix, StringRef ToPrefix) {
|
|
PathMappings.emplace_back(FromPrefix.str(), ToPrefix.str());
|
|
}
|
|
|
|
/// Returns a remapped `Path` if it starts with a prefix in the map; otherwise
|
|
/// the original path is returned.
|
|
std::string remapPath(StringRef Path) const {
|
|
// Clang's implementation of this feature also compares the path string
|
|
// directly instead of treating path segments as indivisible units. The
|
|
// latter would arguably be more accurate, but we choose to preserve
|
|
// compatibility with Clang (especially because we propagate the flag to
|
|
// ClangImporter as well).
|
|
for (const auto &Mapping : PathMappings)
|
|
if (Path.startswith(Mapping.first))
|
|
return (Twine(Mapping.second) +
|
|
Path.substr(Mapping.first.size())).str();
|
|
return Path.str();
|
|
}
|
|
};
|
|
|
|
class PathObfuscator {
|
|
PathRemapper obfuscator, recoverer;
|
|
public:
|
|
void addMapping(StringRef FromPrefix, StringRef ToPrefix) {
|
|
obfuscator.addMapping(FromPrefix, ToPrefix);
|
|
recoverer.addMapping(ToPrefix, FromPrefix);
|
|
}
|
|
std::string obfuscate(StringRef Path) const {
|
|
return obfuscator.remapPath(Path);
|
|
}
|
|
std::string recover(StringRef Path) const {
|
|
return recoverer.remapPath(Path);
|
|
}
|
|
};
|
|
|
|
} // end namespace swift
|
|
|
|
#endif // SWIFT_BASIC_PATHREMAPPER_H
|