aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-09 00:41:31 +0100
committerAleksey Kladov <[email protected]>2018-08-09 00:41:31 +0100
commit34bf0a0b180a4c56cf43c34080a074e6e7537e18 (patch)
treeaf9b8ad059be1d1b41f1f9ac121b01b928a7af78 /src
parent08475a690ccc26ab5fd5e809a8cd3a19fcf5aff4 (diff)
More premature layout micro optimizations
Diffstat (limited to 'src')
-rw-r--r--src/yellow/green.rs49
1 files changed, 12 insertions, 37 deletions
diff --git a/src/yellow/green.rs b/src/yellow/green.rs
index 2d19c252b..787968363 100644
--- a/src/yellow/green.rs
+++ b/src/yellow/green.rs
@@ -6,13 +6,16 @@ use {
6 6
7#[derive(Clone, Debug)] 7#[derive(Clone, Debug)]
8pub(crate) enum GreenNode { 8pub(crate) enum GreenNode {
9 Leaf(GreenLeaf), 9 Leaf {
10 kind: SyntaxKind,
11 text: SmolStr,
12 },
10 Branch(Arc<GreenBranch>), 13 Branch(Arc<GreenBranch>),
11} 14}
12 15
13impl GreenNode { 16impl GreenNode {
14 pub(crate) fn new_leaf(kind: SyntaxKind, text: &str) -> GreenNode { 17 pub(crate) fn new_leaf(kind: SyntaxKind, text: &str) -> GreenNode {
15 GreenNode::Leaf(GreenLeaf::new(kind, text)) 18 GreenNode::Leaf { kind, text: SmolStr::new(text) }
16 } 19 }
17 20
18 pub(crate) fn new_branch(kind: SyntaxKind, children: Box<[GreenNode]>) -> GreenNode { 21 pub(crate) fn new_branch(kind: SyntaxKind, children: Box<[GreenNode]>) -> GreenNode {
@@ -21,21 +24,21 @@ impl GreenNode {
21 24
22 pub fn kind(&self) -> SyntaxKind { 25 pub fn kind(&self) -> SyntaxKind {
23 match self { 26 match self {
24 GreenNode::Leaf(l) => l.kind(), 27 GreenNode::Leaf { kind, .. } => *kind,
25 GreenNode::Branch(b) => b.kind(), 28 GreenNode::Branch(b) => b.kind(),
26 } 29 }
27 } 30 }
28 31
29 pub fn text_len(&self) -> TextUnit { 32 pub fn text_len(&self) -> TextUnit {
30 match self { 33 match self {
31 GreenNode::Leaf(l) => l.text_len(), 34 GreenNode::Leaf { text, ..} => TextUnit::of_str(text.as_str()),
32 GreenNode::Branch(b) => b.text_len(), 35 GreenNode::Branch(b) => b.text_len(),
33 } 36 }
34 } 37 }
35 38
36 pub fn children(&self) -> &[GreenNode] { 39 pub fn children(&self) -> &[GreenNode] {
37 match self { 40 match self {
38 GreenNode::Leaf(_) => &[], 41 GreenNode::Leaf { .. } => &[],
39 GreenNode::Branch(b) => b.children(), 42 GreenNode::Branch(b) => b.children(),
40 } 43 }
41 } 44 }
@@ -46,7 +49,7 @@ impl GreenNode {
46 return buff; 49 return buff;
47 fn go(node: &GreenNode, buff: &mut String) { 50 fn go(node: &GreenNode, buff: &mut String) {
48 match node { 51 match node {
49 GreenNode::Leaf(l) => buff.push_str(&l.text()), 52 GreenNode::Leaf { text, .. } => buff.push_str(text.as_str()),
50 GreenNode::Branch(b) => b.children().iter().for_each(|child| go(child, buff)), 53 GreenNode::Branch(b) => b.children().iter().for_each(|child| go(child, buff)),
51 } 54 }
52 } 55 }
@@ -89,38 +92,10 @@ impl GreenBranch {
89 } 92 }
90} 93}
91 94
92#[derive(Clone, Debug)]
93pub(crate) struct GreenLeaf {
94 kind: SyntaxKind,
95 text: SmolStr,
96}
97
98impl GreenLeaf {
99 fn new(kind: SyntaxKind, text: &str) -> Self {
100 let text = SmolStr::new(text);
101 GreenLeaf { kind, text }
102 }
103
104 pub(crate) fn kind(&self) -> SyntaxKind {
105 self.kind
106 }
107
108 pub(crate) fn text(&self) -> &str {
109 self.text.as_str()
110 }
111
112 pub(crate) fn text_len(&self) -> TextUnit {
113 TextUnit::of_str(self.text())
114 }
115}
116
117
118#[test] 95#[test]
119fn test_sizes() { 96fn test_sizes() {
120 use std::mem::size_of; 97 use std::mem::size_of;
121 98 println!("GreenBranch = {}", size_of::<GreenBranch>());
122 println!("GreenNode = {}", size_of::<GreenNode>()); 99 println!("GreenNode = {}", size_of::<GreenNode>());
123 println!("GreenLeaf = {}", size_of::<GreenLeaf>()); 100 println!("SmolStr = {}", size_of::<SmolStr>());
124 println!("SyntaxKind = {}", size_of::<SyntaxKind>());
125 println!("SmolStr = {}", size_of::<SmolStr>());
126} 101}