1
1
use std:: {
2
- cell:: RefCell ,
3
2
fmt,
4
3
hash:: { Hash , Hasher } ,
5
4
iter, mem, ptr,
@@ -15,12 +14,6 @@ use crate::{
15
14
#[ derive( Debug , Clone ) ]
16
15
pub struct SyntaxNode ( Rc < NodeData > ) ;
17
16
18
- impl Drop for SyntaxNode {
19
- fn drop ( & mut self ) {
20
- NodeData :: delete ( & mut self . 0 )
21
- }
22
- }
23
-
24
17
// Identity semantics for hash & eq
25
18
impl PartialEq for SyntaxNode {
26
19
fn eq ( & self , other : & SyntaxNode ) -> bool {
@@ -89,7 +82,6 @@ impl fmt::Display for SyntaxElement {
89
82
enum Kind {
90
83
Root ( GreenNode ) ,
91
84
Child { parent : SyntaxNode , index : u32 , offset : TextSize } ,
92
- Free { next_free : Option < Rc < NodeData > > } ,
93
85
}
94
86
95
87
impl Kind {
@@ -107,77 +99,9 @@ struct NodeData {
107
99
green : ptr:: NonNull < GreenNode > ,
108
100
}
109
101
110
- struct FreeList {
111
- first_free : Option < Rc < NodeData > > ,
112
- len : usize ,
113
- }
114
-
115
- const FREE_LIST_LEN : usize = 128 ;
116
-
117
- impl FreeList {
118
- fn new ( ) -> FreeList {
119
- let mut res = FreeList { first_free : None , len : 0 } ;
120
- for _ in 0 ..FREE_LIST_LEN {
121
- res. try_push ( & mut Rc :: new ( NodeData {
122
- kind : Kind :: Free { next_free : None } ,
123
- green : ptr:: NonNull :: dangling ( ) ,
124
- } ) )
125
- }
126
- res
127
- }
128
-
129
- fn with < T , F : FnOnce ( & mut FreeList ) -> T > ( f : F ) -> T {
130
- thread_local ! {
131
- static INSTANCE : RefCell <FreeList > = RefCell :: new( FreeList :: new( ) ) ;
132
- }
133
- INSTANCE . with ( |it| f ( & mut * it. borrow_mut ( ) ) )
134
- }
135
-
136
- fn pop ( & mut self ) -> Option < Rc < NodeData > > {
137
- let mut node = self . first_free . take ( ) ?;
138
- self . len -= 1 ;
139
- {
140
- let node = Rc :: get_mut ( & mut node) . unwrap ( ) ;
141
- self . first_free = match & mut node. kind {
142
- Kind :: Free { next_free } => next_free. take ( ) ,
143
- _ => unreachable ! ( ) ,
144
- }
145
- }
146
- Some ( node)
147
- }
148
-
149
- fn try_push ( & mut self , node : & mut Rc < NodeData > ) {
150
- if self . len >= FREE_LIST_LEN {
151
- return ;
152
- }
153
- Rc :: get_mut ( node) . unwrap ( ) . kind = Kind :: Free { next_free : self . first_free . take ( ) } ;
154
- self . first_free = Some ( Rc :: clone ( node) ) ;
155
- self . len += 1 ;
156
- }
157
- }
158
-
159
102
impl NodeData {
160
103
fn new ( kind : Kind , green : ptr:: NonNull < GreenNode > ) -> Rc < NodeData > {
161
- let mut node = FreeList :: with ( |it| it. pop ( ) ) . unwrap_or_else ( || {
162
- Rc :: new ( NodeData {
163
- kind : Kind :: Free { next_free : None } ,
164
- green : ptr:: NonNull :: dangling ( ) ,
165
- } )
166
- } ) ;
167
-
168
- {
169
- let node = Rc :: get_mut ( & mut node) . unwrap ( ) ;
170
- node. kind = kind;
171
- node. green = green;
172
- }
173
- node
174
- }
175
- fn delete ( this : & mut Rc < NodeData > ) {
176
- if let Some ( this_mut) = Rc :: get_mut ( this) {
177
- // NB: this might drop SyntaxNodes
178
- this_mut. kind = Kind :: Free { next_free : None } ;
179
- FreeList :: with ( |it| it. try_push ( this) )
180
- }
104
+ Rc :: new ( NodeData { kind, green } )
181
105
}
182
106
}
183
107
@@ -244,7 +168,6 @@ impl SyntaxNode {
244
168
match & self . 0 . kind {
245
169
Kind :: Root ( _) => None ,
246
170
Kind :: Child { parent, .. } => Some ( parent. clone ( ) ) ,
247
- Kind :: Free { .. } => unreachable ! ( ) ,
248
171
}
249
172
}
250
173
0 commit comments