Skip to content

Commit da1ecae

Browse files
committed
Speedup and simplify
Benchmarks show that our thead-local cache is harmful. I think there might be a much better way to implement it, but let's start with a simple baseline.
1 parent 7a86fe3 commit da1ecae

File tree

2 files changed

+2
-79
lines changed

2 files changed

+2
-79
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rowan"
3-
version = "0.10.1"
3+
version = "0.10.2"
44
authors = ["Aleksey Kladov <[email protected]>"]
55
repository = "https://github.com/rust-analyzer/rowan"
66
license = "MIT OR Apache-2.0"

src/cursor.rs

Lines changed: 1 addition & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::{
2-
cell::RefCell,
32
fmt,
43
hash::{Hash, Hasher},
54
iter, mem, ptr,
@@ -15,12 +14,6 @@ use crate::{
1514
#[derive(Debug, Clone)]
1615
pub struct SyntaxNode(Rc<NodeData>);
1716

18-
impl Drop for SyntaxNode {
19-
fn drop(&mut self) {
20-
NodeData::delete(&mut self.0)
21-
}
22-
}
23-
2417
// Identity semantics for hash & eq
2518
impl PartialEq for SyntaxNode {
2619
fn eq(&self, other: &SyntaxNode) -> bool {
@@ -89,7 +82,6 @@ impl fmt::Display for SyntaxElement {
8982
enum Kind {
9083
Root(GreenNode),
9184
Child { parent: SyntaxNode, index: u32, offset: TextSize },
92-
Free { next_free: Option<Rc<NodeData>> },
9385
}
9486

9587
impl Kind {
@@ -107,77 +99,9 @@ struct NodeData {
10799
green: ptr::NonNull<GreenNode>,
108100
}
109101

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-
159102
impl NodeData {
160103
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 })
181105
}
182106
}
183107

@@ -244,7 +168,6 @@ impl SyntaxNode {
244168
match &self.0.kind {
245169
Kind::Root(_) => None,
246170
Kind::Child { parent, .. } => Some(parent.clone()),
247-
Kind::Free { .. } => unreachable!(),
248171
}
249172
}
250173

0 commit comments

Comments
 (0)