-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathdeferrable.dart
217 lines (195 loc) · 6.48 KB
/
deferrable.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:collection';
import 'package:compiler/src/serialization/serialization.dart';
/// Interface for data that may be deserialized lazily.
///
/// This interface should be used to wrap data objects that aren't needed in
/// later phases of the compiler. Usage of this class should follow a set
/// pattern. Given a class `C` with a field `m0` of type `E` that we wish to
/// make deferrable:
///
/// 1) `m0` should be replaced with an internal field, `_m1`, of type
/// `Deferrable<E>`.
/// 2) An internal constructor should be added to `C` that takes a `d` of type
/// `Deferrable<E>` to initialize `_m1`. This internal constructor should be
/// called from the readFromSource method/factory to create the instance
/// of `C`. `d` should be obtained using [DataSourceReader.readDeferrable].
/// Note: [DataSourceReader.readDeferrable] passes the correct
/// [DataSourceReader] to the reader function so that the [DataSourceReader]
/// does not have to be closed over. If necessary a static tear-off should be
/// used as the argument so a closure isn't created for every read.
/// 3) Any existing constructors of `C` should maintain the same signature
/// and initialize `_m1` passing `m` to [Deferrable.eager] where m is the value
/// previously used to initialize `m0`.
/// 4) If there are external references to `m0` then `C`s interface should be
/// maintained. A getter `m0` should be added: `E get m0 => _m1.loaded()`
/// 5) If all references to `m0` were internal, they can simply be replaced
/// with calls to `_m1.loaded()`.
///
/// Example class before:
///
/// ```
/// class Foo {
/// final Bar bar;
/// final String name;
///
/// Foo(this.bar, this.name);
///
/// factory Foo.readFromSource(DataSourceReader reader) {
/// final bar = Bar.readFromSource(reader);
/// final name = reader.readString();
/// return Foo(bar, name);
/// }
/// }
/// ```
///
/// After:
///
/// ```
/// class Foo {
/// Bar get bar => _bar.loaded();
/// final Deferrable<Bar> _bar;
///
/// String get name => _name.loaded();
/// final Deferrable<String> _name;
///
/// Foo(Bar bar, String name) :
/// _bar = Deferrable.eager(bar), _name = Deferrable.eager(name);
/// Foo._deserialized(this._bar, this._name);
///
/// static String readName(DataSourceReader reader) => reader.readString();
///
/// factory Foo.readFromSource(DataSourceReader reader) {
/// final bar = reader.readDeferrable(Bar.readFromSource);
/// final name = reader.readDeferrable(readName);
/// return Foo._deserialized(bar, name);
/// }
/// }
/// ```
abstract class Deferrable<E> {
E loaded();
factory Deferrable.deferred(
DataSourceReader reader,
E Function(DataSourceReader source) f,
int offset, {
bool cacheData = true,
}) =>
cacheData
? _DeferredCache(reader, f, offset)
: _Deferred(reader, f, offset);
static Deferrable<E> deferredWithArg<E, A>(
DataSourceReader reader,
E Function(DataSourceReader source, A arg) f,
A arg,
int offset, {
bool cacheData = true,
}) =>
cacheData
? _DeferredCacheWithArg(reader, f, arg, offset)
: _DeferredWithArg(reader, f, arg, offset);
const factory Deferrable.eager(E data) = _Eager;
const Deferrable();
}
class _Eager<E> extends Deferrable<E> {
final E _data;
@override
E loaded() => _data;
const _Eager(this._data);
}
class _DeferredWithArg<E, A> extends Deferrable<E> {
final DataSourceReader _reader;
final E Function(DataSourceReader source, A arg) _dataLoader;
final A _arg;
final int _dataOffset;
@override
E loaded() =>
_reader.readWithOffset(_dataOffset, () => _dataLoader(_reader, _arg));
_DeferredWithArg(this._reader, this._dataLoader, this._arg, this._dataOffset);
}
class _DeferredCacheWithArg<E, A> extends Deferrable<E> {
final int _dataOffset;
// Below fields are nullable so they can be cleared after loading.
DataSourceReader? _reader;
E Function(DataSourceReader source, A arg)? _dataLoader;
A? _arg;
late final E _data = _loadData();
@override
E loaded() => _data;
E _loadData() {
final reader = _reader!;
final dataLoader = _dataLoader!;
final arg = _arg as A;
_reader = null;
_dataLoader = null;
_arg = null;
return reader.readWithOffset(_dataOffset, () => dataLoader(reader, arg));
}
_DeferredCacheWithArg(
this._reader,
this._dataLoader,
this._arg,
this._dataOffset,
);
}
class _Deferred<E> extends Deferrable<E> {
final DataSourceReader _reader;
final E Function(DataSourceReader source) _dataLoader;
final int _dataOffset;
@override
E loaded() => _reader.readWithOffset(_dataOffset, () => _dataLoader(_reader));
_Deferred(this._reader, this._dataLoader, this._dataOffset);
}
class _DeferredCache<E> extends Deferrable<E> {
final int _dataOffset;
// Below fields are nullable so they can be cleared after loading.
DataSourceReader? _reader;
E Function(DataSourceReader source)? _dataLoader;
late final E _data = _loadData();
@override
E loaded() => _data;
E _loadData() {
final reader = _reader!;
final dataLoader = _dataLoader!;
_reader = null;
_dataLoader = null;
return reader.readWithOffset(_dataOffset, () => dataLoader(reader));
}
_DeferredCache(this._reader, this._dataLoader, this._dataOffset);
}
/// Implementation of [Map] in which each value of type [V] is internally
/// [Deferrable].
///
/// This map should be used when values of type [V] are expensive to
/// deserialize. This abstracts away the laziness allowing the deferred map to
/// be used as if the values were not deferred.
///
/// The provided map can have a mix of eager and lazy [Deferrable]s if
/// there are a mix of expensive and cheap values.
class DeferrableValueMap<K, V> with MapMixin<K, V> {
DeferrableValueMap(this._map);
final Map<K, Deferrable<V>> _map;
@override
V? operator [](Object? key) {
return _map[key]?.loaded();
}
@override
void operator []=(K key, V value) {
_map[key] = Deferrable.eager(value);
}
@override
void clear() {
_map.clear();
}
@override
Iterable<K> get keys => _map.keys;
@override
V? remove(Object? key) {
return _map.remove(key)?.loaded();
}
@override
V putIfAbsent(K key, V Function() ifAbsent) {
return _map.putIfAbsent(key, () => Deferrable.eager(ifAbsent())).loaded();
}
}