Skip to content
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

pathUtils | fix memory leak #418

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
45 changes: 45 additions & 0 deletions lib/LRUCache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class LRUCache {
/**
* @param {number} maxSize maxSize
*/
constructor(maxSize) {
this._maxSize = maxSize;
/** @type {string[]} */
this._doublyQueue = [];
this._cacheMap = new Map();
}

/**
* @param {string} item item
*/
get(item) {
if (this._cacheMap.has(item)) {
const itemData = this._doublyQueue.splice(
this._doublyQueue.indexOf(item),
1
);
this._doublyQueue.unshift(item);

if (itemData.length > 0) {
this._cacheMap.set(item, itemData[0]);
}
}
}

/**
* @param {any} item item
* @param {any} itemData itemData
*/
set(item, itemData) {
if (this._doublyQueue.length === this._maxSize) {
const last = this._doublyQueue[this._doublyQueue.length - 1];
this._doublyQueue.pop();
this._cacheMap.delete(last);
}

this._doublyQueue.unshift(item);
this._cacheMap.set(item, itemData);
}
}

module.exports = LRUCache;
5 changes: 3 additions & 2 deletions lib/util/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"use strict";

const path = require("path");
const { LRUCache } = require("lru-cache");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use your implementation here and remove this package from here


const CHAR_HASH = "#".charCodeAt(0);
const CHAR_SLASH = "/".charCodeAt(0);
Expand Down Expand Up @@ -170,8 +171,8 @@ const join = (rootPath, request) => {
};
exports.join = join;

/** @type {Map<string, Map<string, string | undefined>>} */
const joinCache = new Map();
/** @type {LRUCache<string, Map<string, string | undefined>>} */
const joinCache = new LRUCache({ max: 500 });

/**
* @param {string} rootPath the root path
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"dependencies": {
"graceful-fs": "^4.2.4",
"lru-cache": "^10.2.2",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this big package here, you can rewrite this package and put it inside project, because we use only the max value

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'll implement it and update

"tapable": "^2.2.0"
},
"license": "MIT",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3063,6 +3063,11 @@ log-update@^4.0.0:
slice-ansi "^4.0.0"
wrap-ansi "^6.2.0"

lru-cache@^10.2.2:
version "10.2.2"
resolved "https://registry.npmjs.org/lru-cache/-/lru-cache-10.2.2.tgz#48206bc114c1252940c41b25b41af5b545aca878"
integrity sha512-9hp3Vp2/hFQUiIwKo8XCeFVnrg8Pk3TYNPIR7tJADKi5YfcF7vEaK7avFHTlSy3kOKYaJQaalfEo6YuXdceBOQ==

lru-cache@^5.1.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-5.1.1.tgz#1da27e6710271947695daf6848e847f01d84b920"
Expand Down