Skip to content

Commit 630fbd9

Browse files
committed
Use LinkedList in scope locals
1 parent 814260e commit 630fbd9

File tree

1 file changed

+14
-58
lines changed

1 file changed

+14
-58
lines changed

vm/src/scope.rs

Lines changed: 14 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
use std::collections::LinkedList;
12
use std::fmt;
2-
use std::rc::Rc;
33

44
use crate::obj::objdict::PyDictRef;
55
use crate::pyobject::{ItemProtocol, PyContext, PyObjectRef, PyResult};
@@ -9,58 +9,9 @@ use crate::vm::VirtualMachine;
99
* So a scope is a linked list of scopes.
1010
* When a name is looked up, it is check in its scope.
1111
*/
12-
#[derive(Debug)]
13-
struct RcListNode<T> {
14-
elem: T,
15-
next: Option<Rc<RcListNode<T>>>,
16-
}
17-
18-
#[derive(Debug, Clone)]
19-
struct RcList<T> {
20-
head: Option<Rc<RcListNode<T>>>,
21-
}
22-
23-
struct Iter<'a, T: 'a> {
24-
next: Option<&'a RcListNode<T>>,
25-
}
26-
27-
impl<T> RcList<T> {
28-
pub fn new() -> Self {
29-
RcList { head: None }
30-
}
31-
32-
pub fn insert(self, elem: T) -> Self {
33-
RcList {
34-
head: Some(Rc::new(RcListNode {
35-
elem,
36-
next: self.head,
37-
})),
38-
}
39-
}
40-
41-
#[cfg_attr(feature = "flame-it", flame("RcList"))]
42-
pub fn iter(&self) -> Iter<T> {
43-
Iter {
44-
next: self.head.as_ref().map(|node| &**node),
45-
}
46-
}
47-
}
48-
49-
impl<'a, T> Iterator for Iter<'a, T> {
50-
type Item = &'a T;
51-
52-
#[cfg_attr(feature = "flame-it", flame("Iter"))]
53-
fn next(&mut self) -> Option<Self::Item> {
54-
self.next.map(|node| {
55-
self.next = node.next.as_ref().map(|node| &**node);
56-
&node.elem
57-
})
58-
}
59-
}
60-
6112
#[derive(Clone)]
6213
pub struct Scope {
63-
locals: RcList<PyDictRef>,
14+
locals: LinkedList<PyDictRef>,
6415
pub globals: PyDictRef,
6516
}
6617

@@ -73,11 +24,14 @@ impl fmt::Debug for Scope {
7324

7425
impl Scope {
7526
pub fn new(locals: Option<PyDictRef>, globals: PyDictRef, vm: &VirtualMachine) -> Scope {
76-
let locals = match locals {
77-
Some(dict) => RcList::new().insert(dict),
78-
None => RcList::new(),
27+
let mut locals_list = LinkedList::new();
28+
if let Some(dict) = locals {
29+
locals_list.push_front(dict)
30+
}
31+
let scope = Scope {
32+
locals: locals_list,
33+
globals,
7934
};
80-
let scope = Scope { locals, globals };
8135
scope.store_name(vm, "__annotations__", vm.ctx.new_dict().into_object());
8236
scope
8337
}
@@ -97,19 +51,21 @@ impl Scope {
9751
}
9852

9953
pub fn get_locals(&self) -> PyDictRef {
100-
match self.locals.iter().next() {
54+
match self.locals.front() {
10155
Some(dict) => dict.clone(),
10256
None => self.globals.clone(),
10357
}
10458
}
10559

10660
pub fn get_only_locals(&self) -> Option<PyDictRef> {
107-
self.locals.iter().next().cloned()
61+
self.locals.front().cloned()
10862
}
10963

11064
pub fn new_child_scope_with_locals(&self, locals: PyDictRef) -> Scope {
65+
let mut new_locals = self.locals.clone();
66+
new_locals.push_front(locals);
11167
Scope {
112-
locals: self.locals.clone().insert(locals),
68+
locals: new_locals,
11369
globals: self.globals.clone(),
11470
}
11571
}

0 commit comments

Comments
 (0)