Skip to content

Commit 86be1d1

Browse files
Track changes to prevent unnecessary disk io
1 parent 114cfe9 commit 86be1d1

File tree

6 files changed

+56
-12
lines changed

6 files changed

+56
-12
lines changed

Include/runcpp2/Data/ScriptInfo.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "runcpp2/Data/ProfilesProcessPaths.hpp"
88
#include "runcpp2/Data/ProfilesDefines.hpp"
99
#include "runcpp2/Data/ProfilesCommands.hpp"
10+
#include "ghc/filesystem.hpp"
1011

1112
#include <string>
1213
#include <vector>
@@ -38,8 +39,12 @@ namespace runcpp2
3839
std::unordered_map<PlatformName, ProfilesCommands> PostBuild;
3940
std::unordered_map<PlatformName, ProfilesCommands> Cleanup;
4041

42+
//Internal tracking
4143
bool Populated = false;
4244

45+
ghc::filesystem::file_time_type LastWriteTime =
46+
ghc::filesystem::file_time_type::min();
47+
4348
bool ParseYAML_Node(ryml::ConstNodeRef& node);
4449
std::string ToString(std::string indentation) const;
4550
bool Equals(const ScriptInfo& other) const;

Include/runcpp2/PipelineSteps.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ namespace runcpp2
125125

126126
using SourceIncludeMap = std::unordered_map<std::string, std::vector<ghc::filesystem::path>>;
127127

128-
bool GatherFilesIncludes( const std::vector<ghc::filesystem::path>& files,
128+
bool GatherFilesIncludes( const std::vector<ghc::filesystem::path>& sourceFiles,
129129
const std::vector<ghc::filesystem::path>& includePaths,
130130
SourceIncludeMap& outSourceIncludes);
131131
}

Include/runcpp2/runcpp2.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ namespace runcpp2
2929
const std::vector<Data::Profile>& profiles,
3030
const std::string& configPreferredProfile,
3131
const Data::ScriptInfo& scriptInfo,
32-
const std::unordered_map<CmdOptions, std::string>& currentOptions,
32+
const std::unordered_map< CmdOptions,
33+
std::string>& currentOptions,
3334
bool& outNeedsUpdate);
3435

3536
PipelineResult StartPipeline( const std::string& scriptPath,

Src/runcpp2/PipelineSteps.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,6 @@ runcpp2::ParseAndValidateScriptInfo(const ghc::filesystem::path& absoluteScriptP
309309
ssLOG_FUNC_INFO();
310310
INTERNAL_RUNCPP2_SAFE_START();
311311

312-
//TODO: Record last script info write time. Use last script info if possible to reduce disk io
313-
314312
//Check if there's script info as yaml file instead
315313
std::error_code e;
316314
std::string parsableInfo;
@@ -320,13 +318,29 @@ runcpp2::ParseAndValidateScriptInfo(const ghc::filesystem::path& absoluteScriptP
320318

321319
if(ghc::filesystem::exists(dedicatedYamlLoc, e))
322320
{
321+
//Record write time for yaml file
322+
outScriptInfo.LastWriteTime = ghc::filesystem::last_write_time(dedicatedYamlLoc, e);
323+
if(e)
324+
{
325+
ssLOG_ERROR("Failed to get last write time for: " << dedicatedYamlLoc);
326+
return PipelineResult::INVALID_SCRIPT_INFO;
327+
}
328+
323329
inputFile.open(dedicatedYamlLoc);
324330
std::stringstream buffer;
325331
buffer << inputFile.rdbuf();
326332
parsableInfo = buffer.str();
327333
}
328334
else
329335
{
336+
//Record write time for script file
337+
outScriptInfo.LastWriteTime = ghc::filesystem::last_write_time(absoluteScriptPath, e);
338+
if(e)
339+
{
340+
ssLOG_ERROR("Failed to get last write time for: " << absoluteScriptPath);
341+
return PipelineResult::INVALID_SCRIPT_INFO;
342+
}
343+
330344
inputFile.open(absoluteScriptPath);
331345

332346
if (!inputFile)
@@ -1192,7 +1206,7 @@ bool runcpp2::GatherIncludePaths( const ghc::filesystem::path& scriptDirectory
11921206
INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(false);
11931207
}
11941208

1195-
bool runcpp2::GatherFilesIncludes( const std::vector<ghc::filesystem::path>& files,
1209+
bool runcpp2::GatherFilesIncludes( const std::vector<ghc::filesystem::path>& sourceFiles,
11961210
const std::vector<ghc::filesystem::path>& includePaths,
11971211
SourceIncludeMap& outSourceIncludes)
11981212
{
@@ -1202,13 +1216,13 @@ bool runcpp2::GatherFilesIncludes( const std::vector<ghc::filesystem::path>& fi
12021216
outSourceIncludes.clear();
12031217
std::unordered_set<std::string> visitedFiles;
12041218

1205-
for(const ghc::filesystem::path& file : files)
1219+
for(const ghc::filesystem::path& source : sourceFiles)
12061220
{
1207-
ssLOG_INFO("Gathering includes for " << file.string());
1221+
ssLOG_INFO("Gathering includes for " << source.string());
12081222

1209-
std::vector<ghc::filesystem::path>& currentIncludes = outSourceIncludes[file.string()];
1223+
std::vector<ghc::filesystem::path>& currentIncludes = outSourceIncludes[source.string()];
12101224
std::queue<ghc::filesystem::path> filesToProcess;
1211-
filesToProcess.push(file);
1225+
filesToProcess.push(source);
12121226

12131227
while(!filesToProcess.empty())
12141228
{

Src/runcpp2/runcpp2.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,8 @@ runcpp2::CheckSourcesNeedUpdate( const std::string& scriptPath,
306306
const std::vector<Data::Profile>& profiles,
307307
const std::string& configPreferredProfile,
308308
const Data::ScriptInfo& scriptInfo,
309-
const std::unordered_map<CmdOptions, std::string>& currentOptions,
309+
const std::unordered_map< CmdOptions,
310+
std::string>& currentOptions,
310311
bool& outNeedsUpdate)
311312
{
312313
INTERNAL_RUNCPP2_SAFE_START();
@@ -325,6 +326,30 @@ runcpp2::CheckSourcesNeedUpdate( const std::string& scriptPath,
325326
if(result != PipelineResult::SUCCESS)
326327
return result;
327328

329+
//First check if script info file has changed
330+
std::error_code e;
331+
ghc::filesystem::path dedicatedYamlLoc =
332+
scriptDirectory / ghc::filesystem::path(scriptName + ".yaml");
333+
334+
ghc::filesystem::file_time_type currentWriteTime;
335+
if(ghc::filesystem::exists(dedicatedYamlLoc, e))
336+
currentWriteTime = ghc::filesystem::last_write_time(dedicatedYamlLoc, e);
337+
else
338+
currentWriteTime = ghc::filesystem::last_write_time(absoluteScriptPath, e);
339+
340+
if(e)
341+
{
342+
ssLOG_ERROR("Failed to get write time for script info");
343+
return PipelineResult::UNEXPECTED_FAILURE;
344+
}
345+
346+
//If script info file is newer than last check, we need to update
347+
if(currentWriteTime > scriptInfo.LastWriteTime)
348+
{
349+
outNeedsUpdate = true;
350+
return PipelineResult::SUCCESS;
351+
}
352+
328353
//Initialize BuildsManager and IncludeManager
329354
ghc::filesystem::path configDir = GetConfigFilePath();
330355
ghc::filesystem::path buildDir;
@@ -506,7 +531,7 @@ runcpp2::StartPipeline( const std::string& scriptPath,
506531
if(result != PipelineResult::SUCCESS)
507532
return result;
508533

509-
if(!lastScriptInfo || recompileNeeded || !changedDependencies.empty() || relinkNeeded)
534+
//if(!lastScriptInfo || recompileNeeded || !changedDependencies.empty() || relinkNeeded)
510535
outScriptInfo = scriptInfo;
511536

512537
std::vector<std::string> gatheredBinariesPaths;

TODO.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ TODO:
1515
- Add branch/tag option for git
1616
- Add initialize submodule option for git
1717
- Use System2 subprocess if no prepend commands to be safer
18-
- Record last script info write time for `ParseAndValidateScriptInfo()`
1918
- Migrate to libyaml
2019
- Add wildcard support for filenames and extensions
2120
- Add tests and examples (On Windows as well)

0 commit comments

Comments
 (0)