You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+54-14Lines changed: 54 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# js-resource-counter
2
2
3
-
Sequentially Allocatable and Deallocatable Resource Counter written in JavaScript. It is useful for tracking resource usage such as inodes and file descriptors. The resource counter is backed by a new lazy recursive perfectly balanced dynamically growing and shrinking bitmap tree data structure. This allows logarithmic allocation and deallocation performance. It's memory usage is better than the alternative deallocated stack + counter method.
3
+
Sequentially Allocatable and Deallocatable Resource Counter written in JavaScript. It is useful for tracking resource usage such as inodes and file descriptors. The resource counter is backed by a new lazy recursive perfectly balanced dynamically growing and shrinking fully-persistent bitmap tree data structure. This allows logarithmic allocation and deallocation performance. It's memory usage is better than the alternative deallocated stack + counter method.
// if you do use them, then you will now have diverging cs
44
+
let c =newCounterImmutable;
45
+
let first, second, third, fourth;
46
+
[first, c] =c.allocate();
47
+
[second, c] =c.allocate();
48
+
[third, c] =c.allocate();
49
+
[fourth, c] =c.allocate();
50
+
[, c] =c.deallocate(second);
51
+
[, c] =c.deallocate(third);
52
+
let first_, second_, fifth_;
53
+
[first_, c] =c.allocate();
54
+
[second_, c] =c.allocate();
55
+
[fifth_, c] =c.allocate();
56
+
console.log(first_ === second); // true
57
+
console.log(second_ === third); // true
58
+
console.log(fifth_ === (fourth +1)); // true
59
+
60
+
// you can also perform a transaction
61
+
c =c.transaction((ct) => {
62
+
constnumber=ct.allocate();
63
+
ct.allocate();
64
+
ct.deallocate(number);
65
+
console.log(number); // 5
66
+
});
67
+
```
68
+
69
+
This can be useful if you need to combine `CounterImmutable` with other fully-persistent data structures to create composite data structures.
70
+
29
71
Documentation
30
72
--------------
31
73
32
74
Documentation is located in the `doc` folder. You can also view the [rendered HTML](http://cdn.rawgit.com/MatrixAI/js-resource-counter/4360d14/doc/index.html).
33
75
34
76
Performance behaviour is lazy memory allocation on counter allocation (for both balanced tree growth and explicit counter allocation). This laziness means intermediate tree nodes won't be allocated when explicitly allocating a counter that has intermediate values. For example allocating only 0 and 500, tree nodes won't be created eagerly in anticipation for counter values 1 to 499.
35
77
36
-
Memory deallocation only occurs when the highest counter is deallocated and the entire counter block for the leaf that it occupies are all also deallocated. So memory deallocation won't occur for deallocating intermediate counter values. Also the
37
-
38
-
It's possible to make the tree also eagerly deallocate memory even for intermediate counter deallocations, but this is inefficient, since you will probably have to use those counter values again later anyway. It may reduce dynamic memory usage, but at the cost of repeatedly allocating and deallocating memory for the same counter value.
78
+
By default both `Counter` and `CounterImmutable` will lazily grow and shrink the tree as necessary. However shrinking adds extra performance overhead for the benefit of more tighter memory usage. If you expect to always use the same set of numbers for allocation and deallocation, it will be faster if you disable shrinking. You can do this by passing the `shrink` parameter as `false`. See the documentation for more.
0 commit comments