-
Notifications
You must be signed in to change notification settings - Fork 0
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
[DRAFT] - [perf] - wasm optimisations #6
base: develop
Are you sure you want to change the base?
Conversation
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.
Great work. Just a couple of comments and also two points to research.
- Check the difference between || and ?? and see if we have used it appropriately.
- Check how
pnpm --filter
works.( e.g. sub-packages installation, context in which the scripts are running). - Check the formatting in the code.
benchmark/__tests__/results-store.js
Outdated
const tempResults = {}; | ||
tempResults[testName] = result; | ||
|
||
saveResults(tempResults); |
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.
Do we use this function anywhere else seems like it is only used in this one place. Maybe we don't need it?
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.
Yes, it's true, I will remove it and move the code to addTestResults
. Thanks
benchmark/__tests__/util.js
Outdated
export function logTestResults(testResults) { | ||
// Determine which function to use based on the structure of the results | ||
const isMinimal = testResults.result.receipts_outcome.length === 2; | ||
const generateGasObjectFn = isMinimal |
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.
Imho: We can make this as a one function.
benchmark/__tests__/util.js
Outdated
details.gasUsedToRefundUnusedGas, | ||
jsEntry ? jsEntry[1].gasUsedToRefundUnusedGas : "", | ||
]); | ||
rows.push([ |
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.
No need to make a push to the same array in two places you can do it in one go. You can take a look from here
|
||
// Check if the JSON file already exists and load data | ||
if ( | ||
await fs |
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.
Maybe we could refactor that part. Just to be more readable
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.
Yea, good point i will change it.
benchmark/__tests__/util.js
Outdated
|
||
const sortedData = {}; | ||
|
||
order.forEach((key) => { |
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.
Why not use array filtration?
// Filter .wasm files and calculate sizes | ||
const wasmFiles = files.filter((file) => path.extname(file) === ".wasm"); | ||
|
||
const fileSizePromises = wasmFiles.map(async (file) => { |
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.
Maybe we can refactor that part.
Thanks for the review. I will check this out. |
…, format the code and implement the comment suggestions
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.
Sorry for all of the comments :)
hope they will help!
🙇 Good Job!!!
benchmark/__tests__/results-store.js
Outdated
return {}; | ||
} | ||
|
||
/** Function to add test results to the report if the GENERATE_REPORT environment variable is set to "true" */ |
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.
Better Type Annotation (for TypeScript or JSDoc):
/**
* @param {string} testName - The name of the test.
* @param {Object} result - The test result object.
*/
export function addTestResults(testName, result) {
...
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.
Lack of error handling:
If there is an error while reading or writing the file, it can cause the program to crash without any clear indication. You should wrap file operations in try-catch blocks to gracefully handle potential file system errors.
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.
Yea, you're right. Thanks for pointing that out.
benchmark/__tests__/results-store.js
Outdated
function readExistingResults() { | ||
if (fs.existsSync(resultsFilePath)) { | ||
const fileContent = fs.readFileSync(resultsFilePath, "utf-8"); | ||
return JSON.parse(fileContent); |
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.
The JSON.parse() call could throw an error if the file contents are malformed or not valid JSON. It's good to wrap this in a try-catch block to handle such cases.
benchmark/__tests__/results-store.js
Outdated
}; | ||
|
||
// Write the combined results to the file | ||
fs.writeFileSync(resultsFilePath, JSON.stringify(combinedResults, null, 2)); |
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.
try/catch?
benchmark/.gitignore
Outdated
build | ||
file-sizes.json |
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.
add new line here ;)
POSIX ... If a file without a trailing newline is modified, Git's diff algorithm might show unexpected differences because the change will also affect the last line. Adding a newline at the end helps prevent diffs from being misleading.
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.
Nice, I did not know that. Thanks for mentioning it.
Object.keys(updatedFileSizes.afterOptimization).length | ||
) { | ||
// Generate Markdown file | ||
generateMarkdown(scriptDir, fileSizes); |
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.
While you do catch errors in calculateFileSizes
, some of the operations (e.g., generateMarkdown) don't have explicit error handling. It’s a good idea to wrap generateMarkdown in a try-catch block in case there are issues during markdown generation or file operations.
}; | ||
|
||
// Function to calculate percentage difference | ||
const calculatePercentageDifference = (beforeSize, afterSize) => { |
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.
The calculatePercentageDifference
function may throw an error if beforeSize or afterSize cannot be parsed properly (e.g., if there are unexpected non-numeric values). Adding error handling here will ensure the script doesn't crash unexpectedly.
const calculatePercentageDifference = (beforeSize, afterSize) => {
const beforeSizeNum = parseFloat(beforeSize);
const afterSizeNum = parseFloat(afterSize);
if (isNaN(beforeSizeNum) || isNaN(afterSizeNum)) {
return "N/A";
}
return (
(((beforeSizeNum - afterSizeNum) / beforeSizeNum) * 100).toFixed(2) + "%"
);
};
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.
Improving CLI UX:
You’re already checking for --before
and --after
, but adding a help flag (--help) or making the usage output more verbose would improve the user experience. You could also validate if both flags (--before and --after) are provided, and throw an error if both are used simultaneously, as it might be confusing.
if (isBefore && isAfter) {
console.log("Please specify either --before or --after, not both.");
process.exit(1);
}
if (args.includes("--help")) {
console.log("Usage: node script.js <relativeDir> [--before|--after]");
process.exit(0);
}
tests/.gitignore
Outdated
build | ||
file-sizes.json |
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.
new line
tests/package.json
Outdated
"near-sdk-js": "workspace:*" | ||
} | ||
} | ||
} |
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.
aaaand new line :)
Hey @pivanov, thanks for the suggestions! I learned a few new things. All should be addressed now. |
…tGas methods, fix the incorrect versioning
Pre-flight checklist
Motivation
Test Plan
Related issues/PRs