FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Replace GC tracking HashSet with intrusive linked list by youknowone · Pull Request #7328 · RustPython/RustPython · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .rs  (4) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
35 changes: 33 additions & 2 deletions crates/common/src/linked_list.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ impl<L: Link> LinkedList<L, L::Target> {
let ptr = L::as_raw(&val);
assert_ne!(self.head, Some(ptr));
unsafe {
// Verify the node is not already in a list (pointers must be clean)
debug_assert!(
L::pointers(ptr).as_ref().get_prev().is_none(),
"push_front: node already has prev pointer (double-insert?)"
);
debug_assert!(
L::pointers(ptr).as_ref().get_next().is_none(),
"push_front: node already has next pointer (double-insert?)"
);
L::pointers(ptr).as_mut().set_next(self.head);
L::pointers(ptr).as_mut().set_prev(None);

Expand Down Expand Up @@ -192,6 +201,20 @@ impl<L: Link> LinkedList<L, L::Target> {
// }
// }

/// Removes the first element from the list and returns it, or None if empty.
pub fn pop_front(&mut self) -> Option<L::Handle> {
let head = self.head?;
unsafe {
self.head = L::pointers(head).as_ref().get_next();
if let Some(new_head) = self.head {
L::pointers(new_head).as_mut().set_prev(None);
}
L::pointers(head).as_mut().set_next(None);
L::pointers(head).as_mut().set_prev(None);
Some(L::from_raw(head))
}
}

/// Returns whether the linked list does not contain any node
pub const fn is_empty(&self) -> bool {
self.head.is_none()
Expand All @@ -212,7 +235,11 @@ impl<L: Link> LinkedList<L, L::Target> {
pub unsafe fn remove(&mut self, node: NonNull<L::Target>) -> Option<L::Handle> {
unsafe {
if let Some(prev) = L::pointers(node).as_ref().get_prev() {
debug_assert_eq!(L::pointers(prev).as_ref().get_next(), Some(node));
debug_assert_eq!(
L::pointers(prev).as_ref().get_next(),
Some(node),
"linked list corruption: prev->next != node (prev={prev:p}, node={node:p})"
);
L::pointers(prev)
.as_mut()
.set_next(L::pointers(node).as_ref().get_next());
Expand All @@ -225,7 +252,11 @@ impl<L: Link> LinkedList<L, L::Target> {
}

if let Some(next) = L::pointers(node).as_ref().get_next() {
debug_assert_eq!(L::pointers(next).as_ref().get_prev(), Some(node));
debug_assert_eq!(
L::pointers(next).as_ref().get_prev(),
Some(node),
"linked list corruption: next->prev != node (next={next:p}, node={node:p})"
);
L::pointers(next)
.as_mut()
.set_prev(L::pointers(node).as_ref().get_prev());
Expand Down
Loading
Loading

Back | FazBrowse Home | New Git URL