Repository navigation
Conversation
|
CI hit #33545 :) |
There was a problem hiding this comment.
This is really nice, thanks! I couldn't find any correctness issues, and the performance boost is big and clear so this seems ready on that front :) There are some questions and suggestions (including a couple that my agent identified) below, many take-it-or-leave-it.
| Faster string searching operations | ||
| ---------------------------------- | ||
| `numpy.strings.startswith` and `numpy.strings.endswith` are now much faster | ||
| for `numpy.dtypes.StringDType` arrays can skip scanning the whole string when |
There was a problem hiding this comment.
run-on here: "are now much faster for StringDType arrays can skip scanning..."
| return 0; | ||
| } | ||
| } | ||
| if (!prepare_search_slice(buf1, start, end)) { |
There was a problem hiding this comment.
The old code had a fast-path for empty needles, should we still have that (in the non-negative index path, for negative indices it's already there)? After all we don't actually run num_codepoints anymore so it could be actually faster!
| if (enc != ENCODING::UTF8) { | ||
| adjust_offsets(&start, &end, buf.num_codepoints()); | ||
| if (start > end) { | ||
| return false; | ||
| } | ||
| buf.after = (buf + end).buf; | ||
| buf += start; | ||
| } | ||
| else { | ||
| if (start < 0 || end < 0) { | ||
| adjust_offsets(&start, &end, buf.num_codepoints()); | ||
| } | ||
| if (start > end || start > (npy_int64)buf.buffer_width() || | ||
| buf.advance(start) != start) { | ||
| return false; | ||
| } | ||
| } | ||
| end -= start; | ||
| return true; |
There was a problem hiding this comment.
I'm tempted to simplify this into stages rather than branch by encoding, something like this:
template <ENCODING enc>
static inline bool
prepare_search_slice(Buffer<enc> &buf, npy_int64 &start, npy_int64 &end)
{
if (enc != ENCODING::UTF8 || start < 0 || end < 0) {
adjust_offsets(&start, &end, buf.num_codepoints());
}
if (start > end) {
return false;
}
if (enc != ENCODING::UTF8) {
buf.after = (buf + end).buf;
buf += start;
}
else if (start > (npy_int64)buf.buffer_width() ||
buf.advance(start) != start) {
return false;
}
end -= start;
return true;
}| static inline bool | ||
| prepare_search_slice(Buffer<enc> &buf, npy_int64 &start, npy_int64 &end) | ||
| { | ||
| if (enc != ENCODING::UTF8) { |
There was a problem hiding this comment.
A bit nitpicky, should we use constexpr here? (If we simplify as below, it can go in the second check.)
Also in any other places where enc is templated and this makes sense.
| static NPY_FINLINE npy_intp | ||
| buffer_search(Buffer<enc> buf, Buffer<enc> pattern, npy_intp max_count, int mode) | ||
| { | ||
| if (enc == ENCODING::UTF32) { |
There was a problem hiding this comment.
These branches can also be merged by typing with CodeUnit!
| } | ||
| if (pattern.buffer_width() == 0) { | ||
| if ((size_t)max_count > buf.buffer_width()) { | ||
| return 1 + buf.num_codepoints(); |
There was a problem hiding this comment.
This does some validation no, otherwise it is the same as the advance below? Is this branch needed for another reason? Not sure how it affects performance (I'd guess advance is faster)!
| buf3.buffer_memcpy(out, span3); | ||
| ret += span3; | ||
| out.advance_chars_or_bytes(span3); | ||
| if (has_null_padding(enc) && len3 == 1) { |
There was a problem hiding this comment.
My agent noticed this branch is not needed!
|
|
||
| template <ENCODING enc> | ||
| inline Buffer<enc> | ||
| operator+(Buffer<enc> lhs, npy_int64 rhs) |
There was a problem hiding this comment.
My agent noticed this could also be simplified to use advance? (It actually suggests +=, which would be much smaller though not sure about the dependency!)
PR summary
A Quansight client privately reported a slow operation on a
StringDTypearray. Here's an example that illustrates the issue:Both
startswithandendswithshould take about the same time on either a short or a long array if there is a match at the beginning or end but on currentmain, performance scales with the length of the strings.I found the same problem in several
np.stringsfunctions: counting characters or continuing to search after finding a match. Skipping those scans helpedStringDTypethe most (particularly forstartswithandendswithwhere searching is the whole operation), and also sped up some fixed-width bytes and Unicode operations. As a bonus, sharing the search and strip code cut the implementation by more than 200 lines.I benchmarked path manipulation and text cleaning on arrays of 1,000 strings. Benchmark code. I tried to include cases that should benefit from this and also cases that shouldn't benefit to catch regressions.
The table shows time with this patch divided by time on the parent commit. Values below 1.000 are faster (release builds, Apple M1, four paired rounds with benchx).
startswith, ASCII pathsendswith, ASCII pathsstartswith, non-ASCII pathsendswith, non-ASCII paths.csvIn the cases I checked, forcing a different link order made the regressions disappear or turn into improvements.
Full benchx summary (47 cases)
I compared
cdd919ae76with0a2e19f713plus the localcountandrfindchanges, using CPython 3.14.6. I alternated which build ran first.AI Disclosure
I worked on this with an AI model.