diff --git a/src/context.cpp b/src/context.cpp index 2c51db2230..ee75eee2e1 100644 --- a/src/context.cpp +++ b/src/context.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include "ast.hpp" #include "util.hpp" @@ -356,6 +357,11 @@ namespace Sass { // process the resolved entry else if (resolved.size() == 1) { bool use_cache = c_importers.size() == 0; + // register needed values for parallel lazy import + lazy_imports.push_back(std::make_tuple(resolved[0], pstate, use_cache)); + // return immediately + return resolved[0]; + // use cache for the resource loading if (use_cache && sheets.count(resolved[0].abs_path)) return resolved[0]; // try to read the content of the resolved file entry @@ -366,6 +372,7 @@ namespace Sass { // return resolved entry return resolved[0]; } + } // nothing found @@ -543,6 +550,17 @@ namespace Sass { } } + void reg_async(Context* ctx, Include resolved, ParserState pstate, bool use_cache) { + if (!use_cache || !ctx->sheets.count(resolved.abs_path)) { + // try to read the content of the resolved file entry + // the memory buffer returned must be freed by us! + if (char* contents = read_file(resolved.abs_path)) { + // register the newly resolved file resource + ctx->register_resource(resolved, { contents, 0 }, &pstate); + } + } + } + Block_Obj File_Context::parse() { @@ -584,6 +602,25 @@ namespace Sass { // create the source entry for file entry register_resource({{ input_path, "." }, abs_path }, { contents, 0 }); + std::vector thrds; + + // create parser threads + while(!lazy_imports.empty()) { + auto lazy = lazy_imports.back(); + Include resolved = std::get<0>(lazy); + ParserState pstate = std::get<1>(lazy); + bool use_cache = std::get<2>(lazy); + std::thread thrd(reg_async, this, resolved, pstate, use_cache); + thrds.push_back(std::move(thrd)); + lazy_imports.pop_back(); + } + + // join all threads + while (!thrds.empty()) { + thrds.back().join(); + thrds.pop_back(); + } + // create root ast tree node return compile(); diff --git a/src/context.hpp b/src/context.hpp index 07a21a5d22..e63f0842b3 100644 --- a/src/context.hpp +++ b/src/context.hpp @@ -65,6 +65,7 @@ namespace Sass { std::vector plugin_paths; // relative paths to load plugins std::vector include_paths; // lookup paths for includes + std::vector> lazy_imports;