Skip to content

Commit 8fb25dc

Browse files
dabekfacebook-github-bot
authored andcommitted
Add boxes to workaround LLVM bug (?) and improve compile time
Summary: Today in "sacrificing readability / safety due to Rust inefficiencies": I believe some of our compile times are due to (less severe than in example there) version of this bug: rust-lang/rust#66617 Adding box in a key place happens to circumvent it. Reviewed By: shiqicao Differential Revision: D18772104 fbshipit-source-id: a94271b208894aecd9bb8ee1f35110b3c0b09961
1 parent 4251e29 commit 8fb25dc

File tree

2 files changed

+46
-19
lines changed

2 files changed

+46
-19
lines changed

hphp/hack/src/parser/positioned_syntax.rs

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@ use crate::{
1313
use ocamlrep::rc::RcOc;
1414
use oxidized::pos::Pos;
1515

16+
#[derive(Debug, Clone)]
17+
pub struct Span {
18+
pub left: PositionedToken,
19+
pub right: PositionedToken,
20+
}
21+
1622
#[derive(Debug, Clone)]
1723
pub enum PositionedValue {
1824
/// value for a token node is token itself
1925
TokenValue(PositionedToken),
2026
/// value for a range denoted by pair of tokens
21-
TokenSpan {
22-
left: PositionedToken,
23-
right: PositionedToken,
24-
},
27+
TokenSpan(Box<Span>),
2528
Missing {
2629
offset: usize,
2730
},
@@ -31,17 +34,19 @@ impl PositionedValue {
3134
pub fn width(&self) -> usize {
3235
match self {
3336
PositionedValue::TokenValue(t) => t.width(),
34-
PositionedValue::TokenSpan { left, right } => {
35-
(right.end_offset() - left.start_offset()) + 1
36-
}
37+
PositionedValue::TokenSpan(x) => (x.right.end_offset() - x.left.start_offset()) + 1,
3738
PositionedValue::Missing { .. } => 0,
3839
}
3940
}
4041

4142
fn start_offset(&self) -> usize {
4243
use PositionedValue::*;
4344
match &self {
44-
TokenValue(t) | TokenSpan { left: t, .. } => t
45+
TokenValue(t) => t
46+
.leading_start_offset()
47+
.expect("invariant violation for Positioned Syntax"),
48+
TokenSpan(x) => x
49+
.left
4550
.leading_start_offset()
4651
.expect("invariant violation for Positioned Syntax"),
4752
Missing { offset, .. } => *offset,
@@ -51,33 +56,55 @@ impl PositionedValue {
5156
fn leading_width(&self) -> usize {
5257
use PositionedValue::*;
5358
match self {
54-
TokenValue(t) | TokenSpan { left: t, .. } => t.leading_width(),
59+
TokenValue(t) => t.leading_width(),
60+
TokenSpan(x) => x.left.leading_width(),
5561
Missing { .. } => 0,
5662
}
5763
}
5864

5965
fn trailing_width(&self) -> usize {
6066
use PositionedValue::*;
6167
match self {
62-
TokenValue(t) | TokenSpan { right: t, .. } => t.trailing_width(),
68+
TokenValue(t) => t.trailing_width(),
69+
TokenSpan(x) => x.right.trailing_width(),
6370
Missing { .. } => 0,
6471
}
6572
}
6673

74+
fn leading_token(&self) -> Option<&PositionedToken> {
75+
use PositionedValue::*;
76+
match self {
77+
TokenValue(l) => Some(&l),
78+
TokenSpan(x) => Some(&x.left),
79+
_ => None,
80+
}
81+
}
82+
83+
fn trailing_token(&self) -> Option<&PositionedToken> {
84+
use PositionedValue::*;
85+
match self {
86+
TokenValue(r) => Some(&r),
87+
TokenSpan(x) => Some(&x.right),
88+
_ => None,
89+
}
90+
}
91+
6792
fn value_from_outer_children(first: &Self, last: &Self) -> Self {
6893
use PositionedValue::*;
6994
match (first, last) {
70-
(TokenValue(l), TokenValue(r))
71-
| (TokenSpan { left: l, .. }, TokenValue(r))
72-
| (TokenValue(l), TokenSpan { right: r, .. })
73-
| (TokenSpan { left: l, .. }, TokenSpan { right: r, .. }) => {
95+
(TokenValue(_), TokenValue(_))
96+
| (TokenSpan(_), TokenValue(_))
97+
| (TokenValue(_), TokenSpan(_))
98+
| (TokenSpan(_), TokenSpan(_)) => {
99+
let l = first.leading_token().unwrap();
100+
let r = last.trailing_token().unwrap();
74101
if RcOc::ptr_eq(&l, &r) {
75102
TokenValue(RcOc::clone(&l))
76103
} else {
77-
TokenSpan {
104+
TokenSpan(Box::new(Span {
78105
left: RcOc::clone(&l),
79106
right: RcOc::clone(&r),
80-
}
107+
}))
81108
}
82109
}
83110
// can have two missing nodes if first and last child nodes of

hphp/hack/src/parser/rust_to_ocaml.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,9 @@ impl ToOcaml for PositionedValue {
247247
caml_set_field(block, 0, token);
248248
block
249249
}
250-
PositionedValue::TokenSpan { left, right } => {
251-
let left = left.to_ocaml(context);
252-
let right = right.to_ocaml(context);
250+
PositionedValue::TokenSpan(x) => {
251+
let left = x.left.to_ocaml(context);
252+
let right = x.right.to_ocaml(context);
253253
// TokenSpan { left: Token.t; right: Token.t }
254254
let block = reserve_block(TOKEN_SPAN_VARIANT.into(), 2);
255255
caml_set_field(block, 0, left);

0 commit comments

Comments
 (0)