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

[DRAFT] - [perf] - wasm optimisations #6

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open

Conversation

pivanov
Copy link

@pivanov pivanov commented Sep 3, 2024

Pre-flight checklist

  • I have read the Contributing Guidelines on pull requests.
  • Commit messages follow the conventional commits spec
  • If this is a code change: I have written unit tests.
  • If this is a new API or substantial change: the PR has an accompanying issue (closes #0000) and the maintainers have approved on my working plan.

Motivation

Test Plan

Related issues/PRs

@pivanov pivanov changed the title [perf] - wasm optimisations [DRAFT] - [perf] - wasm optimisations Sep 3, 2024
Copy link
Collaborator

@valeriivanchev valeriivanchev left a 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.

  1. Check the difference between || and ?? and see if we have used it appropriately.
  2. Check how pnpm --filter works.( e.g. sub-packages installation, context in which the scripts are running).
  3. Check the formatting in the code.

const tempResults = {};
tempResults[testName] = result;

saveResults(tempResults);
Copy link
Collaborator

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?

Copy link
Collaborator

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

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
Copy link
Collaborator

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.

details.gasUsedToRefundUnusedGas,
jsEntry ? jsEntry[1].gasUsedToRefundUnusedGas : "",
]);
rows.push([
Copy link
Collaborator

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
Copy link
Collaborator

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

Copy link
Collaborator

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.


const sortedData = {};

order.forEach((key) => {
Copy link
Collaborator

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) => {
Copy link
Collaborator

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.

@dim-daskalov
Copy link
Collaborator

Great work. Just a couple of comments and also two points to research.

  1. Check the difference between || and ?? and see if we have used it appropriately.
  2. Check how pnpm --filter works.( e.g. sub-packages installation, context in which the scripts are running).
  3. Check the formatting in the code.

Thanks for the review. I will check this out.

…, format the code and implement the comment suggestions
Copy link
Author

@pivanov pivanov left a 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!!!

return {};
}

/** Function to add test results to the report if the GENERATE_REPORT environment variable is set to "true" */
Copy link
Author

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) {
...

Copy link
Author

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.

Copy link
Collaborator

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.

function readExistingResults() {
if (fs.existsSync(resultsFilePath)) {
const fileContent = fs.readFileSync(resultsFilePath, "utf-8");
return JSON.parse(fileContent);
Copy link
Author

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.

};

// Write the combined results to the file
fs.writeFileSync(resultsFilePath, JSON.stringify(combinedResults, null, 2));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try/catch?

build
file-sizes.json
Copy link
Author

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.

Copy link
Collaborator

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);
Copy link
Author

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) => {
Copy link
Author

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) + "%"
  );
};

Copy link
Author

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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new line

"near-sdk-js": "workspace:*"
}
}
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aaaand new line :)

@dim-daskalov
Copy link
Collaborator

dim-daskalov commented Sep 17, 2024

Sorry for all of the comments :) hope they will help!

🙇 Good Job!!!

Hey @pivanov, thanks for the suggestions!

I learned a few new things.

All should be addressed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants