From 42b9540cf6108d577b733f41097d35829f47c244 Mon Sep 17 00:00:00 2001 From: Xianwen Chen Date: Sat, 14 Sep 2019 01:30:13 -0700 Subject: [PATCH 1/4] Skip the unit import if the target unit file is newer than source unit file --- index-import.cpp | 48 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/index-import.cpp b/index-import.cpp index ccd3621..511ef89 100644 --- a/index-import.cpp +++ b/index-import.cpp @@ -98,10 +98,28 @@ struct ModuleNameScope { std::set _moduleNames; }; -static IndexUnitWriter remapUnit(const std::unique_ptr &reader, - const Remapper &remapper, FileManager &fileMgr, - ModuleNameScope &moduleNames, - std::ostream *outs) { +// Returns true if the Unit file of given output file already exists and is +// older than the input file. +static bool isUnitUpToDate(StringRef outputFile, + StringRef inputFile, + IndexUnitWriter &writer) { + std::string error; + auto IsUptodateOpt = writer.isUnitUpToDateForOutputFile(outputFile, inputFile, error); + if (!IsUptodateOpt.hasValue()) { + errs() << "error: failed file status check:\n" + << error << "\n"; + return false; + } + + return *IsUptodateOpt; +} + +// Returns None if the Unit file is already up to date +static Optional remapUnit(const std::string &inputUnitPath, + const std::unique_ptr &reader, + const Remapper &remapper, FileManager &fileMgr, + ModuleNameScope &moduleNames, + std::ostream *outs) { // The set of remapped paths. auto workingDir = remapper.remap(reader->getWorkingDirectory()); auto outputFile = remapper.remap(reader->getOutputFile()); @@ -124,6 +142,13 @@ static IndexUnitWriter remapUnit(const std::unique_ptr &reader, fileMgr.getFile(mainFilePath), reader->isSystemUnit(), reader->isModuleUnit(), reader->isDebugCompilation(), reader->getTarget(), sysrootPath, moduleNames.getModuleInfo); + + // Check if the unit file is already up to date + SmallString<256> outputFileFullPath; + path::append(outputFileFullPath, workingDir, outputFile); + if (isUnitUpToDate(outputFileFullPath, inputUnitPath, writer)) { + return None; + } reader->foreachDependency([&](const IndexUnitReader::DependencyInfo &info) { const auto name = info.UnitOrRecordName; @@ -307,13 +332,14 @@ static bool remapIndex(const Remapper &remapper, } ModuleNameScope moduleNames; - auto writer = remapUnit(reader, remapper, fileMgr, moduleNames, outs); - - std::string unitWriteError; - if (writer.write(unitWriteError)) { - errs() << "error: failed to write index store; " << unitWriteError - << "\n"; - success = false; + auto writer = remapUnit(unitPath, reader, remapper, fileMgr, moduleNames, outs); + if (writer.hasValue()) { + std::string unitWriteError; + if (writer->write(unitWriteError)) { + errs() << "error: failed to write index store; " << unitWriteError + << "\n"; + success = false; + } } } From 5b673924a27771dd97a5929b4e1c2c8ad22465f9 Mon Sep 17 00:00:00 2001 From: Xianwen Chen Date: Sat, 14 Sep 2019 13:05:00 -0700 Subject: [PATCH 2/4] Use StringRef instead of `std::string` --- index-import.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index-import.cpp b/index-import.cpp index 511ef89..fa5df5a 100644 --- a/index-import.cpp +++ b/index-import.cpp @@ -115,7 +115,7 @@ static bool isUnitUpToDate(StringRef outputFile, } // Returns None if the Unit file is already up to date -static Optional remapUnit(const std::string &inputUnitPath, +static Optional remapUnit(StringRef inputUnitPath, const std::unique_ptr &reader, const Remapper &remapper, FileManager &fileMgr, ModuleNameScope &moduleNames, From e93e485a7e40068115a1a9c060604f2ed83a7a75 Mon Sep 17 00:00:00 2001 From: Xianwen Chen Date: Mon, 6 Jan 2020 16:47:47 -0800 Subject: [PATCH 3/4] CR feedback --- index-import.cpp | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/index-import.cpp b/index-import.cpp index fa5df5a..5019ad3 100644 --- a/index-import.cpp +++ b/index-import.cpp @@ -100,26 +100,25 @@ struct ModuleNameScope { // Returns true if the Unit file of given output file already exists and is // older than the input file. -static bool isUnitUpToDate(StringRef outputFile, - StringRef inputFile, +static bool isUnitUpToDate(StringRef outputFile, StringRef inputFile, IndexUnitWriter &writer) { std::string error; - auto IsUptodateOpt = writer.isUnitUpToDateForOutputFile(outputFile, inputFile, error); - if (!IsUptodateOpt.hasValue()) { - errs() << "error: failed file status check:\n" - << error << "\n"; + auto isUptodateOpt = + writer.isUnitUpToDateForOutputFile(outputFile, inputFile, error); + if (!isUptodateOpt.hasValue()) { + errs() << "error: failed file status check:\n" << error << "\n"; return false; } - - return *IsUptodateOpt; + + return *isUptodateOpt; } // Returns None if the Unit file is already up to date -static Optional remapUnit(StringRef inputUnitPath, - const std::unique_ptr &reader, - const Remapper &remapper, FileManager &fileMgr, - ModuleNameScope &moduleNames, - std::ostream *outs) { +static Optional +remapUnit(StringRef inputUnitPath, + const std::unique_ptr &reader, + const Remapper &remapper, FileManager &fileMgr, + ModuleNameScope &moduleNames, std::ostream *outs) { // The set of remapped paths. auto workingDir = remapper.remap(reader->getWorkingDirectory()); auto outputFile = remapper.remap(reader->getOutputFile()); @@ -142,11 +141,16 @@ static Optional remapUnit(StringRef inputUnitPath, fileMgr.getFile(mainFilePath), reader->isSystemUnit(), reader->isModuleUnit(), reader->isDebugCompilation(), reader->getTarget(), sysrootPath, moduleNames.getModuleInfo); - + // Check if the unit file is already up to date - SmallString<256> outputFileFullPath; - path::append(outputFileFullPath, workingDir, outputFile); - if (isUnitUpToDate(outputFileFullPath, inputUnitPath, writer)) { + SmallString<256> remappedOutputFilePath; + if (outputFile[0] != '/') { + // Convert outputFile to absolute path + path::append(remappedOutputFilePath, workingDir, outputFile); + } else { + remappedOutputFilePath = outputFile; + } + if (isUnitUpToDate(remappedOutputFilePath, inputUnitPath, writer)) { return None; } @@ -332,12 +336,13 @@ static bool remapIndex(const Remapper &remapper, } ModuleNameScope moduleNames; - auto writer = remapUnit(unitPath, reader, remapper, fileMgr, moduleNames, outs); + auto writer = + remapUnit(unitPath, reader, remapper, fileMgr, moduleNames, outs); if (writer.hasValue()) { std::string unitWriteError; if (writer->write(unitWriteError)) { errs() << "error: failed to write index store; " << unitWriteError - << "\n"; + << "\n"; success = false; } } From 2448fb9dc64d420d867e000aa3baa54f4275227e Mon Sep 17 00:00:00 2001 From: Xianwen Chen Date: Mon, 6 Jan 2020 16:55:26 -0800 Subject: [PATCH 4/4] Rename `isUptodateOpt` to `isUpToDateOpt` --- index-import.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index-import.cpp b/index-import.cpp index 5019ad3..233cd4f 100644 --- a/index-import.cpp +++ b/index-import.cpp @@ -103,14 +103,14 @@ struct ModuleNameScope { static bool isUnitUpToDate(StringRef outputFile, StringRef inputFile, IndexUnitWriter &writer) { std::string error; - auto isUptodateOpt = + auto isUpToDateOpt = writer.isUnitUpToDateForOutputFile(outputFile, inputFile, error); - if (!isUptodateOpt.hasValue()) { + if (!isUpToDateOpt.hasValue()) { errs() << "error: failed file status check:\n" << error << "\n"; return false; } - return *isUptodateOpt; + return *isUpToDateOpt; } // Returns None if the Unit file is already up to date