Skip to content

Add a method memoizer to dartdoc #1571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Dec 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 159 additions & 0 deletions lib/src/model_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

library dartdoc.model_utils;

import 'dart:collection';
import 'dart:convert';
import 'dart:io';

Expand All @@ -12,6 +13,7 @@ import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/sdk.dart';
import 'package:analyzer/src/generated/source_io.dart';
import 'package:dartdoc/src/model.dart';
import 'package:quiver_hashcode/hashcode.dart';

import 'config.dart';

Expand Down Expand Up @@ -166,3 +168,160 @@ String crossdartifySource(
}
return newSource;
}

/// An UnmodifiableListView that computes equality and hashCode based on the
/// equality and hashCode of its contained objects.
class _HashableList extends UnmodifiableListView<dynamic> {
_HashableList(Iterable<dynamic> iterable) : super(iterable);

@override
bool operator ==(other) {
if (other is _HashableList) {
if (this.length == other.length) {
for (var index = 0; index < length; ++index) {
if (this[index] != other[index]) return false;
}
return true;
}
}
return false;
}

@override
get hashCode => hashObjects(this);
}

/// Extend or use as a mixin to track object-specific cached values, or
/// instantiate directly to track other values.
///
/// For all methods in this class, the parameter [f] must be a tear-off method
/// or top level function (not an inline closure) for memoization to work.
/// [Memoizer] depends on the equality operator on the given function to detect
/// when we are calling the same function.
///
/// Use:
///
/// ```dart
/// String aTestFunction(String greeting, String name) => "${greeting}, ${name}";
/// int aSlowFunction() { doSome(); return expensiveCalculations(); }
///
/// myMemoizer.memoized2(aTestFunction, "Hello, "world");
/// myMemoizer.memoized(aSlowFunction);
/// ```
///
/// *Not*:
///
/// ```dart
/// String aTestFunction(String greeting, String name) => "${greeting}, ${name}";
///
/// myMemoizer.memoized2((a, b) => aTestFunction(a, b), "Hello", "world");
/// myMemoizer.memoized(() => aSlowFunction());;
/// ```
class Memoizer {
/// Map of a function and its positional parameters (if any), to a value.
Map<_HashableList, dynamic> _memoizationTable;

Memoizer() {
invalidateMemos();
}

/// Reset the memoization table, forcing calls of the underlying functions.
void invalidateMemos() {
_memoizationTable = new Map();
}

/// Calls and caches the return value of [f]() if not in the cache, then
/// returns the cached value of [f]().
R memoized<R>(Function f) {
_HashableList key = new _HashableList([f]);
return _memoizationTable.putIfAbsent(key, f);
}

/// Calls and caches the return value of [f]([param1]) if not in the cache, then
/// returns the cached value of [f]([param1]).
R memoized1<R, A>(R Function(A) f, A param1) {
_HashableList key = new _HashableList([f, param1]);
return _memoizationTable.putIfAbsent(key, () => f(param1));
}

/// Calls and caches the return value of [f]([param1], [param2]) if not in the
/// cache, then returns the cached value of [f]([param1], [param2]).
R memoized2<R, A, B>(R Function(A, B) f, A param1, B param2) {
_HashableList key = new _HashableList([f, param1, param2]);
return _memoizationTable.putIfAbsent(key, () => f(param1, param2));
}

/// Calls and caches the return value of [f]([param1], [param2], [param3]) if
/// not in the cache, then returns the cached value of [f]([param1],
/// [param2], [param3]).
R memoized3<R, A, B, C>(R Function(A, B, C) f, A param1, B param2, C param3) {
_HashableList key = new _HashableList([f, param1, param2, param3]);
return _memoizationTable.putIfAbsent(key, () => f(param1, param2, param3));
}

/// Calls and caches the return value of [f]([param1], [param2], [param3],
/// [param4]) if not in the cache, then returns the cached value of
/// [f]([param1], [param2], [param3], [param4]).
R memoized4<R, A, B, C, D>(
R Function(A, B, C, D) f, A param1, B param2, C param3, D param4) {
_HashableList key = new _HashableList([f, param1, param2, param3, param4]);
return _memoizationTable.putIfAbsent(
key, () => f(param1, param2, param3, param4));
}

/// Calls and caches the return value of [f]([param1], [param2], [param3],
/// [param4], [param5]) if not in the cache, then returns the cached value of [f](
/// [param1], [param2], [param3], [param4], [param5]).
R memoized5<R, A, B, C, D, E>(R Function(A, B, C, D, E) f, A param1, B param2,
C param3, D param4, E param5) {
_HashableList key =
new _HashableList([f, param1, param2, param3, param4, param5]);
return _memoizationTable.putIfAbsent(
key, () => f(param1, param2, param3, param4, param5));
}

/// Calls and caches the return value of [f]([param1], [param2], [param3],
/// [param4], [param5], [param6]) if not in the cache, then returns the cached
/// value of [f]([param1], [param2], [param3], [param4], [param5], [param6]).
R memoized6<R, A, B, C, D, E, F>(R Function(A, B, C, D, E, F) f, A param1,
B param2, C param3, D param4, E param5, F param6) {
_HashableList key =
new _HashableList([f, param1, param2, param3, param4, param5, param6]);
return _memoizationTable.putIfAbsent(
key, () => f(param1, param2, param3, param4, param5, param6));
}

/// Calls and caches the return value of [f]([param1], [param2], [param3],
/// [param4], [param5], [param6], [param7]) if not in the cache, then returns
/// the cached value of [f]([param1], [param2], [param3], [param4], [param5],
/// [param6], [param7]).
R memoized7<R, A, B, C, D, E, F, G>(R Function(A, B, C, D, E, F, G) f,
A param1, B param2, C param3, D param4, E param5, F param6, G param7) {
_HashableList key = new _HashableList(
[f, param1, param2, param3, param4, param5, param6, param7]);
return _memoizationTable.putIfAbsent(
key, () => f(param1, param2, param3, param4, param5, param6, param7));
}

/// Calls and caches the return value of [f]([param1], [param2], [param3],
/// [param4], [param5], [param6], [param7], [param8]) if not in the cache,
/// then returns the cached value of [f]([param1], [param2], [param3],
/// [param4], [param5], [param6], [param7], [param8]).
R memoized8<R, A, B, C, D, E, F, G, H>(
R Function(A, B, C, D, E, F, G, H) f,
A param1,
B param2,
C param3,
D param4,
E param5,
F param6,
G param7,
H param8) {
_HashableList key = new _HashableList(
[f, param1, param2, param3, param4, param5, param6, param7, param8]);
return _memoizationTable.putIfAbsent(
key,
() =>
f(param1, param2, param3, param4, param5, param6, param7, param8));
}
}
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -374,4 +374,4 @@ packages:
source: hosted
version: "2.1.13"
sdks:
dart: ">=1.23.0 <=2.0.0-dev.10.0"
dart: ">=1.23.0 <=2.0.0-dev.12.0"
140 changes: 140 additions & 0 deletions test/model_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,83 @@ library dartdoc.model_utils_test;
import 'package:dartdoc/src/model_utils.dart';
import 'package:test/test.dart';

class MemoizerUser extends Memoizer {
int foo = 0;
// These are actually not things you would ordinarily memoize, because
// they change. But they are useful for testing.
int _toMemoize() {
return foo++;
}

int get toMemoize => memoized(_toMemoize);

String _memoizedParameter1(String param) => "${foo++} ${param}";
String memoizedParameter1(String param) =>
memoized1(_memoizedParameter1, param);

String _memoizedParameter2(String param, String param2) =>
"${foo++} ${param} ${param2}";
String memoizedParameter2(String param, String param2) =>
memoized2(_memoizedParameter2, param, param2);

String _memoizedParameter3(String param, String param2, String param3) =>
"${foo++} ${param} ${param2} ${param3}";
String memoizedParameter3(String param, String param2, String param3) =>
memoized3(_memoizedParameter3, param, param2, param3);

String _memoizedParameter4(
String param, String param2, String param3, String param4) =>
"${foo++} ${param} ${param2} ${param3} ${param4}";
String memoizedParameter4(
String param, String param2, String param3, String param4) =>
memoized4(_memoizedParameter4, param, param2, param3, param4);

String _memoizedParameter5(String param, String param2, String param3,
String param4, String param5) =>
"${foo++} ${param} ${param2} ${param3} ${param4} ${param5}";
String memoizedParameter5(String param, String param2, String param3,
String param4, String param5) =>
memoized5(_memoizedParameter5, param, param2, param3, param4, param5);

String _memoizedParameter6(String param, String param2, String param3,
String param4, String param5, String param6) =>
"${foo++} ${param} ${param2} ${param3} ${param4} ${param5} ${param6}";
String memoizedParameter6(String param, String param2, String param3,
String param4, String param5, String param6) =>
memoized6(
_memoizedParameter6, param, param2, param3, param4, param5, param6);

String _memoizedParameter7(String param, String param2, String param3,
String param4, String param5, String param6, String param7) =>
"${foo++} ${param} ${param2} ${param3} ${param4} ${param5} ${param6} ${param7}";
String memoizedParameter7(String param, String param2, String param3,
String param4, String param5, String param6, String param7) =>
memoized7(_memoizedParameter7, param, param2, param3, param4, param5,
param6, param7);

String _memoizedParameter8(
String param,
String param2,
String param3,
String param4,
String param5,
String param6,
String param7,
String param8) =>
"${foo++} ${param} ${param2} ${param3} ${param4} ${param5} ${param6} ${param7} ${param8}";
String memoizedParameter8(
String param,
String param2,
String param3,
String param4,
String param5,
String param6,
String param7,
String param8) =>
memoized8(_memoizedParameter8, param, param2, param3, param4, param5,
param6, param7, param8);
}

void main() {
group('model_utils stripIndentFromSource', () {
test('no indent', () {
Expand Down Expand Up @@ -52,4 +129,67 @@ void main() {
'void foo() {\n print(1);\n}\n');
});
});

group('model_utils MethodMemoizer', () {
test('basic memoization and invalidation', () {
var m = new MemoizerUser();
expect(m.toMemoize, equals(0), reason: "initialization problem");
expect(m.toMemoize, equals(0), reason: "failed to memoize");
m.invalidateMemos();
expect(m.toMemoize, equals(1), reason: "failed to invalidate");
});

test('memoization of a method with parameter', () {
var m = new MemoizerUser();
expect(m.memoizedParameter1("hello"), equals("0 hello"),
reason: "initialization problem");
expect(m.memoizedParameter1("hello"), equals("0 hello"),
reason: "failed to memoize");
expect(m.memoizedParameter1("goodbye"), equals("1 goodbye"));
expect(m.memoizedParameter1("something"), equals("2 something"));
m.invalidateMemos();
expect(m.memoizedParameter1("hello"), equals("3 hello"),
reason: "failed to invalidate");
});

test('memoization of many parameters', () {
var m = new MemoizerUser();
expect(m.memoizedParameter1("hello"), equals("0 hello"));
expect(m.memoizedParameter2("hello", "obi"), equals("1 hello obi"));
expect(m.memoizedParameter3("hello", "obi", "wan"),
equals("2 hello obi wan"));
expect(m.memoizedParameter4("hello", "obi", "wan", "how"),
equals("3 hello obi wan how"));
expect(m.memoizedParameter5("hello", "obi", "wan", "how", "are"),
equals("4 hello obi wan how are"));
expect(m.memoizedParameter6("hello", "obi", "wan", "how", "are", "you"),
equals("5 hello obi wan how are you"));
expect(
m.memoizedParameter7(
"hello", "obi", "wan", "how", "are", "you", "doing"),
equals("6 hello obi wan how are you doing"));
expect(
m.memoizedParameter8(
"hello", "obi", "wan", "how", "are", "you", "doing", "today"),
equals("7 hello obi wan how are you doing today"));
expect(m.memoizedParameter1("hello"), equals("0 hello"));
expect(m.memoizedParameter2("hello", "obi"), equals("1 hello obi"));
expect(m.memoizedParameter3("hello", "obi", "wan"),
equals("2 hello obi wan"));
expect(m.memoizedParameter4("hello", "obi", "wan", "how"),
equals("3 hello obi wan how"));
expect(m.memoizedParameter5("hello", "obi", "wan", "how", "are"),
equals("4 hello obi wan how are"));
expect(m.memoizedParameter6("hello", "obi", "wan", "how", "are", "you"),
equals("5 hello obi wan how are you"));
expect(
m.memoizedParameter7(
"hello", "obi", "wan", "how", "are", "you", "doing"),
equals("6 hello obi wan how are you doing"));
expect(
m.memoizedParameter8(
"hello", "obi", "wan", "how", "are", "you", "doing", "today"),
equals("7 hello obi wan how are you doing today"));
});
});
}
30 changes: 9 additions & 21 deletions tool/grind.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,23 @@ import 'dart:convert';
import 'dart:io' hide ProcessException;

import 'package:dartdoc/src/io_utils.dart';
import 'package:dartdoc/src/model_utils.dart';
import 'package:grinder/grinder.dart';
import 'package:path/path.dart' as path;
import 'package:yaml/yaml.dart' as yaml;

main([List<String> args]) => grind(args);

Directory _dartdocDocsDir;
Directory get dartdocDocsDir {
if (_dartdocDocsDir == null) {
_dartdocDocsDir = Directory.systemTemp.createTempSync('dartdoc');
}
return _dartdocDocsDir;
}
// Directory.systemTemp is not a constant. So wrap it.
Directory createTempSync(String prefix) =>
Directory.systemTemp.createTempSync(prefix);

Directory _sdkDocsDir;
Directory get sdkDocsDir {
if (_sdkDocsDir == null) {
_sdkDocsDir = Directory.systemTemp.createTempSync('sdkdocs');
}
return _sdkDocsDir;
}
final Memoizer tempdirsCache = new Memoizer();

Directory _flutterDir;
Directory get flutterDir {
if (_flutterDir == null) {
_flutterDir = Directory.systemTemp.createTempSync('flutter');
}
return _flutterDir;
}
Directory get dartdocDocsDir =>
tempdirsCache.memoized1(createTempSync, 'dartdoc');
Directory get sdkDocsDir => tempdirsCache.memoized1(createTempSync, 'sdkdocs');
Directory get flutterDir => tempdirsCache.memoized1(createTempSync, 'flutter');

final Directory flutterDirDevTools =
new Directory(path.join(flutterDir.path, 'dev', 'tools'));
Expand Down