Skip to content

Commit 574b882

Browse files
committed
Removed variable _cachedView.
1 parent eeb6d62 commit 574b882

File tree

4 files changed

+33
-36
lines changed

4 files changed

+33
-36
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 0.2.2
2+
- Removed variable `_cacheView`. Amended `lazy_benchmark.dart`.
3+
14
# 0.2.1
25
- Fixed `LazyList`, `LazySet`, and `LazyMap`. The call method now returns
36
the same object until an update of the cache is requested.

benchmark/bin/lazy_benchmark.dart

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,23 @@ import 'dart:math';
33
import 'package:benchmark_harness/benchmark_harness.dart';
44
import 'package:lazy_memo/lazy_memo.dart';
55

6-
final lazy = Lazy<double>(() => sqrt(27));
7-
double testFunc() => sqrt(27);
8-
late double a;
6+
final lazy = LazyMap<int, double>(() => <int, double>{
7+
27: sqrt(27),
8+
29: sqrt(29),
9+
});
10+
Map<int, double> testFunc() => <int, double>{
11+
27: sqrt(27),
12+
29: sqrt(29),
13+
};
14+
15+
late Map<int, double> map;
916

1017
// Create a new benchmark by extending BenchmarkBase
1118
class CallBenchmark extends BenchmarkBase {
12-
const CallBenchmark() : super('Lazy<double>(() => sqrt(27)) ');
19+
const CallBenchmark() : super('''LazyMap<int, double>(() => <int, double>{
20+
27: sqrt(27),
21+
29: sqrt(29),
22+
}); \n''');
1323

1424
static void main() {
1525
const CallBenchmark().report();
@@ -18,7 +28,7 @@ class CallBenchmark extends BenchmarkBase {
1828
// The benchmark code.
1929
@override
2030
void run() {
21-
a = lazy();
31+
map = lazy();
2232
}
2333

2434
// Not measured setup code executed prior to the benchmark runs.
@@ -36,7 +46,10 @@ class CallBenchmark extends BenchmarkBase {
3646

3747
// Create a new benchmark by extending BenchmarkBase
3848
class FunctionCallBenchmark extends BenchmarkBase {
39-
const FunctionCallBenchmark() : super('double testFunc() => sqrt(27) ');
49+
const FunctionCallBenchmark() : super('''<int, double>{
50+
27: sqrt(27),
51+
29: sqrt(29),
52+
}; \n''');
4053

4154
static void main() {
4255
const FunctionCallBenchmark().report();
@@ -45,7 +58,7 @@ class FunctionCallBenchmark extends BenchmarkBase {
4558
// The benchmark code.
4659
@override
4760
void run() {
48-
a = testFunc();
61+
map = testFunc();
4962
}
5063

5164
// Not measured setup code executed prior to the benchmark runs.
@@ -62,6 +75,8 @@ class FunctionCallBenchmark extends BenchmarkBase {
6275
}
6376

6477
void main() {
78+
print('Returning cached map:');
6579
CallBenchmark.main();
80+
print('\nBuilding map object:');
6681
FunctionCallBenchmark.main();
6782
}

lib/src/lazy.dart

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,17 @@ class LazyList<T> extends Lazy<List<T>> {
5656
/// Constructs an object of type `LazyList<T>`.
5757
LazyList(super.objectFactory);
5858

59-
/// Returns the cached object.
60-
/// * The object is initialized when first accessed.
61-
/// * To re-initialize the cached object use the
62-
/// optional parameter `updateCache`.
6359
@override
6460
List<T> call({
6561
bool updateCache = false,
6662
}) {
6763
if (updateCache || !_isUpToDate) {
6864
_isUpToDate = true;
69-
_cache = objectFactory();
70-
_cacheView = UnmodifiableListView(_cache);
65+
_cache = UnmodifiableListView(objectFactory());
7166
}
72-
return _cacheView;
67+
return _cache;
7368
}
7469

75-
late UnmodifiableListView<T> _cacheView;
76-
7770
@override
7871
String toString() {
7972
return 'LazyList<$T>: ${call()}';
@@ -90,24 +83,17 @@ class LazySet<T> extends Lazy<Set<T>> {
9083
/// Constructs an object of type `LazySet<T>`.
9184
LazySet(super.objectFactory);
9285

93-
/// Returns the cached object.
94-
/// * The object is initialized when first accessed.
95-
/// * To re-initialize the cached object use the
96-
/// optional parameter `updateCache`.
9786
@override
9887
Set<T> call({
9988
bool updateCache = false,
10089
}) {
10190
if (updateCache || !_isUpToDate) {
10291
_isUpToDate = true;
103-
_cache = objectFactory();
104-
_cacheView = UnmodifiableSetView(_cache);
92+
_cache = UnmodifiableSetView(objectFactory());
10593
}
106-
return _cacheView;
94+
return _cache;
10795
}
10896

109-
late UnmodifiableSetView<T> _cacheView;
110-
11197
@override
11298
String toString() {
11399
return 'LazySet<$T>: ${call()}';
@@ -124,26 +110,19 @@ class LazyMap<K, V> extends Lazy<Map<K, V>> {
124110
/// Constructs an object of type `LazyMap<T>`.
125111
LazyMap(super.objectFactory);
126112

127-
/// Returns the cached object.
128-
/// * The object is initialized when first accessed.
129-
/// * To re-initialize the cached object use the
130-
/// optional parameter `updateCache`.
131113
@override
132114
Map<K, V> call({
133115
bool updateCache = false,
134116
}) {
135117
if (updateCache || !_isUpToDate) {
136118
_isUpToDate = true;
137-
_cache = objectFactory();
138-
_cacheView = UnmodifiableMapView(_cache);
119+
_cache = UnmodifiableMapView(objectFactory());
139120
}
140-
return _cacheView;
121+
return _cache;
141122
}
142123

143-
late UnmodifiableMapView<K, V> _cacheView;
144-
145124
@override
146125
String toString() {
147-
return 'LazyMap<K, V>: ${call()}';
126+
return 'LazyMap<$K, $V>: ${call()}';
148127
}
149128
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: lazy_memo
33
description: Lazy variables that can be marked for re-initialization and
44
memoized single and double argument functions.
55

6-
version: 0.2.1
6+
version: 0.2.2
77

88
homepage: https://github.com/simphotonics/lazy_memo
99

0 commit comments

Comments
 (0)