Skip to content

Commit

Permalink
fixup! fix(ses): avoid holding deep stacks strongly
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Mar 21, 2023
1 parent 9f408cb commit fd64167
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions packages/ses/src/error/note-log-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const spliceOut = cell => {
* bookkeeping cell has not been pushed off the end of the cache by `budget`
* number of more recently referenced cells. If the key is dropped by the user,
* the value will no longer be held by the cache, but the bookkeeping cell
* itself vill stay in memory until it has cycled off the end of the cache.
* itself will stay in memory.
*
* @template {{}} K
* @template {unknown} V
Expand All @@ -107,11 +107,12 @@ export const makeLRUCacheMap = budget => {
if (!isSafeInteger(budget) || budget < 0) {
throw new TypeError('budget must be a safe non-negative integer number');
}
/** @type {WeakMap<K, DoublyLinkedCell<WeakMap<K, V> | undefined>>} */
/** @typedef {DoublyLinkedCell<WeakMap<K, V> | undefined>} LRUCacheCell */
/** @type {WeakMap<K, LRUCacheCell>} */
const keyToCell = new WeakMap();
let size = 0; // `size` must remain <= `budget`
// As a sigil, `head` uniquely is not in the `keyToCell` map.
/** @type {DoublyLinkedCell<WeakMap<K, V> | undefined>} */
/** @type {LRUCacheCell>} */
const head = makeSelfCell(undefined);

const touchCell = key => {
Expand All @@ -136,7 +137,7 @@ export const makeLRUCacheMap = budget => {
*/
// TODO Change to the following line, once our tools don't choke on `?.`.
// See https://github.com/endojs/endo/issues/1514
// const get = key => touchCell(key)?.keyValue.get(key);
// const get = key => touchCell(key)?.data?.get(key);
const get = key => {
const cell = touchCell(key);
return cell && cell.data && cell.data.get(key);
Expand All @@ -149,7 +150,7 @@ export const makeLRUCacheMap = budget => {
*/
const set = (key, value) => {
if (budget >= 1) {
/** @type {DoublyLinkedCell<WeakMap<K, V> | undefined> | undefined} */
/** @type {LRUCacheCell | undefined} */
let cell = touchCell(key);
if (cell === undefined) {
if (size >= budget) {
Expand All @@ -159,13 +160,16 @@ export const makeLRUCacheMap = budget => {
size -= 1;
}
size += 1;
cell = makeSelfCell(new WeakMap());
keyToCell.set(key, cell);
cell = makeSelfCell(undefined);
spliceAfter(head, cell); // start most recently used
}
if (!cell.data) {
throw TypeError('Internal error; expected cell.data');
// Either a new cell or a condemned cell. Add its data.
cell.data = new WeakMap();
// Advertise the cell for this key.
keyToCell.set(key, cell);
}
// Update the data.
cell.data.set(key, value);
}
// eslint-disable-next-line no-use-before-define
Expand Down

0 comments on commit fd64167

Please sign in to comment.