From 6bab5044fec5a1be3856f18ccacccd837f1af869 Mon Sep 17 00:00:00 2001 From: h-a-n-a Date: Mon, 13 Feb 2023 00:49:05 +0800 Subject: [PATCH 1/4] perf: skip visiting if it's out of range --- .../turbopack-ecmascript/src/path_visitor.rs | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/crates/turbopack-ecmascript/src/path_visitor.rs b/crates/turbopack-ecmascript/src/path_visitor.rs index 54a2b1d446a3a..bc4c583e05915 100644 --- a/crates/turbopack-ecmascript/src/path_visitor.rs +++ b/crates/turbopack-ecmascript/src/path_visitor.rs @@ -29,15 +29,25 @@ fn find_range<'a, 'b>( index: usize, ) -> Option<&'b [(&'a AstPath, &'a dyn VisitorFactory)]> { // Precondition: visitors is never empty - let start = if visitors.first().unwrap().0[index] >= *kind { - // Fast path: It's likely that the whole range is selected - 0 - } else { - visitors.partition_point(|(path, _)| path[index] < *kind) - }; + let first_visitor = visitors.first().unwrap(); + + if first_visitor.0[index] > *kind { + // Fast path: If ast path of the first visitor is already out of range, then we + // can skip the whole visit. + return None; + } + + let start = visitors.partition_point(|(path, _)| path[index] < *kind); if start >= visitors.len() { return None; } + + if visitors[start].0[index] > *kind { + // Fast path: If the starting point is greater than the given kind, it's + // meaningless to visit later. + return None; + } + let end = if visitors.last().unwrap().0[index] == *kind { // Fast path: It's likely that the whole range is selected visitors.len() From 87abfffa30221271e02ca026fd511b6869f45b4b Mon Sep 17 00:00:00 2001 From: h-a-n-a Date: Mon, 13 Feb 2023 01:10:05 +0800 Subject: [PATCH 2/4] perf: update --- crates/turbopack-ecmascript/src/path_visitor.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/turbopack-ecmascript/src/path_visitor.rs b/crates/turbopack-ecmascript/src/path_visitor.rs index bc4c583e05915..e347bb9905047 100644 --- a/crates/turbopack-ecmascript/src/path_visitor.rs +++ b/crates/turbopack-ecmascript/src/path_visitor.rs @@ -29,9 +29,7 @@ fn find_range<'a, 'b>( index: usize, ) -> Option<&'b [(&'a AstPath, &'a dyn VisitorFactory)]> { // Precondition: visitors is never empty - let first_visitor = visitors.first().unwrap(); - - if first_visitor.0[index] > *kind { + if visitors.first().unwrap().0[index] > *kind { // Fast path: If ast path of the first visitor is already out of range, then we // can skip the whole visit. return None; From b95c7b6924bf7f95390ba2d93541f435ff9e2851 Mon Sep 17 00:00:00 2001 From: h-a-n-a Date: Mon, 13 Feb 2023 21:17:21 +0800 Subject: [PATCH 3/4] chore: cr --- crates/turbopack-ecmascript/src/path_visitor.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/turbopack-ecmascript/src/path_visitor.rs b/crates/turbopack-ecmascript/src/path_visitor.rs index e347bb9905047..7c0f5000d4b2e 100644 --- a/crates/turbopack-ecmascript/src/path_visitor.rs +++ b/crates/turbopack-ecmascript/src/path_visitor.rs @@ -29,13 +29,19 @@ fn find_range<'a, 'b>( index: usize, ) -> Option<&'b [(&'a AstPath, &'a dyn VisitorFactory)]> { // Precondition: visitors is never empty - if visitors.first().unwrap().0[index] > *kind { + if visitors.first().unwrap().0[index] > *kind || visitors.last().unwrap().0[index] < *kind { // Fast path: If ast path of the first visitor is already out of range, then we // can skip the whole visit. return None; } - let start = visitors.partition_point(|(path, _)| path[index] < *kind); + let start = if visitors.first().unwrap.0[index] == *kind { + // Fast path: It looks like the whole range is selected + 0 + } else { + visitors.partition_point(|(path, _)| path[index] < *kind) + }; + if start >= visitors.len() { return None; } From 3e5cb84e3339970552828b9897b6c5bfca41ef02 Mon Sep 17 00:00:00 2001 From: Hana Date: Mon, 13 Feb 2023 22:25:10 +0800 Subject: [PATCH 4/4] chore: update --- crates/turbopack-ecmascript/src/path_visitor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/turbopack-ecmascript/src/path_visitor.rs b/crates/turbopack-ecmascript/src/path_visitor.rs index 7c0f5000d4b2e..895029cbc5092 100644 --- a/crates/turbopack-ecmascript/src/path_visitor.rs +++ b/crates/turbopack-ecmascript/src/path_visitor.rs @@ -35,7 +35,7 @@ fn find_range<'a, 'b>( return None; } - let start = if visitors.first().unwrap.0[index] == *kind { + let start = if visitors.first().unwrap().0[index] == *kind { // Fast path: It looks like the whole range is selected 0 } else {