Skip to content

Commit 27c66ce

Browse files
committed
perf: use try_dispatch to categorize the statement_body, reducing the number of branches and stack usage (otherwise, stack overflow is extremely likely).
1 parent e3911c4 commit 27c66ce

File tree

6 files changed

+306
-267
lines changed

6 files changed

+306
-267
lines changed

โ€Žsrc/query/ast/benches/bench.rsโ€Ž

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ fn main() {
1818

1919
// bench fastest โ”‚ slowest โ”‚ median โ”‚ mean โ”‚ samples โ”‚ iters
2020
// โ•ฐโ”€ dummy โ”‚ โ”‚ โ”‚ โ”‚ โ”‚
21-
// โ”œโ”€ deep_function_call 132.5 ยตs โ”‚ 470.9 ยตs โ”‚ 162.3 ยตs โ”‚ 166.2 ยตs โ”‚ 100 โ”‚ 100
22-
// โ”œโ”€ deep_query 232.3 ยตs โ”‚ 495 ยตs โ”‚ 283.6 ยตs โ”‚ 283.4 ยตs โ”‚ 100 โ”‚ 100
23-
// โ”œโ”€ large_query 196.4 ยตs โ”‚ 348.2 ยตs โ”‚ 240.1 ยตs โ”‚ 240.1 ยตs โ”‚ 100 โ”‚ 100
24-
// โ”œโ”€ large_statement 197.6 ยตs โ”‚ 286.6 ยตs โ”‚ 234.6 ยตs โ”‚ 236.1 ยตs โ”‚ 100 โ”‚ 100
25-
// โ”œโ”€ wide_embedding 3.964 ms โ”‚ 7.229 ms โ”‚ 4.3 ms โ”‚ 4.397 ms โ”‚ 100 โ”‚ 100
26-
// โ•ฐโ”€ wide_expr 40.59 ยตs โ”‚ 56.32 ยตs โ”‚ 43.06 ยตs โ”‚ 43.68 ยตs โ”‚ 100 โ”‚ 100
21+
// โ”œโ”€ deep_function_call 117.2 ยตs โ”‚ 407.3 ยตs โ”‚ 127.5 ยตs โ”‚ 132.2 ยตs โ”‚ 100 โ”‚ 100
22+
// โ”œโ”€ deep_query 229.7 ยตs โ”‚ 395.8 ยตs โ”‚ 242.6 ยตs โ”‚ 244.6 ยตs โ”‚ 100 โ”‚ 100
23+
// โ”œโ”€ large_query 203.8 ยตs โ”‚ 275.7 ยตs โ”‚ 210.9 ยตs โ”‚ 212.8 ยตs โ”‚ 100 โ”‚ 100
24+
// โ”œโ”€ large_statement 206.7 ยตs โ”‚ 238.3 ยตs โ”‚ 212.9 ยตs โ”‚ 213.7 ยตs โ”‚ 100 โ”‚ 100
25+
// โ”œโ”€ wide_embedding 3.628 ms โ”‚ 4.088 ms โ”‚ 3.761 ms โ”‚ 3.762 ms โ”‚ 100 โ”‚ 100
26+
// โ•ฐโ”€ wide_expr 39.76 ยตs โ”‚ 46.04 ยตs โ”‚ 40.18 ยตs โ”‚ 40.43 ยตs โ”‚ 100 โ”‚ 100
2727

2828
#[divan::bench_group(max_time = 0.5)]
2929
mod dummy {

โ€Žsrc/query/ast/src/parser/expr.rsโ€Ž

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,6 @@ macro_rules! with_span {
4040
};
4141
}
4242

43-
macro_rules! try_dispatch {
44-
($input:expr, $($pat:pat => $body:expr),+ $(,)?) => {{
45-
if let Some(token_0) = $input.tokens.first() {
46-
use TokenKind::*;
47-
48-
if let Some(result) = match token_0.kind {
49-
$($pat => Some($body),)+
50-
_ => None,
51-
} {
52-
if result.is_ok() {
53-
return result;
54-
}
55-
}
56-
}
57-
}};
58-
}
59-
6043
pub fn expr(i: Input) -> IResult<Expr> {
6144
context("expression", subexpr(0)).parse(i)
6245
}
@@ -1562,7 +1545,7 @@ pub fn expr_element(i: Input) -> IResult<WithSpan<ExprElement>> {
15621545
ExprElement::StageLocation { location }
15631546
});
15641547

1565-
try_dispatch!(i,
1548+
try_dispatch!(i, true,
15661549
IS => with_span!(rule!(#is_null | #is_distinct_from)).parse(i),
15671550
NOT => with_span!(rule!(
15681551
#in_list
@@ -1828,7 +1811,7 @@ pub fn literal(i: Input) -> IResult<Literal> {
18281811
|token| parse_float(token.text()).map_err(nom::Err::Failure),
18291812
);
18301813

1831-
try_dispatch!(i,
1814+
try_dispatch!(i, true,
18321815
LiteralString => string.parse(i),
18331816
LiteralCodeString => code_string.parse(i),
18341817
LiteralInteger => decimal_uint.parse(i),

โ€Žsrc/query/ast/src/parser/mod.rsโ€Ž

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
macro_rules! try_dispatch {
16+
($input:expr, $return_if_ok:literal, $($pat:pat => $body:expr),+ $(,)?) => {{
17+
if let Some(token_0) = $input.tokens.first() {
18+
use TokenKind::*;
19+
20+
if let Some(result) = match token_0.kind {
21+
$($pat => Some($body),)+
22+
_ => None,
23+
} {
24+
if !$return_if_ok || result.is_ok() {
25+
return result;
26+
}
27+
}
28+
}
29+
}};
30+
}
31+
1532
mod comment;
1633
mod common;
1734
mod copy;

0 commit comments

Comments
ย (0)