Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC: Parallel parsing of imports [DNM/WIP] #2544

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <iomanip>
#include <sstream>
#include <iostream>
#include <thread>

#include "ast.hpp"
#include "util.hpp"
Expand Down Expand Up @@ -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
Expand All @@ -366,6 +372,7 @@ namespace Sass {
// return resolved entry
return resolved[0];
}

}

// nothing found
Expand Down Expand Up @@ -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()
{

Expand Down Expand Up @@ -584,6 +602,25 @@ namespace Sass {
// create the source entry for file entry
register_resource({{ input_path, "." }, abs_path }, { contents, 0 });

std::vector<std::thread> 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();

Expand Down
1 change: 1 addition & 0 deletions src/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ namespace Sass {

std::vector<std::string> plugin_paths; // relative paths to load plugins
std::vector<std::string> include_paths; // lookup paths for includes
std::vector<std::tuple<Include, ParserState, bool>> lazy_imports;



Expand Down