Skip to content
/ ada Public

Commit 27a2d93

Browse files
authoredJul 6, 2024··
perf: improve has_tabs_or_newline performance (#670)
1 parent 14e824e commit 27a2d93

File tree

2 files changed

+9
-16
lines changed

2 files changed

+9
-16
lines changed
 

‎benchmarks/CMakeLists.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ if(RUST_FOUND)
273273

274274
# Check if servo-url target was created successfully
275275
if(TARGET servo-url)
276+
message(STATUS "servo-url target was created. Linking benchmarks and servo-url.")
276277
target_link_libraries(bench PRIVATE servo-url)
277278
target_compile_definitions(bench PRIVATE ADA_RUST_VERSION="${Rust_VERSION}")
278279

@@ -287,8 +288,6 @@ if(RUST_FOUND)
287288

288289
target_link_libraries(wpt_bench PRIVATE servo-url)
289290
target_compile_definitions(wpt_bench PRIVATE ADA_RUST_VERSION="${Rust_VERSION}")
290-
else()
291-
message(WARNING "servo-url target was not created. Skipping linking benchmarks and servo-url.")
292291
endif()
293292
else()
294293
message(STATUS "Rust/Cargo is unavailable." )

‎src/unicode.cpp

+8-14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ ADA_POP_DISABLE_WARNINGS
1616

1717
namespace ada::unicode {
1818

19+
constexpr bool is_tabs_or_newline(char c) noexcept {
20+
return c == '\r' || c == '\n' || c == '\t';
21+
}
22+
1923
constexpr uint64_t broadcast(uint8_t v) noexcept {
2024
return 0x101010101010101ull * v;
2125
}
@@ -50,13 +54,8 @@ ada_really_inline bool has_tabs_or_newline(
5054
std::string_view user_input) noexcept {
5155
// first check for short strings in which case we do it naively.
5256
if (user_input.size() < 16) { // slow path
53-
for (size_t i = 0; i < user_input.size(); i++) {
54-
if (user_input[i] == '\r' || user_input[i] == '\n' ||
55-
user_input[i] == '\t') {
56-
return true;
57-
}
58-
}
59-
return false;
57+
return std::any_of(user_input.begin(), user_input.end(),
58+
is_tabs_or_newline);
6059
}
6160
// fast path for long strings (expected to be common)
6261
size_t i = 0;
@@ -94,13 +93,8 @@ ada_really_inline bool has_tabs_or_newline(
9493
std::string_view user_input) noexcept {
9594
// first check for short strings in which case we do it naively.
9695
if (user_input.size() < 16) { // slow path
97-
for (size_t i = 0; i < user_input.size(); i++) {
98-
if (user_input[i] == '\r' || user_input[i] == '\n' ||
99-
user_input[i] == '\t') {
100-
return true;
101-
}
102-
}
103-
return false;
96+
return std::any_of(user_input.begin(), user_input.end(),
97+
is_tabs_or_newline);
10498
}
10599
// fast path for long strings (expected to be common)
106100
size_t i = 0;

0 commit comments

Comments
 (0)
Please sign in to comment.