Skip to content

Commit

Permalink
Avoid repeated allocations.
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatz authored and simolus3 committed Jan 23, 2024
1 parent 059108f commit 1a1f53f
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions extras/drift_postgres/lib/src/pg_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,31 +157,29 @@ class _BoundArguments {
final List<Type> types;
final List<TypedValue> parameters;

_BoundArguments(this.types, this.parameters);
_BoundArguments(this.parameters)
: types = parameters.map((p) => p.type).toList(growable: false);

factory _BoundArguments.ofDartArgs(List<Object?> args) {
final types = <Type>[];
final parameters = <TypedValue>[];

void add(TypedValue param) {
types.add(param.type);
parameters.add(param);
}

for (final value in args) {
add(switch (value) {
TypedValue() => value,
null => TypedValue(Type.text, null),
int() || BigInt() => TypedValue(Type.bigInteger, value),
String() => TypedValue(Type.text, value),
bool() => TypedValue(Type.boolean, value),
double() => TypedValue(Type.double, value),
List<int>() => TypedValue(Type.byteArray, value),
_ => throw ArgumentError.value(value, 'value', 'Unsupported type'),
});
}
final parameters = List<TypedValue>.generate(
args.length,
(i) {
final value = args[i];
return switch (value) {
TypedValue() => value,
null => TypedValue(Type.text, null),
int() || BigInt() => TypedValue(Type.bigInteger, value),
String() => TypedValue(Type.text, value),
bool() => TypedValue(Type.boolean, value),
double() => TypedValue(Type.double, value),
List<int>() => TypedValue(Type.byteArray, value),
_ => throw ArgumentError.value(value, 'value', 'Unsupported type'),
};
},
growable: false,
);

return _BoundArguments(types, parameters);
return _BoundArguments(parameters);
}
}

Expand Down

0 comments on commit 1a1f53f

Please sign in to comment.