Skip to content

Commit b3c8ac2

Browse files
refactor(concerto-core): improve deep copy performance in decorator manager (#1014)
Signed-off-by: Yagnik Patel <[email protected]> Co-authored-by: Yagnik Patel <[email protected]>
1 parent 5e7e8b9 commit b3c8ac2

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

package-lock.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/concerto-core/lib/decoratormanager.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ const { MetaModelNamespace } = require('@accordproject/concerto-metamodel');
2222
const semver = require('semver');
2323
const DecoratorExtractor = require('./decoratorextractor');
2424
const { Warning, ErrorCodes } = require('@accordproject/concerto-util');
25+
const rfdc = require('rfdc')({
26+
circles: true,
27+
proto: false,
28+
});
2529

2630
// Types needed for TypeScript generation.
2731
/* eslint-disable no-unused-vars */
@@ -349,9 +353,8 @@ class DecoratorManager {
349353
* @private
350354
*/
351355
static pushMapValues(array, map, key) {
352-
const targetCommands = map.get(key);
353-
if (targetCommands) {
354-
array.push(...targetCommands);
356+
for (const value of map.get(key) || []) {
357+
array.push(value);
355358
}
356359
}
357360

@@ -375,7 +378,7 @@ class DecoratorManager {
375378

376379
// we create synthetic imports for all decorator declarations
377380
// along with any of their type reference arguments
378-
const decoratorImports = decoratorCommandSet.commands.map(command => {
381+
const decoratorImports = decoratorCommandSet.commands.flatMap(command => {
379382
return [{
380383
$class: `${MetaModelNamespace}.ImportType`,
381384
name: command.decorator.name,
@@ -389,21 +392,21 @@ class DecoratorManager {
389392
};
390393
})
391394
: []);
392-
}).flat().filter(i => i.namespace);
395+
}).filter(i => i.namespace);
393396
const { namespaceCommandsMap, declarationCommandsMap, propertyCommandsMap, mapElementCommandsMap, typeCommandsMap } = this.getDecoratorMaps(decoratorCommandSet);
394397
const ast = modelManager.getAst(true, true);
395-
const decoratedAst = JSON.parse(JSON.stringify(ast));
398+
const decoratedAst = rfdc(ast);
396399
decoratedAst.models.forEach((model) => {
397400
// remove the imports for types defined in this namespace
398401
const neededImports = decoratorImports.filter(i => i.namespace !== model.namespace);
399402
// add the imports for decorators, in case they get added below
400403
model.imports = model.imports ? model.imports.concat(neededImports) : neededImports;
404+
const namespaceName = ModelUtil.parseNamespace(model.namespace).name;
401405
model.declarations.forEach((decl) => {
402406
const declarationDecoratorCommandSets = [];
403407
const { name: declarationName, $class: $classForDeclaration } = decl;
404408
this.pushMapValues(declarationDecoratorCommandSets, declarationCommandsMap, declarationName);
405409
this.pushMapValues(declarationDecoratorCommandSets, namespaceCommandsMap, model.namespace);
406-
const namespaceName = ModelUtil.parseNamespace(model.namespace).name;
407410
this.pushMapValues(declarationDecoratorCommandSets, namespaceCommandsMap, namespaceName);
408411
this.pushMapValues(declarationDecoratorCommandSets, typeCommandsMap, $classForDeclaration);
409412
const sortedDeclarationDecoratorCommandSets = declarationDecoratorCommandSets.sort((declDcs1, declDcs2) => declDcs1.getIndex() - declDcs2.getIndex());

packages/concerto-core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"debug": "4.3.7",
7272
"lorem-ipsum": "2.0.8",
7373
"randexp": "0.5.3",
74+
"rfdc": "1.4.1",
7475
"semver": "7.6.3",
7576
"urijs": "1.19.11",
7677
"uuid": "11.0.3"

0 commit comments

Comments
 (0)