-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Fix Bug #1946 #1947
base: main
Are you sure you want to change the base?
Fix Bug #1946 #1947
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import fs from 'fs'; | ||
import os from 'os'; | ||
import path from 'path'; | ||
import colors from 'picocolors'; | ||
import { fileURLToPath } from 'url'; | ||
|
@@ -300,23 +301,32 @@ function optimizeFolder(config, dir, output) { | |
* @param {string} output output directory | ||
* @return {Promise} | ||
*/ | ||
function processDirectory(config, dir, files, output) { | ||
async function processDirectory(config, dir, files, output) { | ||
// take only *.svg files, recursively if necessary | ||
var svgFilesDescriptions = getFilesDescriptions(config, dir, files, output); | ||
|
||
return svgFilesDescriptions.length | ||
? Promise.all( | ||
svgFilesDescriptions.map((fileDescription) => | ||
optimizeFile( | ||
config, | ||
fileDescription.inputPath, | ||
fileDescription.outputPath, | ||
), | ||
), | ||
) | ||
: Promise.reject( | ||
new Error(`No SVG files have been found in '${dir}' directory.`), | ||
); | ||
|
||
if ( svgFilesDescriptions.length == 0 ) { | ||
throw 'No SVG files have been found in "' + dir + '" directory.'; | ||
} | ||
|
||
let results = [ ], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Run Prettier to fix some of the failing tests, it will fix the formatting on a couple lines including this one |
||
cpus = os.cpus().length; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's worth noting that since SVGO doesn't run in parallel it might not make sense to scale this by CPUs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eventloop run in single thread in fact. |
||
|
||
while ( svgFilesDescriptions.length > 0 ) { | ||
let _svgFilesDescriptions = svgFilesDescriptions.splice(0, cpus); | ||
|
||
_svgFilesDescriptions = _svgFilesDescriptions.map(function(fileDescription) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if this line errors or not, I would expect it to since with TS once a variable has a type you don't change it. Usually here you would create a new variable to store these results. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe you are right, i dont use TS, i do this changes over the npm downloaded code |
||
return optimizeFile( | ||
config, | ||
fileDescription.inputPath, | ||
fileDescription.outputPath, | ||
); | ||
}); | ||
|
||
results = results.concat(await Promise.all(_svgFilesDescriptions)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is slower than the existing version because each "chunk" of files takes as long as the longest file. I would suggest changing this to a worker-based method, let me know if you want me to explain There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is slower. This PR in fact run a throttle mechanism over chunks, this is the goal. Workers dont fix this, under linux the kernel has a limit over how many files can be open at same time, this limit is shared by all process of user, and another limit is shared for all process. The only way to prevent EMFILE error runing svgo over a huge number of svg is go slower and do the job in chunks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're misunderstanding what I said. Right now, it goes like "10 files open - > 6 files open - > 1 file open" if one takes longer than the rest, leading to it taking longer to start the next set of files. If you instead create 10 workers, each one will continously have 1 file open. |
||
} | ||
|
||
return results; | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should still be an
Error