Skip to content

Commit

Permalink
perf: use collections which are more efficient for the task
Browse files Browse the repository at this point in the history
  • Loading branch information
blaugold committed Apr 20, 2022
1 parent 9d1f86a commit 0c33c13
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 22 deletions.
11 changes: 6 additions & 5 deletions packages/cbl/lib/src/service/channel.dart
@@ -1,6 +1,7 @@
// ignore_for_file: prefer_constructors_over_static_methods, avoid_print

import 'dart:async';
import 'dart:collection';

import 'package:stream_channel/stream_channel.dart';

Expand Down Expand Up @@ -127,12 +128,12 @@ class Channel {
var _status = ChannelStatus.initial;
ChannelStatus get status => _status;

final _callHandlers = <Type, _UntypedCallHandler>{};
final _callCompleter = <int, Completer<_Message>>{};
final _callHandlers = HashMap<Type, _UntypedCallHandler>.identity();
final _callCompleter = HashMap<int, Completer<_Message>>();

final _streamHandlers = <Type, _UntypedStreamHandler>{};
final _streamControllers = <int, StreamController<_Message>>{};
final _streamSubscriptions = <int, StreamSubscription>{};
final _streamHandlers = HashMap<Type, _UntypedStreamHandler>.identity();
final _streamControllers = HashMap<int, StreamController<_Message>>();
final _streamSubscriptions = HashMap<int, StreamSubscription>();

/// Makes a call to an endpoint at other side of the channel.
Future<R> call<R>(Request<R> request) async => asyncOperationTracePoint(
Expand Down
6 changes: 4 additions & 2 deletions packages/cbl/lib/src/service/object_registry.dart
@@ -1,11 +1,13 @@
import 'dart:collection';

class ObjectRegistry {
int _nextId = 0;

// `Object` is typed as nullable because of a bug in Dart, which has been
// fixed, but is not yet in the stable branch.
// https://github.com/dart-lang/sdk/issues/46165
final _idToObject = <int, Object?>{};
final _objectToId = Map<Object?, int>.identity();
final _idToObject = HashMap<int, Object?>();
final _objectToId = HashMap<Object?, int>.identity();

T? getObject<T>(int id) {
final object = _idToObject[id];
Expand Down
30 changes: 17 additions & 13 deletions packages/cbl/lib/src/service/serialization/serialization.dart
@@ -1,3 +1,5 @@
import 'dart:collection';

import 'package:cbl_ffi/cbl_ffi.dart';
import 'package:collection/collection.dart';

Expand Down Expand Up @@ -185,23 +187,25 @@ class SerializationRegistry {
factory SerializationRegistry() => _basicSerializationRegistry();

SerializationRegistry.empty()
: _nameToType = {},
_typeToName = {},
_typeToInstanceOf = {},
_serializers = {},
_deserializers = {};
: _nameToType = HashMap(),
_typeToName = HashMap.identity(),
_typeToInstanceOf = HashMap.identity(),
_serializers = HashMap.identity(),
_deserializers = HashMap.identity();

SerializationRegistry.merge(
SerializationRegistry registry, {
required SerializationRegistry into,
}) : _nameToType = {...into._nameToType, ...registry._nameToType},
_typeToName = {...into._typeToName, ...registry._typeToName},
_typeToInstanceOf = {
...into._typeToInstanceOf,
...registry._typeToInstanceOf
},
_serializers = {...into._serializers, ...registry._serializers},
_deserializers = {...into._deserializers, ...registry._deserializers};
}) : _nameToType =
HashMap.of({...into._nameToType, ...registry._nameToType}),
_typeToName = HashMap.identity()
..addAll({...into._typeToName, ...registry._typeToName}),
_typeToInstanceOf = HashMap.identity()
..addAll({...into._typeToInstanceOf, ...registry._typeToInstanceOf}),
_serializers = HashMap.identity()
..addAll({...into._serializers, ...registry._serializers}),
_deserializers = HashMap.identity()
..addAll({...into._deserializers, ...registry._deserializers});

final Map<String, Type> _nameToType;
final Map<Type, String> _typeToName;
Expand Down
3 changes: 2 additions & 1 deletion packages/cbl/lib/src/support/dart_finalizer.dart
@@ -1,3 +1,4 @@
import 'dart:collection';
import 'dart:ffi';
import 'dart:isolate';

Expand All @@ -11,7 +12,7 @@ class DartFinalizerRegistry {
DartFinalizerRegistry._();

int _nextToken = 0;
final _finalizers = <int, DartFinalizer>{};
final _finalizers = HashMap<int, DartFinalizer>();
ReceivePort? _receivePort;
int? _sendPort;

Expand Down
3 changes: 2 additions & 1 deletion packages/cbl/lib/src/support/resource.dart
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:collection';
import 'dart:ffi';

import 'package:meta/meta.dart';
Expand Down Expand Up @@ -37,7 +38,7 @@ abstract class NativeResource<T extends NativeType> {
mixin ClosableResourceMixin implements ClosableResource {
ClosableResourceMixin? _parent;
late final Set<ClosableResourceMixin> _childrenToClose = {};
late final _pendingRequests = <Future<void>>[];
late final _pendingRequests = DoubleLinkedQueue<Future<void>>();
Future<void>? _pendingClose;

@override
Expand Down

0 comments on commit 0c33c13

Please sign in to comment.