JavaScript 链表导致垃圾回收

标签 javascript data-structures linked-list garbage-collection ecmascript-6

我在 JavaScript 中实现了以下链表数据结构:

class Node {
  constructor(data, list) {
    this.data = data;
    this.list = list;
    this.prev = null;
    this.next = null;
  }

  remove() {
    if (this.prev) {
      this.prev.next = this.next;
    } else {
      this.list.start = this.next;
    }

    if (this.next) {
      this.next.prev = this.prev;
    } else {
      this.list.end = this.prev;
    }

    this.next = null;
    this.prev = null;
    this.list.length -= 1;
  }
}

class LinkedList {
  constructor() {
    this.end = null;
    this.start = null;
    this.length = 0;
  }

  append(data) {
    const node = new Node(data, this);

    if (!this.start) {
      this.start = node;
    }

    if (this.end) {
      node.prev = this.end;
      this.end.next = node;
    }

    this.end = node;
    this.length += 1;

    return data;
  }

  remove() {
    if (this.end) {
      return this.end.remove();
    }
  }

  *[Symbol.iterator]() {
    let current = this.start;
    while (current) {
      yield current;
      current = current.next;
    }
  }
}

module.exports = LinkedList;

我像这样使用它来更新动画列表:

static update(timeDelta) {
  for (let node of this.animations) {
    const animation = node.data;

    if (animation.animating) {
      animation.update(timeDelta);
    } else {
      node.remove();
    }
  }
}

node.remove() 行导致我的游戏出现非常明显的延迟。我怀疑它正在触发垃圾收集。反常的是,如果我注释掉 node.remove() 行并允许链表永远增长,游戏就会顺利运行。

动画不断被添加和删除。我在动画 update 函数中添加了一些日志:

start iterating linked list
removing
ms elapsed:  0.45499999999992724
end iterating
start iterating linked list
removing
ms elapsed:  0.455000000000382
end iterating
start iterating linked list
removing
ms elapsed:  0.13000000000010914
end iterating
start iterating linked list
(13) updating
ms elapsed:  2.200000000000273
end iterating

您可以看到链表每秒迭代多次,偶尔会删除一个节点。

如何在不实际导致性能下降的情况下实现从我的列表中删除 O(1)?

最佳答案

The node.remove() line causes very noticeable lag in my game. I suspect it is triggering garbage collection.

没有。滞后是因为每个 update 调用只更新很少的动画。

您的问题是古老而著名的“迭代期间删除”问题。在您的情况下,它不会触发罕见的边缘情况错误,它只是停止迭代:

while (current) {
  yield current;
  // after yielding, in the `update` function, we have
  //    node = current
  // and most importantly
  //    node.remove()
  // which assigns (with `this` being `node`)
  //    this.next = null;
  // then the iteration is resumed
  current = current.next;
}

糟糕。简单的解决方法是在产生之前缓存要迭代的下一个节点:

let next = this.start;
while (next) {
  const current = next;
  next = current.next;
  yield current;
}

(或类似的东西),但是当删除下一个节点时它当然仍然失败。更好的方法可能是省略行

this.next = null;
this.prev = null;

来自节点的 remove 方法,以便引用在删除期间保持不变。这不会影响 GC。


另一种解决方案是完全删除链表 - 它设计过度,除非您经常在列表中间添加/删除节点在迭代之外。在迭代过程中过滤掉旧的动画很简单,它可以用一个好的旧的(内存效率高?)Array 来完成,甚至就地:

function filter(array, callback) {
    var i=0, j=0;
    while (j < array.length) {
        if (callback(array[j]))
            array[i++] = array[j++];
        else
            array[i] = array[j++];
    }
    array.length = i;
}
function update(timeDelta) {
    filter(animations, animation => {
        var keep = animation.animating;
        if (keep) animation.update(timeDelta);
        return keep;
    });
}

(您可以通过在 i===j 时不重新分配来优化 filter)

关于JavaScript 链表导致垃圾回收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39053035/

相关文章:

javascript - 符号 '&$checked'是什么意思

r - R中反转下三角矩阵的树列表

python - self 加入 Pandas

c# - 为什么 LinkedList 通常比 List 慢?

c - 链表的无限冒泡排序

c - 使链表的节点成为最后一个节点的功能?

Javascript 在 WebView 中不起作用 - Android?

javascript - Koa session : MySQL

javascript - Ascensor.js 不起作用?

algorithm - B+树中的非叶节点