@graphile/lru 4.11.0 → 5.0.0-0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # @graphile/lru
2
+
3
+ ## 5.0.0-0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#125](https://github.com/benjie/postgraphile-private/pull/125)
8
+ [`91f2256b3`](https://github.com/benjie/postgraphile-private/commit/91f2256b3fd699bec19fc86f1ca79df057e58639)
9
+ Thanks [@benjie](https://github.com/benjie)! - Initial changesets release
package/LICENSE.md CHANGED
@@ -2,23 +2,19 @@
2
2
 
3
3
  Copyright © `2019` Benjie Gillam
4
4
 
5
- Permission is hereby granted, free of charge, to any person
6
- obtaining a copy of this software and associated documentation
7
- files (the Software”), to deal in the Software without
8
- restriction, including without limitation the rights to use,
9
- copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- copies of the Software, and to permit persons to whom the
11
- Software is furnished to do so, subject to the following
12
- conditions:
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the “Software”), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
13
11
 
14
- The above copyright notice and this permission notice shall be
15
- included in all copies or substantial portions of the Software.
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
16
14
 
17
- THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
18
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24
- OTHER DEALINGS IN THE SOFTWARE.
15
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,50 @@
1
1
  # @graphile/lru
2
2
 
3
- You probably want [lru-cache](https://github.com/isaacs/node-lru-cache) instead.
3
+ **You probably want [lru-cache](https://github.com/isaacs/node-lru-cache)
4
+ instead.**
5
+
6
+ This is an obsessively optimized LRU cache for Node.js, it forgoes features in
7
+ favour of performance, and is very marginally faster than node-lru-cache v6 in
8
+ certain circumstances (namely those that we care about inside the Graphile
9
+ internals). A performance comparison versus node-lru-cache v7 has not yet been
10
+ performed.
11
+
12
+ ## Usage
13
+
14
+ ```js
15
+ import LRU from "@graphile/lru";
16
+
17
+ // Construct an LRU cache
18
+ const lru = new LRU({
19
+ // Maximum number of items to store (required)
20
+ maxLength: 500, // Must be an integer at least 2
21
+
22
+ // Optional callback to handle when a value is removed (both when LRU becomes
23
+ // full and when LRU is reset)
24
+ dispose(key, value) {
25
+ console.log(`Disposing of key '${key}' with value '${inspect(value)}'`);
26
+ },
27
+ });
28
+
29
+ const ANY_KEY_HERE = { foo: "bar" };
30
+ const ANY_VALUE_HERE = { randomNumber: () => 4 };
31
+ // Store a value
32
+ lru.set(ANY_KEY_HERE, ANY_VALUE_HERE);
33
+
34
+ // Retrieve a value
35
+ const value = lru.get(ANY_KEY_HERE);
36
+
37
+ // Clear the cache
38
+ lru.reset();
39
+ ```
40
+
41
+ ## Considering a pull request?
42
+
43
+ Pull requests that add features that aren't required by the Graphile suite are
44
+ unlikely to be entertained - use node-lru-cache instead!
45
+
46
+ Pull requests that improve performance should come with benchmarks, benchmark
47
+ scripts and explanation of technique. And if you pull it off without breaking
48
+ anything - YES PLEASE.
49
+
50
+ Pull requests that add documentation are welcome.
@@ -0,0 +1,30 @@
1
+ export interface LRUOptions<KeyType, ValueType> {
2
+ maxLength: number;
3
+ dispose?: (key: KeyType, value: ValueType) => void;
4
+ }
5
+ /**
6
+ * An tiny LRU cache with maximum count, identical weighting and no expiration.
7
+ */
8
+ export declare class LRU<KeyType = any, ValueType = any> {
9
+ length: number;
10
+ /** max length */
11
+ private m;
12
+ /** head */
13
+ private h;
14
+ /** tail */
15
+ private t;
16
+ /** cache */
17
+ private c;
18
+ /** dispose */
19
+ private d;
20
+ constructor({ maxLength, dispose }: LRUOptions<KeyType, ValueType>);
21
+ reset(): void;
22
+ get(key: KeyType): ValueType | undefined;
23
+ set(key: KeyType, value: ValueType): void;
24
+ /** hoist (aka "raise") */
25
+ private r;
26
+ /** add */
27
+ private a;
28
+ }
29
+ export default LRU;
30
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAkCA,MAAM,WAAW,UAAU,CAAC,OAAO,EAAE,SAAS;IAC5C,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS,KAAK,IAAI,CAAC;CACpD;AAED;;GAEG;AACH,qBAAa,GAAG,CAAC,OAAO,GAAG,GAAG,EAAE,SAAS,GAAG,GAAG;IACtC,MAAM,EAAE,MAAM,CAAC;IACtB,iBAAiB;IACjB,OAAO,CAAC,CAAC,CAAS;IAClB,WAAW;IACX,OAAO,CAAC,CAAC,CAAkC;IAC3C,WAAW;IACX,OAAO,CAAC,CAAC,CAAkC;IAC3C,YAAY;IACZ,OAAO,CAAC,CAAC,CAAyC;IAClD,cAAc;IACd,OAAO,CAAC,CAAC,CAAoD;gBAEjD,EAAE,SAAS,EAAE,OAAO,EAAE,EAAE,UAAU,CAAC,OAAO,EAAE,SAAS,CAAC;IAkB3D,KAAK;IAaL,GAAG,CAAC,GAAG,EAAE,OAAO,GAAG,SAAS,GAAG,SAAS;IASxC,GAAG,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS,GAAG,IAAI;IAgBhD,0BAA0B;IAC1B,OAAO,CAAC,CAAC;IAuBT,UAAU;IACV,OAAO,CAAC,CAAC;CAyBV;AACD,eAAe,GAAG,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+ /*
3
+ * I know this is ridiculous, but I've seen in the past that the length of keys
4
+ * in objects can have a marginal impact on performance. @graphile/lru is
5
+ * obsessively optimized, so we'll take every smidging of performance we can
6
+ * get. As such, all non-public keys are single letters. To try and keep things
7
+ * from being too confusing, we try not to use the same letter in two places.
8
+ * Letters:
9
+ *
10
+ * Letter | Place | longName
11
+ * ------ | ----- | --------
12
+ * a | LRU | add
13
+ * c | LRU | cache
14
+ * d | LRU | dispose
15
+ * h | LRU | head
16
+ * k | Node | key
17
+ * m | LRU | maxLength
18
+ * n | Node | next
19
+ * p | Node | previous
20
+ * r | LRU | raise (aka "hoist")
21
+ * t | LRU | tail
22
+ * v | Node | value
23
+ */
24
+ Object.defineProperty(exports, "__esModule", { value: true });
25
+ exports.LRU = void 0;
26
+ /**
27
+ * An tiny LRU cache with maximum count, identical weighting and no expiration.
28
+ */
29
+ class LRU {
30
+ length;
31
+ /** max length */
32
+ m;
33
+ /** head */
34
+ h;
35
+ /** tail */
36
+ t;
37
+ /** cache */
38
+ c;
39
+ /** dispose */
40
+ d;
41
+ constructor({ maxLength, dispose }) {
42
+ if (maxLength < 2) {
43
+ throw new Error("Max length must be >= 2");
44
+ }
45
+ if (parseInt(String(maxLength), 10) !== maxLength) {
46
+ throw new Error("Max length must be an integer");
47
+ }
48
+ this.length = 0;
49
+ this.m = maxLength;
50
+ this.h = null;
51
+ this.t = null;
52
+ this.c = new Map();
53
+ this.d = dispose || null;
54
+ this.reset();
55
+ }
56
+ reset() {
57
+ const values = this.c.values();
58
+ this.c.clear();
59
+ this.h = null;
60
+ this.t = null;
61
+ this.length = 0;
62
+ if (this.d) {
63
+ for (const hit of values) {
64
+ this.d(hit.k, hit.v);
65
+ }
66
+ }
67
+ }
68
+ get(key) {
69
+ const hit = this.c.get(key);
70
+ if (hit) {
71
+ this.r(hit);
72
+ return hit.v;
73
+ }
74
+ return undefined;
75
+ }
76
+ set(key, value) {
77
+ const hit = this.c.get(key);
78
+ if (hit) {
79
+ hit.v = value;
80
+ }
81
+ else {
82
+ const newHead = {
83
+ k: key,
84
+ v: value,
85
+ n: null,
86
+ p: null,
87
+ };
88
+ this.c.set(key, newHead);
89
+ this.a(newHead);
90
+ }
91
+ }
92
+ /** hoist (aka "raise") */
93
+ r(newHead) {
94
+ if (newHead === this.h) {
95
+ return;
96
+ }
97
+ if (!this.h) {
98
+ this.h = this.t = newHead;
99
+ return;
100
+ }
101
+ // Remove newHead from old position
102
+ newHead.p.n = newHead.n;
103
+ if (newHead.n) {
104
+ newHead.n.p = newHead.p;
105
+ }
106
+ else {
107
+ // It was the t, now newHead.prev is the t
108
+ this.t = newHead.p;
109
+ }
110
+ // Add newHead at top
111
+ newHead.n = this.h;
112
+ this.h.p = newHead;
113
+ this.h = newHead;
114
+ newHead.p = null;
115
+ }
116
+ /** add */
117
+ a(newHead) {
118
+ if (!this.h) {
119
+ this.h = this.t = newHead;
120
+ this.length = 1;
121
+ return;
122
+ }
123
+ this.h.p = newHead;
124
+ newHead.n = this.h;
125
+ this.h = newHead;
126
+ if (this.length === this.m) {
127
+ // Remove the t
128
+ const oldTail = this.t;
129
+ this.c.delete(oldTail.k);
130
+ this.t = oldTail.p;
131
+ this.t.n = null;
132
+ if (this.d) {
133
+ this.d(oldTail.k, oldTail.v);
134
+ }
135
+ }
136
+ else {
137
+ if (this.length === 0) {
138
+ this.t = newHead;
139
+ }
140
+ this.length++;
141
+ }
142
+ }
143
+ }
144
+ exports.LRU = LRU;
145
+ exports.default = LRU;
146
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;;;AAkBH;;GAEG;AACH,MAAa,GAAG;IACP,MAAM,CAAS;IACtB,iBAAiB;IACT,CAAC,CAAS;IAClB,WAAW;IACH,CAAC,CAAkC;IAC3C,WAAW;IACH,CAAC,CAAkC;IAC3C,YAAY;IACJ,CAAC,CAAyC;IAClD,cAAc;IACN,CAAC,CAAoD;IAE7D,YAAY,EAAE,SAAS,EAAE,OAAO,EAAkC;QAChE,IAAI,SAAS,GAAG,CAAC,EAAE;YACjB,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;SAC5C;QACD,IAAI,QAAQ,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,EAAE,CAAC,KAAK,SAAS,EAAE;YACjD,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC;SAClD;QAED,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;QAChB,IAAI,CAAC,CAAC,GAAG,SAAS,CAAC;QACnB,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC;QACd,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC;QACd,IAAI,CAAC,CAAC,GAAG,IAAI,GAAG,EAAE,CAAC;QACnB,IAAI,CAAC,CAAC,GAAG,OAAO,IAAI,IAAI,CAAC;QAEzB,IAAI,CAAC,KAAK,EAAE,CAAC;IACf,CAAC;IAE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AAC,CAAE,CAAC,CAAC,GAAG,IAAI,CAAC;YACjB,IAAI,IAAI,CAAC,CAAC,EAAE;gBACV,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;aAC9B;SACF;aAAM;YACL,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE;gBACrB,IAAI,CAAC,CAAC,GAAG,OAAO,CAAC;aAClB;YACD,IAAI,CAAC,MAAM,EAAE,CAAC;SACf;IACH,CAAC;CACF;AAvHD,kBAuHC;AACD,kBAAe,GAAG,CAAC"}
package/package.json CHANGED
@@ -1,40 +1,42 @@
1
1
  {
2
2
  "name": "@graphile/lru",
3
- "version": "4.11.0",
3
+ "version": "5.0.0-0.1",
4
4
  "description": "Extremely simple zero-dependencies ES6 LRU (you probably want lru-cache instead)",
5
- "main": "node8plus/index.js",
6
- "types": "node8plus/index.d.ts",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
7
  "scripts": {
8
- "test": "jest",
9
- "prepack": "mkdir -p node8plus && tsc",
10
- "watch": "mkdir -p node8plus && tsc --watch"
8
+ "test": "jest -i",
9
+ "prepack": "tsc -b"
11
10
  },
12
11
  "repository": {
13
12
  "type": "git",
14
- "url": "git+https://github.com/graphile/graphile-engine.git"
13
+ "url": "git+https://github.com/graphile/heart.git"
15
14
  },
16
15
  "keywords": [
17
- "lru"
16
+ "lru",
17
+ "graphile",
18
+ "graphite"
18
19
  ],
19
20
  "author": "Benjie Gillam <code@benjiegillam.com>",
20
21
  "license": "MIT",
21
22
  "bugs": {
22
- "url": "https://github.com/graphile/graphile-engine/issues"
23
+ "url": "https://github.com/graphile/heart/issues"
23
24
  },
24
- "homepage": "https://github.com/graphile/graphile-engine/tree/master/packages/lru",
25
+ "homepage": "https://github.com/graphile/heart/tree/main/utils/lru",
25
26
  "engines": {
26
- "node": ">=8.6"
27
- },
28
- "devDependencies": {
29
- "jest": "25.x",
30
- "ts-node": "^9.0.0",
31
- "typescript": "^4.0.2"
27
+ "node": ">=14.17"
32
28
  },
33
29
  "files": [
34
- "node8plus"
30
+ "dist"
35
31
  ],
36
32
  "dependencies": {
37
- "tslib": "^2.0.1"
33
+ "tslib": "^2.4.0"
38
34
  },
39
- "gitHead": "742133b9064cd64d61e45edc981261a036d56c7f"
40
- }
35
+ "devDependencies": {
36
+ "@types/jest": "^27.5.1",
37
+ "@types/node": "^16.11.15",
38
+ "jest": "^28.1.0",
39
+ "ts-node": "^10.9.1",
40
+ "typescript": "^4.8.1-rc"
41
+ }
42
+ }
@@ -1,22 +0,0 @@
1
- interface LRUOptions<KeyType, ValueType> {
2
- maxLength: number;
3
- dispose?: (key: KeyType, value: ValueType) => void;
4
- }
5
- /**
6
- * An tiny LRU cache with maximum count, identical weighting and no expiration.
7
- */
8
- export default class LRU<KeyType = any, ValueType = any> {
9
- length: number;
10
- private _maxLength;
11
- private _head;
12
- private _tail;
13
- private _cache;
14
- private _dispose;
15
- constructor({ maxLength, dispose }: LRUOptions<KeyType, ValueType>);
16
- reset(): void;
17
- get(key: KeyType): ValueType | undefined;
18
- set(key: KeyType, value: ValueType): void;
19
- private _hoist;
20
- private _add;
21
- }
22
- export {};
@@ -1,106 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- /**
4
- * An tiny LRU cache with maximum count, identical weighting and no expiration.
5
- */
6
- class LRU {
7
- constructor({ maxLength, dispose }) {
8
- if (maxLength < 2) {
9
- throw new Error("Max length must be >= 2");
10
- }
11
- if (parseInt(String(maxLength), 10) !== maxLength) {
12
- throw new Error("Max length must be an integer");
13
- }
14
- this._maxLength = maxLength;
15
- this._dispose = dispose || null;
16
- this._cache = new Map();
17
- this.reset();
18
- }
19
- reset() {
20
- const values = this._cache.values();
21
- this._cache.clear();
22
- this._head = null;
23
- this._tail = null;
24
- this.length = 0;
25
- if (this._dispose) {
26
- for (const hit of values) {
27
- this._dispose(hit.key, hit.value);
28
- }
29
- }
30
- }
31
- get(key) {
32
- const hit = this._cache.get(key);
33
- if (hit) {
34
- this._hoist(hit);
35
- return hit.value;
36
- }
37
- return undefined;
38
- }
39
- set(key, value) {
40
- const hit = this._cache.get(key);
41
- if (hit) {
42
- hit.value = value;
43
- }
44
- else {
45
- const newHead = {
46
- key,
47
- value,
48
- next: null,
49
- prev: null,
50
- };
51
- this._cache.set(key, newHead);
52
- this._add(newHead);
53
- }
54
- }
55
- _hoist(newHead) {
56
- if (newHead === this._head) {
57
- return;
58
- }
59
- if (!this._head) {
60
- this._head = this._tail = newHead;
61
- return;
62
- }
63
- // Remove newHead from old position
64
- newHead.prev.next = newHead.next;
65
- if (newHead.next) {
66
- newHead.next.prev = newHead.prev;
67
- }
68
- else {
69
- // It was the _tail, now newHead.prev is the _tail
70
- this._tail = newHead.prev;
71
- }
72
- // Add newHead at top
73
- newHead.next = this._head;
74
- this._head.prev = newHead;
75
- this._head = newHead;
76
- newHead.prev = null;
77
- }
78
- _add(newHead) {
79
- if (!this._head) {
80
- this._head = this._tail = newHead;
81
- this.length = 1;
82
- return;
83
- }
84
- this._head.prev = newHead;
85
- newHead.next = this._head;
86
- this._head = newHead;
87
- if (this.length === this._maxLength) {
88
- // Remove the _tail
89
- const oldTail = this._tail;
90
- this._cache.delete(oldTail.key);
91
- this._tail = oldTail.prev;
92
- this._tail.next = null;
93
- if (this._dispose) {
94
- this._dispose(oldTail.key, oldTail.value);
95
- }
96
- }
97
- else {
98
- if (this.length === 0) {
99
- this._tail = newHead;
100
- }
101
- this.length++;
102
- }
103
- }
104
- }
105
- exports.default = LRU;
106
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAYA;;GAEG;AACH,MAAqB,GAAG;IAQtB,YAAY,EAAE,SAAS,EAAE,OAAO,EAAkC;QAChE,IAAI,SAAS,GAAG,CAAC,EAAE;YACjB,MAAM,IAAI,KAAK,CAAC,yBAAy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