As sourceFileContentMap may be significantly larger than sourceFileMap, iterate over sourceFileMap looking for entries in sourceFileContentMap rather than the other way around.
Reduce the cost of SourceMapGeneratorV3#addSourcesContentMap by ~90%.  In a sample app this is a savings of ~90s.
@@ -134,6 +134,7 @@ public final class SourceMapGeneratorV3 implements SourceMapGenerator {
*/
@Override
public void reset() {
+ // Do not reset sourceFileContentMap
mappings.clear();
lastMapping = null;
sourceFileMap.clear();
@@ -495,13 +496,17 @@ public final class SourceMapGeneratorV3 implements SourceMapGenerator {
int size = sourceFileMap.size();
List<String> contents = new ArrayList<>(size);
contents.addAll(Collections.nCopies(size, ""));
- for (Map.Entry<String, String> entry : sourceFileContentMap.entrySet()) {
- Integer index = sourceFileMap.get(entry.getKey());
- if (index != null && index < size) {
- contents.set(index, entry.getValue());
+
+ for (Map.Entry<String, Integer> entry : sourceFileMap.entrySet()) {
+ Integer index = entry.getValue();
+ checkState(index < size);
+ String content = sourceFileContentMap.get(entry.getKey());
+ if (content != null) {
+ contents.set(index, content);
found = true;
}
}
+
if (!found) {
return;
}
