Skip to content

Commit f4c7f90

Browse files
📚 docs(Optimization): Improve readability.
1 parent 2d784d6 commit f4c7f90

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

doc/manual/optimization.md

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ V8 optimization inhibitors, or anything else..._
1212
What the benchmark code does is, in order:
1313

1414
- build an empty tree `T`
15-
- add 100000 elements to the beginning of `T`
16-
- remove those 100000 elements by popping the first element of `T` 100000 times
17-
- add 100000 elements to the end of `T`
15+
- add 100000 values to the beginning of `T`
16+
- remove those 100000 values by popping the first value of `T` 100000 times
17+
- add 100000 values to the end of `T`
1818
- split `T` at all 100000 positions, one after the other
19-
- remove the 100000 elements of `T` by popping the last element 100000 times
19+
- remove the 100000 values of `T` by popping the last value 100000 times
2020

2121
Here are the first running times I measured for this implementation. It is not
22-
difficult to see that the implementation is not fast at all. 100000 elements is
22+
difficult to see that the implementation is not fast at all. 100000 values is
2323
not that much right?
2424

2525
```sh
@@ -32,11 +32,11 @@ split: 144243ms
3232
init: 6896ms
3333
```
3434

35-
However most of the motivation for those optimization steps came from applying
35+
Most of the motivation for those optimization steps came from applying
3636
the same benchmark to
3737
[another JavaScript implementation](https://github.com/qiao/fingertree.js)
38-
(actually, I copied the benchmark from Joe's repository). This made it obvious
39-
that my implementation was performing badly.
38+
(actually, I initially copied the benchmark from Joe's repository).
39+
This made it obvious that my implementation was performing badly.
4040

4141
```sh
4242
$ node ../fingertree.js/benchmark/benchmark.js
@@ -51,7 +51,7 @@ init: 141ms
5151
## First implementation of lazy evaluation of subtrees
5252
> f0ca58169a32140a8617625466f15489a3549124
5353
54-
A Deep instance posseses a subtree which in some cases is not accessed at all
54+
A Deep instance possesses a subtree which in some cases is not accessed at all
5555
during the whole lifetime of its parent. So why build it? The idea is to delay
5656
the construction of the object, which might be expensive, to a later point in
5757
time using a proxy: a finger tree look-alike object that will construct the
@@ -80,8 +80,8 @@ structure defined as:
8080
```js
8181
class Measured {
8282

83-
constructor ( element , v ) {
84-
this.element = element ;
83+
constructor ( value , v ) {
84+
this.value = value ;
8585
this.v = v ;
8686
}
8787

@@ -92,23 +92,22 @@ class Measured {
9292
}
9393
```
9494

95-
where `v` is the value returned by `Measure.measure( element )`. This wrapping
96-
allowed to give the same interface to elements, nodes, digits and trees with
95+
where `v` is the measure returned by `Measure.measure( value )`. This wrapping
96+
allowed to give the same interface to values, nodes, digits and trees with
9797
respect to computing measures. This approach has at least 2 problems:
9898

9999
- It introduces one additional level of indirection by wrapping all values.
100-
- The way it was implemented here caches measures for all elements. This might not
100+
- The way it was implemented here caches measures for all values. This might not
101101
be a good thing in settings where measures are big objects. Moreover, this
102-
is in contradiction with the *lazy way* of functional programming
103-
paradigms.
102+
is in contradiction with the *lazy way* of functional programming paradigms.
104103

105104
However, there is a simpler way to solve the *common interface* problem. Indeed,
106-
the elements contained in the digits of the root tree can be measured
105+
the values contained in the digits of the root tree can be measured
107106
with the Measure object left unchanged, and starting from the subtree of the
108107
root level we just need a modified Measure object that uses the interface of
109108
Node2 and Node3 to compute the node measures. Never will digits contain objects
110109
of different types, so patching the Measure object this way suffices, there is
111-
no need to give the same interface to nodes and elements.
110+
no need to give the same interface to nodes and values.
112111

113112
Here are the measurements after the change:
114113

@@ -127,7 +126,7 @@ Mostly noise for the moment. There must be something else to fix...
127126
## Removing iterator loops and bindings for fixed size sequences
128127
> 28f6ee08d33e2cb2b9d0e5cae69948a7013bc0b7
129128
130-
At some point I decided to use a better tool than intuition to progress:
129+
At this point I decided to use a better tool than intuition to progress:
131130
profiling. This change is the first where I guided the pruning by looking at
132131
the output of `node --prof benchmark/tree.js` using `node-tick-processor`
133132
from the [tick](https://www.npmjs.com/package/tick) package.
@@ -139,7 +138,7 @@ reduce( M.plus.bind( M ) , map( M.measure.bind( M ) , digit ) , M.zero( ) ) ;
139138
```
140139

141140
However, since all four digit types are hard-coded has classes One, Two, Three
142-
and Four we could as well give a custom implementation of the measure( ) method
141+
and Four we could as well give a custom implementation of the `measure( )` method
143142
to each digit type. For type Two for example, we have:
144143

145144
```js
@@ -174,8 +173,8 @@ split method...
174173
## Removing loops: part II
175174
> 5b0af102240b3f0cfda7001f6b108a9811594c7a
176175
177-
It turns out I didn't remove all occurences of this generic digit measurement
178-
method. Here are the running times after removing occurences of this method
176+
It turns out I didn't remove all occurrences of this generic digit measurement
177+
method. Here are the running times after removing occurrences of this method
179178
from `Deep.splitTree`.
180179

181180
```js
@@ -195,7 +194,7 @@ Better.
195194
> 726354a3bd0591d767fb658d206680a6a74d2fbb
196195
197196
These two commits introduce specialized procedures for building trees from
198-
small lists and digits. These are really small trees, i.e. 0 to 4 elements, so
197+
small lists and digits. These are really small trees, i.e. 0 to 4 values, so
199198
why use the generic `from_iterable` constructor?
200199

201200
```js

0 commit comments

Comments
 (0)