Skip to content

Commit

Permalink
Merge pull request #1946 from nextcloud/perf/dynamite/memoize_cpec_ge…
Browse files Browse the repository at this point in the history
…tters

perf(dynamite): use memoization for for better performance
  • Loading branch information
Leptopoda authored Apr 22, 2024
2 parents 0d8c5ee + b25c408 commit 7776b63
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 9 deletions.
7 changes: 2 additions & 5 deletions packages/dynamite/dynamite/lib/src/builder/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -634,11 +634,8 @@ final authentication = $client.authentications?.firstWhereOrNull(
..writeAll(
securityRequirements.map((requirement) {
final securityScheme = spec.components!.securitySchemes![requirement.keys.single]!;
final dynamiteAuth = toDartName(
'Dynamite-${securityScheme.fullName.join('-')}-Authentication',
className: true,
);
return refer(dynamiteAuth, 'package:dynamite_runtime/http_client.dart')

return refer(securityScheme.fullName, 'package:dynamite_runtime/http_client.dart')
.newInstance(const [])
.accept(state.emitter)
.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ abstract class OpenAPI implements Built<OpenAPI, OpenAPIBuilder> {

BuiltMap<String, PathItem>? get paths;

@memoized
bool get hasAnySecurity => components?.securitySchemes?.isNotEmpty ?? false;

@BuiltValueHook(finalizeBuilder: true)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ abstract class Parameter implements Built<Parameter, ParameterBuilder> {

ParameterStyle get style;

@memoized
Schema? get schema {
if ($schema != null) {
return $schema;
Expand Down Expand Up @@ -178,6 +179,7 @@ abstract class Parameter implements Built<Parameter, ParameterBuilder> {
}
}

@memoized
String get formattedDescription {
final name = toDartName(this.name);
final buffer = StringBuffer()..write(' * [$name]');
Expand Down
15 changes: 15 additions & 0 deletions packages/dynamite/dynamite/lib/src/models/openapi/parameter.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ abstract class PathItem implements Built<PathItem, PathItemBuilder> {

Operation? get trace;

@memoized
Map<PathItemOperation, Operation> get operations => <PathItemOperation, Operation>{
if (get != null) PathItemOperation.get: get!,
if (put != null) PathItemOperation.put: put!,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions packages/dynamite/dynamite/lib/src/models/openapi/schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ abstract class Schema implements Built<Schema, SchemaBuilder> {

bool get nullable;

bool get isContentString =>
type == SchemaType.string && (contentMediaType?.isNotEmpty ?? false) && contentSchema != null;
@memoized
bool get isContentString => type == SchemaType.string && contentMediaType != null && contentSchema != null;

@memoized
String? get formattedDescription => formatDescription(description);

@BuiltValueHook(finalizeBuilder: true)
Expand Down
15 changes: 15 additions & 0 deletions packages/dynamite/dynamite/lib/src/models/openapi/schema.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
import 'package:collection/collection.dart';
import 'package:dynamite/src/helpers/dart_helpers.dart';

part 'security_scheme.g.dart';

Expand All @@ -22,5 +22,32 @@ abstract class SecurityScheme implements Built<SecurityScheme, SecuritySchemeBui

String? get name;

Iterable<String> get fullName => [type, scheme, $in, name].whereNotNull();
@memoized
String get fullName {
final buffer = StringBuffer('Dynamite-')
..write(type)
..write('-');

if (scheme != null) {
buffer
..write(scheme)
..write('-');
}
if ($in != null) {
buffer
..write($in)
..write('-');
}
if (name != null) {
buffer
..write(name)
..write('-');
}
buffer.write('-Authentication');

return toDartName(
buffer.toString(),
className: true,
);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/dynamite/dynamite/lib/src/models/openapi/tag.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ abstract class Tag implements Built<Tag, TagBuilder> {
@BuiltValueField(compare: false)
String? get description;

@memoized
String? get formattedDescription => formatDescription(description);
}
11 changes: 11 additions & 0 deletions packages/dynamite/dynamite/lib/src/models/openapi/tag.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7776b63

Please sign in to comment.