Skip to content

Commit

Permalink
perf(expr): prebuild the AC for to_char (#7048)
Browse files Browse the repository at this point in the history
As title, put it on lazy-static to avoid building every time.

bench | Before time(us) | After time(us) | Change(%)
-- | -- | -- | --
to_char(timestamp,varchar) | 11060.000 | 283.900 | -97.4%

Approved-By: TennyZhuang
  • Loading branch information
wangrunji0408 authored Dec 23, 2022
1 parent d28768e commit a182b43
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/expr/src/vector_op/to_char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use aho_corasick::AhoCorasickBuilder;
use std::sync::LazyLock;

use aho_corasick::{AhoCorasick, AhoCorasickBuilder};
use risingwave_common::array::{StringWriter, WrittenGuard};
use risingwave_common::types::NaiveDateTimeWrapper;

Expand All @@ -31,18 +33,18 @@ pub fn compile_pattern_to_chrono(tmpl: &str) -> String {
"%H", "%H", "%I", "%I", "%I", "%I", "%M", "%M", "%S", "%S", "%Y", "%Y", "%y", "%y", "%G",
"%G", "%g", "%g", "%m", "%m", "%d", "%d",
];

let ac = AhoCorasickBuilder::new()
.ascii_case_insensitive(false)
.match_kind(aho_corasick::MatchKind::LeftmostLongest)
.build(PG_PATTERNS);
static AC: LazyLock<AhoCorasick> = LazyLock::new(|| {
AhoCorasickBuilder::new()
.ascii_case_insensitive(false)
.match_kind(aho_corasick::MatchKind::LeftmostLongest)
.build(PG_PATTERNS)
});

let mut chrono_tmpl = String::new();
ac.replace_all_with(tmpl, &mut chrono_tmpl, |mat, _, dst| {
AC.replace_all_with(tmpl, &mut chrono_tmpl, |mat, _, dst| {
dst.push_str(CHRONO_PATTERNS[mat.pattern()]);
true
});

chrono_tmpl
}

Expand Down

0 comments on commit a182b43

Please sign in to comment.