Skip to content

Commit 102feeb

Browse files
committed
Regex impl (facebook#2607)
Summary: Pull Request resolved: facebook#2607 Execution style chosen: All of them. This uses a backtracking implementation by default, but, unless it can prove it isn't needed, will track the number of backtracks that occur and fall back to the NFA, or, if available, DFA executor, if too many occur. This gets the perf benefits for the backtracking implementation by default in most cases, while also countering the worst-case-scenario. For the benchmarks, Folly's implementation manages at least one, sometimes two, orders of magnitude improvement vs. the other regex implementations. The only implementations it currently falls behind in are a couple where CTRE wins. It also manages to beat CTRE in quite a few others, and importantly, is consistently fast. There are a number of patterns in the benchmarks where CTRE shows catastrophic performance, all of which this implementation handles without issue. Work is still ongoing, the PR is only to see how the OSS tests behave. Differential Revision: D97922157
1 parent ff3c1b8 commit 102feeb

40 files changed

Lines changed: 23144 additions & 0 deletions

‎folly/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,6 +1507,7 @@ if (PYTHON_EXTENSIONS)
15071507
add_subdirectory(python)
15081508
endif()
15091509
add_subdirectory(random)
1510+
add_subdirectory(regex)
15101511
add_subdirectory(result)
15111512
add_subdirectory(settings)
15121513
add_subdirectory(ssl)

‎folly/portability/Constexpr.h‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,28 @@ constexpr bool is_constant_evaluated_or(
141141
#endif
142142
}
143143

144+
// constexpr_memcpy
145+
//
146+
// constexpr-compatible memcpy. Uses __builtin_memcpy when available as a
147+
// constexpr builtin (dramatically faster in constexpr evaluation), otherwise
148+
// falls back to a trivial byte-by-byte copy loop.
149+
template <typename T>
150+
constexpr T* constexpr_memcpy(
151+
T* dest, const T* src, std::size_t count) noexcept {
152+
#if defined(__has_constexpr_builtin)
153+
#if __has_constexpr_builtin(__builtin_memcpy)
154+
__builtin_memcpy(dest, src, count * sizeof(T));
155+
#else
156+
for (std::size_t i = 0; i < count; ++i) {
157+
dest[i] = src[i];
158+
}
159+
#endif
160+
#else
161+
for (std::size_t i = 0; i < count; ++i) {
162+
dest[i] = src[i];
163+
}
164+
#endif
165+
return dest;
166+
}
167+
144168
} // namespace folly

‎folly/regex/BUCK‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
load("@fbsource//tools/build_defs/dirsync:fb_dirsync_cpp_library.bzl", "fb_dirsync_cpp_library")
2+
3+
oncall("fbcode_entropy_wardens_folly")
4+
5+
fb_dirsync_cpp_library(
6+
name = "regex",
7+
headers = [
8+
"Regex.h",
9+
],
10+
use_raw_headers = True,
11+
exported_deps = [
12+
"//folly/regex/detail:ast",
13+
"//folly/regex/detail:ast_optimizer",
14+
"//folly/regex/detail:char_class",
15+
"//folly/regex/detail:dfa",
16+
"//folly/regex/detail:dfa_executor",
17+
"//folly/regex/detail:direction",
18+
"//folly/regex/detail:executor",
19+
"//folly/regex/detail:nfa",
20+
"//folly/regex/detail:nfa_executor",
21+
"//folly/regex/detail:parser",
22+
],
23+
)

‎folly/regex/CMakeLists.txt‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# @generated by folly/facebook/generate_cmake.py
16+
17+
folly_add_library(
18+
NAME regex
19+
HEADERS
20+
Regex.h
21+
EXPORTED_DEPS
22+
folly_regex_detail_ast
23+
folly_regex_detail_ast_optimizer
24+
folly_regex_detail_char_class
25+
folly_regex_detail_dfa
26+
folly_regex_detail_dfa_executor
27+
folly_regex_detail_direction
28+
folly_regex_detail_executor
29+
folly_regex_detail_nfa
30+
folly_regex_detail_nfa_executor
31+
folly_regex_detail_parser
32+
)
33+
34+
add_subdirectory(detail)

0 commit comments

Comments
 (0)