Skip to content

Commit 5d163a4

Browse files
committed
Fully sync.
1 parent 6120277 commit 5d163a4

File tree

8 files changed

+162
-194
lines changed

8 files changed

+162
-194
lines changed

_benchmark/lib/config.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Config {
3939
rootDirectory: Directory(argResults['root-directory'] as String),
4040
sizes:
4141
argResults['size'] == null
42-
? [1, 100, 250, 500, 750, 1000]
42+
? [1, 500, 1000, 1500, 2000, 2500, 3000]
4343
: [int.parse(argResults['size'] as String)],
4444
shapes:
4545
argResults['shape'] == null

build/lib/src/state/filesystem_cache.dart

Lines changed: 96 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'dart:async';
65
import 'dart:convert';
76
import 'dart:typed_data';
87

9-
import 'package:pool/pool.dart';
10-
118
import '../../build.dart';
129

1310
/// Cache for file existence and contents.
@@ -17,101 +14,90 @@ abstract interface class FilesystemCache {
1714
/// Clears all [ids] from all caches.
1815
///
1916
/// Waits for any pending reads to complete first.
20-
Future<void> invalidate(Iterable<AssetId> ids);
17+
void invalidate(Iterable<AssetId> ids);
2118

22-
Future<void> flush();
19+
void flush();
2320

2421
/// Whether [id] exists.
2522
///
2623
/// Returns a cached result if available, or caches and returns `ifAbsent()`.
27-
Future<bool> exists(AssetId id, {required Future<bool> Function() ifAbsent});
24+
bool exists(AssetId id, {required bool Function() ifAbsent});
2825

2926
/// Reads [id] as bytes.
3027
///
3128
/// Returns a cached result if available, or caches and returns `ifAbsent()`.
32-
Future<Uint8List> readAsBytes(
33-
AssetId id, {
34-
required Future<Uint8List> Function() ifAbsent,
35-
});
29+
Uint8List readAsBytes(AssetId id, {required Uint8List Function() ifAbsent});
3630

37-
Future<void> writeAsBytes(
31+
void writeAsBytes(
3832
AssetId id,
3933
List<int> contents, {
40-
required Future<void> Function() writer,
34+
required void Function() writer,
4135
});
4236

4337
/// Reads [id] as a `String`.
4438
///
4539
/// Returns a cached result if available, or caches and returns `ifAbsent()`.
46-
Future<String> readAsString(
40+
String readAsString(
4741
AssetId id, {
4842
Encoding encoding = utf8,
49-
required Future<Uint8List> Function() ifAbsent,
43+
required Uint8List Function() ifAbsent,
5044
});
5145

52-
Future<void> writeAsString(
46+
void writeAsString(
5347
AssetId id,
5448
String contents, {
5549
Encoding encoding = utf8,
56-
required Future<void> Function() writer,
50+
required void Function() writer,
5751
});
5852

59-
Future<void> delete(AssetId id, {required Future<void> Function() deleter});
53+
void delete(AssetId id, {required void Function() deleter});
6054
}
6155

6256
/// [FilesystemCache] that always reads from the underlying source.
6357
class PassthroughFilesystemCache implements FilesystemCache {
6458
const PassthroughFilesystemCache();
6559

6660
@override
67-
Future<void> invalidate(Iterable<AssetId> ids) async {}
61+
void invalidate(Iterable<AssetId> ids) {}
6862

6963
@override
70-
Future<void> flush() => Future<void>.value();
64+
void flush() {}
7165

7266
@override
73-
Future<bool> exists(
74-
AssetId id, {
75-
required Future<bool> Function() ifAbsent,
76-
}) => ifAbsent();
67+
bool exists(AssetId id, {required bool Function() ifAbsent}) => ifAbsent();
7768

7869
@override
79-
Future<Uint8List> readAsBytes(
80-
AssetId id, {
81-
required Future<Uint8List> Function() ifAbsent,
82-
}) => ifAbsent();
70+
Uint8List readAsBytes(AssetId id, {required Uint8List Function() ifAbsent}) =>
71+
ifAbsent();
8372

8473
@override
85-
Future<String> readAsString(
74+
String readAsString(
8675
AssetId id, {
8776
Encoding encoding = utf8,
88-
required Future<Uint8List> Function() ifAbsent,
89-
}) async => encoding.decode(await ifAbsent());
77+
required Uint8List Function() ifAbsent,
78+
}) => encoding.decode(ifAbsent());
9079

9180
@override
92-
Future<void> writeAsBytes(
81+
void writeAsBytes(
9382
AssetId id,
9483
List<int> contents, {
95-
required Future<void> Function() writer,
84+
required void Function() writer,
9685
}) => writer();
9786

9887
@override
99-
Future<void> writeAsString(
88+
void writeAsString(
10089
AssetId id,
10190
String contents, {
10291
Encoding encoding = utf8,
103-
required Future<void> Function() writer,
92+
required void Function() writer,
10493
}) => writer();
10594

10695
@override
107-
Future<void> delete(AssetId id, {required Future<void> Function() deleter}) =>
108-
deleter();
96+
void delete(AssetId id, {required void Function() deleter}) => deleter();
10997
}
11098

11199
/// [FilesystemCache] that stores data in memory.
112100
class InMemoryFilesystemCache implements FilesystemCache {
113-
final _pool = Pool(1);
114-
115101
/// Cached results of [readAsBytes].
116102
final _bytesContentCache = <AssetId, Uint8List>{};
117103

@@ -127,138 +113,116 @@ class InMemoryFilesystemCache implements FilesystemCache {
127113
/// Only files read with [utf8] encoding (the default) will ever be cached.
128114
final _stringContentCache = <AssetId, String>{};
129115

130-
final _pendingWrites = <AssetId, Future<void> Function()>{};
131-
final _pendingDeletes = <AssetId, Future<void> Function()>{};
116+
final _pendingWrites = <AssetId, void Function()>{};
117+
final _pendingDeletes = <AssetId, void Function()>{};
132118

133119
@override
134-
Future<void> invalidate(Iterable<AssetId> ids) async {
135-
await _pool.withResource(() {
136-
for (var id in ids) {
137-
_canReadCache.remove(id);
138-
_bytesContentCache.remove(id);
139-
_stringContentCache.remove(id);
140-
}
141-
});
120+
void invalidate(Iterable<AssetId> ids) {
121+
for (var id in ids) {
122+
_canReadCache.remove(id);
123+
_bytesContentCache.remove(id);
124+
_stringContentCache.remove(id);
125+
}
142126
}
143127

144128
@override
145-
Future<void> flush() async {
146-
await _pool.withResource(() async {
147-
for (final write in _pendingWrites.values) {
148-
await write();
149-
}
150-
_pendingWrites.clear();
151-
});
129+
void flush() {
130+
for (final write in _pendingWrites.values) {
131+
write();
132+
}
133+
for (final delete in _pendingDeletes.values) {
134+
delete();
135+
}
136+
_pendingWrites.clear();
152137
}
153138

154139
@override
155-
Future<bool> exists(
156-
AssetId id, {
157-
required Future<bool> Function() ifAbsent,
158-
}) async {
159-
return await _pool.withResource(() async {
160-
final maybeResult = _canReadCache[id];
161-
if (maybeResult != null) return maybeResult;
162-
return _canReadCache[id] = await ifAbsent();
163-
});
140+
bool exists(AssetId id, {required bool Function() ifAbsent}) {
141+
final maybeResult = _canReadCache[id];
142+
if (maybeResult != null) return maybeResult;
143+
return _canReadCache[id] = ifAbsent();
164144
}
165145

166146
@override
167-
Future<Uint8List> readAsBytes(
168-
AssetId id, {
169-
required Future<Uint8List> Function() ifAbsent,
170-
}) async {
171-
return await _pool.withResource(() async {
172-
final maybeResult = _bytesContentCache[id];
173-
if (maybeResult != null) return maybeResult;
174-
175-
// check canRead first?
176-
if (_pendingDeletes.containsKey(id)) throw AssetNotFoundException(id);
177-
if (_pendingWrites.containsKey(id)) {
178-
throw StateError('Whoops, should have been cached: $id');
179-
}
147+
Uint8List readAsBytes(AssetId id, {required Uint8List Function() ifAbsent}) {
148+
final maybeResult = _bytesContentCache[id];
149+
if (maybeResult != null) return maybeResult;
180150

181-
return _bytesContentCache[id] = await ifAbsent();
182-
});
151+
// check canRead first?
152+
if (_pendingDeletes.containsKey(id)) throw AssetNotFoundException(id);
153+
if (_pendingWrites.containsKey(id)) {
154+
throw StateError('Whoops, should have been cached: $id');
155+
}
156+
157+
return _bytesContentCache[id] = ifAbsent();
183158
}
184159

185160
@override
186-
Future<String> readAsString(
161+
String readAsString(
187162
AssetId id, {
188163
Encoding encoding = utf8,
189-
required Future<Uint8List> Function() ifAbsent,
190-
}) async {
164+
required Uint8List Function() ifAbsent,
165+
}) {
191166
if (encoding != utf8) {
192-
final bytes = await readAsBytes(id, ifAbsent: ifAbsent);
167+
final bytes = readAsBytes(id, ifAbsent: ifAbsent);
193168
return encoding.decode(bytes);
194169
}
195170

196-
return await _pool.withResource(() async {
197-
final maybeResult = _stringContentCache[id];
198-
if (maybeResult != null) return maybeResult;
199-
200-
// check canRead first?
201-
if (_pendingDeletes.containsKey(id)) throw AssetNotFoundException(id);
202-
if (_pendingWrites.containsKey(id)) {
203-
final bytes = _bytesContentCache[id];
204-
if (bytes == null) {
205-
throw StateError('Whoops, should have been cached: $id');
206-
} else {
207-
return _stringContentCache[id] = utf8.decode(_bytesContentCache[id]!);
208-
}
171+
final maybeResult = _stringContentCache[id];
172+
if (maybeResult != null) return maybeResult;
173+
174+
// check canRead first?
175+
if (_pendingDeletes.containsKey(id)) throw AssetNotFoundException(id);
176+
if (_pendingWrites.containsKey(id)) {
177+
final bytes = _bytesContentCache[id];
178+
if (bytes == null) {
179+
throw StateError('Whoops, should have been cached: $id');
180+
} else {
181+
return _stringContentCache[id] = utf8.decode(_bytesContentCache[id]!);
209182
}
183+
}
210184

211-
final bytes = await ifAbsent();
212-
return _stringContentCache[id] = utf8.decode(bytes);
213-
});
185+
final bytes = ifAbsent();
186+
return _stringContentCache[id] = utf8.decode(bytes);
214187
}
215188

216189
@override
217-
Future<void> writeAsBytes(
190+
void writeAsBytes(
218191
AssetId id,
219192
List<int> contents, {
220-
required Future<void> Function() writer,
193+
required void Function() writer,
221194
}) {
222-
return _pool.withResource(() {
223-
_bytesContentCache[id] =
224-
(contents is Uint8List ? contents : Uint8List.fromList(contents));
225-
// [contents] might not be a valid string so defer trying to
226-
// decode it until if/when it's read as a string.
227-
_stringContentCache.remove(id);
228-
_canReadCache[id] = true;
229-
230-
_pendingDeletes.remove(id);
231-
_pendingWrites[id] = writer;
232-
return Future.value(null);
233-
});
195+
_bytesContentCache[id] =
196+
(contents is Uint8List ? contents : Uint8List.fromList(contents));
197+
// [contents] might not be a valid string so defer trying to
198+
// decode it until if/when it's read as a string.
199+
_stringContentCache.remove(id);
200+
_canReadCache[id] = true;
201+
202+
_pendingDeletes.remove(id);
203+
_pendingWrites[id] = writer;
234204
}
235205

236206
@override
237-
Future<void> writeAsString(
207+
void writeAsString(
238208
AssetId id,
239209
String contents, {
240210
Encoding encoding = utf8,
241-
required Future<void> Function() writer,
211+
required void Function() writer,
242212
}) {
243-
return _pool.withResource(() {
244-
// TODO: encoding?
245-
_stringContentCache[id] = contents;
246-
_bytesContentCache[id] = utf8.encode(contents);
247-
_canReadCache[id] = true;
248-
249-
_pendingDeletes.remove(id);
250-
_pendingWrites[id] = writer;
251-
return Future.value(null);
252-
});
213+
// TODO: encoding?
214+
_stringContentCache[id] = contents;
215+
_bytesContentCache[id] = utf8.encode(contents);
216+
_canReadCache[id] = true;
217+
218+
_pendingDeletes.remove(id);
219+
_pendingWrites[id] = writer;
253220
}
254221

255222
@override
256-
Future<void> delete(AssetId id, {required Future<void> Function() deleter}) {
257-
return _pool.withResource(() {
258-
_pendingWrites.remove(id);
259-
_pendingDeletes[id] = deleter;
260-
_canReadCache[id] = false;
261-
return Future.value(null);
262-
});
223+
void delete(AssetId id, {required void Function() deleter}) {
224+
_pendingWrites.remove(id);
225+
_pendingDeletes[id] = deleter;
226+
_canReadCache[id] = false;
263227
}
264228
}

build/test/state/filesystem_cache_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'dart:convert';
5+
/*import 'dart:convert';
66
77
import 'package:build/build.dart';
88
import 'package:build/src/state/filesystem_cache.dart';
9-
import 'package:test/test.dart';
9+
import 'package:test/test.dart';*/
1010

1111
void main() {
12-
final txt1 = AssetId('a', 'foo.txt');
12+
/*final txt1 = AssetId('a', 'foo.txt');
1313
final txt1String = 'txt1';
1414
final txt1Bytes = utf8.encode(txt1String);
1515
@@ -109,5 +109,5 @@ void main() {
109109
txt2String /* updated value */,
110110
);
111111
});
112-
});
112+
});*/
113113
}

0 commit comments

Comments
 (0)