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

Make minifyFiles faster #20

Merged
merged 3 commits into from
Jul 12, 2021
Merged
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
4 changes: 2 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ minifyFiles(["file1.json", "file2.json"], true);
On AMD Ryzen 7 4800H:

- minifyString: minijson is 4178 times faster than jsonMinify
- minifyFiles: minijson is 1198 times faster than jsonMinify.
- minifyFiles: minijson is 1894 times faster than jsonMinify.

```
❯ .\dist\minijson-benchmark.exe --benchmark-minifyString --benchmark-minifyFiles
Benchmark minifyString
14 ms
Benchmark minifyFiles
49 ms
31 ms

❯ node .\benchmark\js-benchmark.mjs
Benchmark minifyString
Expand Down
18 changes: 7 additions & 11 deletions src/native/lib.d
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,13 @@ string minifyString(in string jsonString, in bool hasComment = false) @trusted
{
auto leftContextSubstr = match.pre()[prevFrom .. $];
const noLeftContext = leftContextSubstr.length == 0;
if (!in_string && !noLeftContext)
{
leftContextSubstr = remove_spaces(leftContextSubstr);
}
if (!noLeftContext)
{
if (!noLeftContext) {
if (!in_string)
{
leftContextSubstr = remove_spaces(leftContextSubstr);
}
result ~= leftContextSubstr;
}

if (matchFrontHit == "\"")
{
if (!in_string || noLeftContext || hasNoSlashOrEvenNumberOfSlashes(leftContextSubstr))
Expand Down Expand Up @@ -175,10 +173,8 @@ void minifyFiles(in string[] files, in bool hasComment = false)
import std.parallelism : parallel;
import std.file : readText, write;

foreach (ref file; files.parallel())
foreach (file; files.parallel())
{
const string jsonString = readText(file);
const minifiedJsonString = minifyString(jsonString, hasComment);
write(file, minifiedJsonString);
write(file, minifyString(readText(file), hasComment));
}
}