Skip to content

Commit 8abae62

Browse files
Generator: improve Dart formatter performance #74
1 parent 8b729ab commit 8abae62

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

generator/lib/src/code_chunks.dart

+12-4
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ class CodeChunks {
510510
paramValueCode = fieldReaders[index];
511511
if (entity.properties[index].isRelation) {
512512
if (paramDartType.startsWith('ToOne<')) {
513-
paramValueCode = 'ToOne(targetId: $paramValueCode)';
513+
paramValueCode = '$paramDartType(targetId: $paramValueCode)';
514514
} else if (paramType == 'optional-named') {
515515
log.info('Skipping constructor parameter $paramName on '
516516
"'${entity.name}': the matching field is a relation but the type "
@@ -519,7 +519,7 @@ class CodeChunks {
519519
}
520520
}
521521
} else if (paramDartType.startsWith('ToMany<')) {
522-
paramValueCode = 'ToMany()';
522+
paramValueCode = '$paramDartType()';
523523
} else {
524524
// If we can't find a positional param, we can't use the constructor at all.
525525
if (paramType == 'positional' || paramType == 'required-named') {
@@ -533,14 +533,22 @@ class CodeChunks {
533533
return true; // continue to the next param
534534
}
535535

536+
// The Dart Formatter consumes a large amount of time if constructor
537+
// parameters are complex expressions, so add a variable for each
538+
// parameter instead and pass that to the constructor.
539+
// As the parameter name is user supplied add a suffix to avoid collision
540+
// with other variables of the generated method.
541+
final paramVar = "${paramName}Param";
542+
preLines.add("final $paramVar = $paramValueCode;");
543+
536544
switch (paramType) {
537545
case 'positional':
538546
case 'optional':
539-
constructorLines.add(paramValueCode);
547+
constructorLines.add(paramVar);
540548
break;
541549
case 'required-named':
542550
case 'optional-named':
543-
constructorLines.add('$paramName: $paramValueCode');
551+
constructorLines.add('$paramName: $paramVar');
544552
break;
545553
default:
546554
throw InvalidGenerationSourceError(

objectbox/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## latest
22

3+
* Improve code generator performance if there are many entities with many constructor parameters.
4+
35
## 2.1.0 (2023-06-13)
46

57
* **Support for integer and floating point lists**: store 8-bit, 16-bit, 32-bit and 64-bit integer

0 commit comments

Comments
 (0)