Skip to content

Commit 9730e67

Browse files
authored
Clean up the old memoizer (#1854)
* Remove memoizer from dartdoc (never wound up being viable) and clean up grind so it depends on less * Restore relevant parts of model_utils_test
1 parent c43b63c commit 9730e67

File tree

5 files changed

+38
-382
lines changed

5 files changed

+38
-382
lines changed

lib/src/dartdoc_options.dart

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import 'dart:io';
2020
import 'package:analyzer/dart/element/element.dart';
2121
import 'package:args/args.dart';
2222
import 'package:dartdoc/dartdoc.dart';
23+
import 'package:dartdoc/src/io_utils.dart';
2324
import 'package:dartdoc/src/tuple.dart';
2425
import 'package:path/path.dart' as pathLib;
2526
import 'package:yaml/yaml.dart';
@@ -39,21 +40,6 @@ final Directory directoryCurrent = Directory.current;
3940
final String directoryCurrentPath =
4041
pathLib.canonicalize(Directory.current.path);
4142

42-
String resolveTildePath(String originalPath) {
43-
if (originalPath == null || !originalPath.startsWith('~/')) {
44-
return originalPath;
45-
}
46-
47-
String homeDir;
48-
49-
if (Platform.isWindows) {
50-
homeDir = pathLib.absolute(Platform.environment['USERPROFILE']);
51-
} else {
52-
homeDir = pathLib.absolute(Platform.environment['HOME']);
53-
}
54-
55-
return pathLib.join(homeDir, originalPath.substring(2));
56-
}
5743

5844
class DartdocOptionError extends DartdocFailure {
5945
DartdocOptionError(String details) : super(details);

lib/src/io_utils.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@ import 'dart:io';
1212

1313
import 'package:path/path.dart' as pathLib;
1414

15+
/// Return a resolved path including the home directory in place of tilde
16+
/// references.
17+
String resolveTildePath(String originalPath) {
18+
if (originalPath == null || !originalPath.startsWith('~/')) {
19+
return originalPath;
20+
}
21+
22+
String homeDir;
23+
24+
if (Platform.isWindows) {
25+
homeDir = pathLib.absolute(Platform.environment['USERPROFILE']);
26+
} else {
27+
homeDir = pathLib.absolute(Platform.environment['HOME']);
28+
}
29+
30+
return pathLib.join(homeDir, originalPath.substring(2));
31+
}
32+
1533
/// Lists the contents of [dir].
1634
///
1735
/// If [recursive] is `true`, lists subdirectory contents (defaults to `false`).

lib/src/model_utils.dart

Lines changed: 1 addition & 195 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@
44

55
library dartdoc.model_utils;
66

7-
import 'dart:collection';
87
import 'dart:convert';
98
import 'dart:io';
109

1110
import 'package:analyzer/dart/element/element.dart';
1211
import 'package:analyzer/src/generated/engine.dart';
1312
import 'package:analyzer/src/generated/sdk.dart';
1413
import 'package:analyzer/src/generated/source_io.dart';
15-
import 'package:collection/collection.dart';
1614
import 'package:dartdoc/src/model.dart';
1715
import 'package:path/path.dart' as pathLib;
18-
import 'package:quiver/core.dart';
1916

2017
final Map<String, String> _fileContents = <String, String>{};
2118

@@ -178,195 +175,4 @@ String crossdartifySource(
178175
newSource = sanitizer.convert(source);
179176
}
180177
return newSource;
181-
}
182-
183-
/// An UnmodifiableListView that computes equality and hashCode based on the
184-
/// equality and hashCode of its contained objects.
185-
class _HashableList {
186-
final List _source;
187-
int _hashCache;
188-
_HashableList(this._source);
189-
190-
@override
191-
bool operator ==(other) {
192-
if (other is _HashableList) {
193-
if (_source.length == other._source.length) {
194-
for (var index = 0; index < _source.length; ++index) {
195-
if (_source[index] != other._source[index]) return false;
196-
}
197-
return true;
198-
}
199-
}
200-
return false;
201-
}
202-
203-
@override
204-
int get hashCode => _hashCache ??= hashObjects(_source);
205-
}
206-
207-
/// Like [Memoizer], except in checked mode will validate that the value of the
208-
/// memoized function is unchanging using [DeepCollectionEquality]. Still
209-
/// returns the cached value assuming the assertion passes.
210-
class ValidatingMemoizer extends Memoizer {
211-
bool _assert_on_difference = false;
212-
213-
ValidatingMemoizer() : super() {
214-
// Assignment within assert to take advantage of the expression only
215-
// being executed in checked mode.
216-
assert(_assert_on_difference = true);
217-
invalidateMemos();
218-
}
219-
220-
/// In checked mode and when constructed with assert_on_difference == true,
221-
/// validate that the return value from f() equals the memoized value.
222-
/// Otherwise, a wrapper around putIfAbsent.
223-
@override
224-
R _cacheIfAbsent<R>(_HashableList key, R Function() f) {
225-
if (_assert_on_difference) {
226-
if (_memoizationTable.containsKey(key)) {
227-
R value = f();
228-
if (!new DeepCollectionEquality()
229-
.equals(value, _memoizationTable[key])) {
230-
throw new AssertionError('${value} != $_memoizationTable[key]');
231-
}
232-
}
233-
}
234-
return super._cacheIfAbsent(key, f);
235-
}
236-
}
237-
238-
/// A basic Memoizer class. Instantiate as a member variable, extend, or use
239-
/// as a mixin to track object-specific cached values, or instantiate directly
240-
/// to track other values.
241-
///
242-
/// For all methods in this class, the parameter [f] must be a tear-off method
243-
/// or top level function (not an inline closure) for memoization to work.
244-
/// [Memoizer] depends on the equality operator on the given function to detect
245-
/// when we are calling the same function.
246-
///
247-
/// Use:
248-
///
249-
/// ```dart
250-
/// String aTestFunction(String greeting, String name) => "${greeting}, ${name}";
251-
/// int aSlowFunction() { doSome(); return expensiveCalculations(); }
252-
///
253-
/// myMemoizer.memoized2(aTestFunction, "Hello, "world");
254-
/// myMemoizer.memoized(aSlowFunction);
255-
/// ```
256-
///
257-
/// *Not*:
258-
///
259-
/// ```dart
260-
/// String aTestFunction(String greeting, String name) => "${greeting}, ${name}";
261-
///
262-
/// myMemoizer.memoized2((a, b) => aTestFunction(a, b), "Hello", "world");
263-
/// myMemoizer.memoized(() => aSlowFunction());;
264-
/// ```
265-
class Memoizer {
266-
/// Map of a function and its positional parameters (if any), to a value.
267-
final Map<_HashableList, dynamic> _memoizationTable = new HashMap();
268-
269-
/// Reset the memoization table, forcing calls of the underlying functions.
270-
void invalidateMemos() {
271-
_memoizationTable.clear();
272-
}
273-
274-
/// A wrapper around putIfAbsent, exposed to allow overrides.
275-
R _cacheIfAbsent<R>(_HashableList key, R Function() f) {
276-
return _memoizationTable.putIfAbsent(key, f);
277-
}
278-
279-
/// Calls and caches the return value of [f]() if not in the cache, then
280-
/// returns the cached value of [f]().
281-
R memoized<R>(Function f, {String altKey}) {
282-
Object obj = altKey ?? f;
283-
_HashableList key = new _HashableList([obj]);
284-
return _cacheIfAbsent(key, f);
285-
}
286-
287-
/// Calls and caches the return value of [f]([param1]) if not in the cache, then
288-
/// returns the cached value of [f]([param1]).
289-
R memoized1<R, A>(R Function(A) f, A param1) {
290-
_HashableList key = new _HashableList([f, param1]);
291-
return _cacheIfAbsent(key, () => f(param1));
292-
}
293-
294-
/// Calls and caches the return value of [f]([param1], [param2]) if not in the
295-
/// cache, then returns the cached value of [f]([param1], [param2]).
296-
R memoized2<R, A, B>(R Function(A, B) f, A param1, B param2) {
297-
_HashableList key = new _HashableList([f, param1, param2]);
298-
return _cacheIfAbsent(key, () => f(param1, param2));
299-
}
300-
301-
/// Calls and caches the return value of [f]([param1], [param2], [param3]) if
302-
/// not in the cache, then returns the cached value of [f]([param1],
303-
/// [param2], [param3]).
304-
R memoized3<R, A, B, C>(R Function(A, B, C) f, A param1, B param2, C param3) {
305-
_HashableList key = new _HashableList([f, param1, param2, param3]);
306-
return _cacheIfAbsent(key, () => f(param1, param2, param3));
307-
}
308-
309-
/// Calls and caches the return value of [f]([param1], [param2], [param3],
310-
/// [param4]) if not in the cache, then returns the cached value of
311-
/// [f]([param1], [param2], [param3], [param4]).
312-
R memoized4<R, A, B, C, D>(
313-
R Function(A, B, C, D) f, A param1, B param2, C param3, D param4) {
314-
_HashableList key = new _HashableList([f, param1, param2, param3, param4]);
315-
return _cacheIfAbsent(key, () => f(param1, param2, param3, param4));
316-
}
317-
318-
/// Calls and caches the return value of [f]([param1], [param2], [param3],
319-
/// [param4], [param5]) if not in the cache, then returns the cached value of [f](
320-
/// [param1], [param2], [param3], [param4], [param5]).
321-
R memoized5<R, A, B, C, D, E>(R Function(A, B, C, D, E) f, A param1, B param2,
322-
C param3, D param4, E param5) {
323-
_HashableList key =
324-
new _HashableList([f, param1, param2, param3, param4, param5]);
325-
return _cacheIfAbsent(key, () => f(param1, param2, param3, param4, param5));
326-
}
327-
328-
/// Calls and caches the return value of [f]([param1], [param2], [param3],
329-
/// [param4], [param5], [param6]) if not in the cache, then returns the cached
330-
/// value of [f]([param1], [param2], [param3], [param4], [param5], [param6]).
331-
R memoized6<R, A, B, C, D, E, F>(R Function(A, B, C, D, E, F) f, A param1,
332-
B param2, C param3, D param4, E param5, F param6) {
333-
_HashableList key =
334-
new _HashableList([f, param1, param2, param3, param4, param5, param6]);
335-
return _cacheIfAbsent(
336-
key, () => f(param1, param2, param3, param4, param5, param6));
337-
}
338-
339-
/// Calls and caches the return value of [f]([param1], [param2], [param3],
340-
/// [param4], [param5], [param6], [param7]) if not in the cache, then returns
341-
/// the cached value of [f]([param1], [param2], [param3], [param4], [param5],
342-
/// [param6], [param7]).
343-
R memoized7<R, A, B, C, D, E, F, G>(R Function(A, B, C, D, E, F, G) f,
344-
A param1, B param2, C param3, D param4, E param5, F param6, G param7) {
345-
_HashableList key = new _HashableList(
346-
[f, param1, param2, param3, param4, param5, param6, param7]);
347-
return _cacheIfAbsent(
348-
key, () => f(param1, param2, param3, param4, param5, param6, param7));
349-
}
350-
351-
/// Calls and caches the return value of [f]([param1], [param2], [param3],
352-
/// [param4], [param5], [param6], [param7], [param8]) if not in the cache,
353-
/// then returns the cached value of [f]([param1], [param2], [param3],
354-
/// [param4], [param5], [param6], [param7], [param8]).
355-
R memoized8<R, A, B, C, D, E, F, G, H>(
356-
R Function(A, B, C, D, E, F, G, H) f,
357-
A param1,
358-
B param2,
359-
C param3,
360-
D param4,
361-
E param5,
362-
F param6,
363-
G param7,
364-
H param8) {
365-
_HashableList key = new _HashableList(
366-
[f, param1, param2, param3, param4, param5, param6, param7, param8]);
367-
return _cacheIfAbsent(
368-
key,
369-
() =>
370-
f(param1, param2, param3, param4, param5, param6, param7, param8));
371-
}
372-
}
178+
}

0 commit comments

Comments
 (0)