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

feat(dcs): optimize decorate models #857

Conversation

sanketshevkar
Copy link
Member

@sanketshevkar sanketshevkar commented Jun 7, 2024

Closes #856

Proposal for optimizing decorate models. More details on #856

Changes

  • Index all the commands in dcs in their respective maps based on their primary target: namespace, declaration, property, mapElement, types all have their separate maps. Wrap the commands with their index in dcs before indexing them.
  • Traverse through the model ast and lookup into the maps for any command related to the targets. Collect them in a list and sort them based on index before applying on the model element.
  • Function signatures have more or less stayed the same, except for executeCommand but are not a breaking change in nature.
  • Logic for executeCommand has changed a bit and decorateModels has changed significantly. New private methods have been introduced to support these.
  • New test case has added that aims at covering all possible command target combinations in dcs. (though list is not exhanustive)

Performance and Memory Impact.

Performance

  • Auto-generated model of size 1.6MB took Execution time: 6696.545083999634 ms to apply autogenerated vocabulary to the model with the older implementation.
  • New implementation takes Execution time: 44.81274998188019 ms for the same operation.
const start = performance.now();
DecoratorManager.decorateModels(mm, vocDcs);
const end = performance.now();
console.log(`Execution time: ${end - start} ms`);
  • 152 times faster. <<hmm this makes me skeptical 💭 >>

Memory

total_heap_size

DecoratorManager.decorateModels(mm, vocDcs);
console.log(v8.getHeapStatistics());
  • total_heap_size recorded after an auto-generated model of size 1.6MB was applied autogenerated vocabulary to the model with the older implementation was 58261504
  • New implementation recorded total_heap_size as 61571072
  • 1.06 times more space
  • 5% increase in total_heap_size

used_heap_size

console.log(v8.getHeapStatistics());
DecoratorManager.decorateModels(mm, vocDcs);
console.log(v8.getHeapStatistics());
  • Increase in used_heap_size recorded after an auto-generated model of size 1.6MB was applied autogenerated vocabulary to the model with the older implementation was 6541360
  • New implementation recorded increase used_heap_size as 13041876
  • 2 times more increase in the increase of used_heap_space
  • 99.3% increase in increase of used_heap_size

Author Checklist

  • Ensure you provide a DCO sign-off for your commits using the --signoff option of git commit.
  • Vital features and changes captured in unit and/or integration tests
  • Commits messages follow AP format
  • Extend the documentation, if necessary
  • Merging to main from fork:branchname

Signed-off-by: Sanket Shevkar <[email protected]>
Copy link
Member

@mttrbrts mttrbrts left a comment

Choose a reason for hiding this comment

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

It looks like you're on the right track, @sanketshevkar. I look forward to seeing the perf numbers!

packages/concerto-core/lib/decoratormanager.js Outdated Show resolved Hide resolved
packages/concerto-core/lib/decoratormanager.js Outdated Show resolved Hide resolved
packages/concerto-core/lib/decoratormanager.js Outdated Show resolved Hide resolved
@sanketshevkar sanketshevkar changed the title feat(dcs): optimize decorate models feat(dcs): optimize decorate models - not ready for review Jul 15, 2024
@sanketshevkar sanketshevkar changed the title feat(dcs): optimize decorate models - not ready for review feat(dcs): optimize decorate models Jul 17, 2024
@sanketshevkar sanketshevkar marked this pull request as ready for review July 17, 2024 12:08
@sanketshevkar sanketshevkar requested a review from mttrbrts July 17, 2024 12:09
packages/concerto-core/api.txt Outdated Show resolved Hide resolved
packages/concerto-core/lib/decoratormanager.js Outdated Show resolved Hide resolved
packages/concerto-core/lib/decoratormanager.js Outdated Show resolved Hide resolved
packages/concerto-core/lib/decoratormanager.js Outdated Show resolved Hide resolved
packages/concerto-core/lib/decoratormanager.js Outdated Show resolved Hide resolved
packages/concerto-core/lib/decoratormanager.js Outdated Show resolved Hide resolved
packages/concerto-core/lib/decoratormanager.js Outdated Show resolved Hide resolved
packages/concerto-core/test/dcsIndexWrapper.js Outdated Show resolved Hide resolved
@sanketshevkar sanketshevkar requested a review from mttrbrts July 18, 2024 19:41
Copy link
Contributor

@dselman dselman left a comment

Choose a reason for hiding this comment

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

Couple of minor changes. Looks good.

/**
* Creates five different maps to index decorator command sets by target type and returns them
* @param {*} decoratorCommandSet the DecoratorCommandSet object
* @returns {Object} a new model manager with the decorations applied
Copy link
Contributor

Choose a reason for hiding this comment

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

Incorrect JSDoc.

Copy link
Member Author

Choose a reason for hiding this comment

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

Corrected


decoratorCommandSet.commands.map((decoratorCommand, index) => {
const dcsWithIndex = new DcsIndexWrapper(decoratorCommand, index);
switch (true) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this switch supposed to be exhaustive? If yes, add a default case and throw an exception.

Copy link
Member Author

@sanketshevkar sanketshevkar Jul 22, 2024

Choose a reason for hiding this comment

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

I was under the assumption that if we pass a invalid dcs, it will only get caught if validation is enabled. In the case of a malformed decorator, if we pass (in this case target is undefined, falsy, null or does not match with any of the switch statements) that decorator won't get applied.

This is the older behavior: https://replit.com/@sanketshevkar/Decorator-malformed-dcs#index.js
This differs a bit, I think is was a bug as well. shouldn't target: false be invalid dcs, but its passing the syntax validation. In either case, if we validate or not, it just applies the decorator on top of all the declarations, which should be a bug?

* @param {string} namespace the namespace for the declaration
* @param {*} declaration the class declaration
* @param {*} command the Command object from the dcs
* @param {boolean} [enableDcsNamespaceTarget] - flag to control applying namespace targeted decorators on top of the namespace instead of all declarations in that namespace
* @param {*} property the property
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like property is now an optional argument? Would be good to document that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes corrected

* [DecoratorCommandSet](https://models.accordproject.org/concerto/decorators.cto)
* @memberof module:concerto-core
*/
class DcsIndexWrapper {
Copy link
Contributor

Choose a reason for hiding this comment

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

Private?

Copy link
Contributor

Choose a reason for hiding this comment

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

Should not be exported? Private?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes made the method private

*/
private static getDecoratorMaps;
/**
* Migrate or validate the DecoratorCommandSet object if the options are set as true
Copy link
Contributor

Choose a reason for hiding this comment

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

Migrate AND validate?

Copy link
Member Author

Choose a reason for hiding this comment

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

It can also do either of the operation if parameters for the arguments are passed so. It can only validate or it might only migrate and can do both if the parameters are true for both.

The only reason I moved this out of decorateModels is to reduce the code in the decorateModels method, so is a private method just used in decorateModels method.

@sanketshevkar sanketshevkar requested a review from dselman July 22, 2024 11:59
Copy link
Contributor

@dselman dselman left a comment

Choose a reason for hiding this comment

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

Bravo!

@sanketshevkar sanketshevkar merged commit bf3385c into accordproject:main Jul 24, 2024
11 checks passed
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.

Optimizations for applying decorators on a model
3 participants