我正在JDK 11中阅读LinkedHashMap源代码,发现一段死代码(我不确定(
众所周知,LinkedHashMap使用双链表来保留所有元素的顺序。它有一个名为accessOrder
的成员
final boolean accessOrder;
默认情况下,它是false,但如果设置为true,则每次运行get
时,它都会将获得的元素移动到链表的末尾。这就是函数afterNodeAccess
的作用
//if accessOrder were set as true, after you visit node e, if e is not the end node of the linked list,
//it will move the node to the end of the linkedlist.
void afterNodeAccess(Node<K, V> e) {
LinkedHashMap.Entry<K, V> last;
if(accessOrder && (last = tail) != e) {
//if enter `if` ,it indicates that e is not the end of the linked list, because (last=tail!=e)
//then `a` as the after node of p(p is e after casting to LinkedHashMap.Entry) is never gonna be null. Only if p is last node of the linked list then a will be null.
LinkedHashMap.Entry<K, V> p = (LinkedHashMap.Entry<K, V>) e, b = p.before, a = p.after;
p.after = null;
if(b == null) {
head = a;
} else {
b.after = a;
}
// Is the if else clasue redundant? `a` must not be null.. the else clase will never be excuted.
if(a != null) {
a.before = b;
} else {
last = b;
}
if(last == null) {
head = p;
} else {
p.before = last;
last.after = p;
}
tail = p;
++modCount;
}
}
所以我的问题来了:
(accessOrder && (last = tail) != e
意味着e不是链表的最后一个节点。如果e已经是最后一个节点,我们就不必做任何正确的事情了?
然后a
作为p的后节点(p在转换为LinkedHashMap.Entry后为e(,它不能为null。只有当p
是最后一个节点时,a
才能为null。
那么下面的代码有什么意义呢?
// Is the if else clasue redundant? `a` must not be null.. the else clase will never be excuted.
if(a != null) {
a.before = b;
} else {
last = b;
}
a
始终为!= null
,则永远不会执行else子句last = b
。。。。那么它是死代码吗?
此外,我还做了一个实验,将accessorder
设置为true
,然后我将get
作为调试模式中的最后一个节点,似乎我永远无法进入上述其他使用last = b
的情况
有什么建议吗?
OP中提供的代码是用于单个链表的节点移除算法,该算法将移除的节点设置为列表的尾部(重新定位到尾部(:
LinkedHashMap.Entry<K, V> current = (LinkedHashMap.Entry<K, V>) e
LinkedHashMap.Entry<K, V> pred = current.before, succ = current.after;
current.after = null;
// position the successor of the removed node correctly
// (either as the head of the list or as the successor of the node BEFORE the removed node)
if(pred == null) {
head = succ;
} else {
pred.after = succ ;
}
// position the predecessor of the removed node correctly
// (either as the tail of the list or as the predecessor of the node AFTER the removed node)
if(succ != null) {
succ.before = pred;
} else { // unreachable for non tail nodes
last = pred;
}
// final step - if the predecessor of the removed node was null then the head
// of the list is the removed node (the list contains a single node).
// otherwise update the removed node as the tail of the list -
// its predecessor will be the previous tail of the list
if(last == null) { // unreachable for non tail nodes
head = current;
} else {
current.before = last;
last.after = current;
}
tail = current;
该算法处理给定节点的所有可能情况,该节点应重新定位为链接情况的尾部。
在afterNodeAccess
方法的上下文中,在一般情况下的算法中将存在一些冗余,因为由于(last = tail) != e
,重新定位的节点从不在列表的尾部。因此,一种更有效的算法是:
current.after = null;
// position the successor of the removed node correctly
// (either as the head of the list or as the successor of the node BEFORE the removed node)
if(pred == null) {
head = succ;
} else {
pred.after = succ ;
}
// position the predecessor of the removed node correctly
// (as the predecessor of the node AFTER the removed node)
// update the removed node as the tail of the list -
// its predecessor will be the previous tail of the list
succ.before = pred;
current.before = last;
last.after = current;
tail = current;
正如霍尔格在评论中提到的那样,这是一个经典的"复制粘贴"解决方案,IMHO表明,在某些情况下重用代码似乎效率低下且不明确。
正如Johannes Kuhn所建议的,您可以考虑向OpenJDK社区提交无法访问的代码的修复程序。请参阅有关如何做到这一点的参考资料。
参考文献:
- AdoptOpen JDK 11的LinkedHashMap源代码
- 如何为OpenJDK提供修复程序