导致垃圾收集的JavaScript链接列表

我在
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()行并允许链表永远增长,游戏就会顺利运行.

动画不断被添加和删除.我在动画更新功能中添加了一些日志记录:

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.

不.滞后来自每个更新调用仅更新极少数动画的事实.

您的问题是旧的和着名的“迭代期间删除”问题.在您的情况下,它不会触发罕见的边缘情况错误,它只是停止迭代:

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.

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

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;
    });
}

(当你= = j时,你可以通过不重新分配来优化过滤器)

点赞