Skip to content

Commit

Permalink
Auto merge of #125609 - diondokter:opt-size-char-count, r=thomcc
Browse files Browse the repository at this point in the history
Always use the general case char count with `optimize_for_size`

The faster algo is really expensive, over a kilobyte if the full algo is present in a binary.
With this PR the general case algo is picked always instead of only for small strings.

In a test of mine this change makes the total binary go from 3116 bytes to 2032 bytes in opt-level 3 and from 1652 bytes to 1428 bytes in opt-level z. I've seen it much worse in real application, so the savings (especially on 'z') will be higher in many cases.

This is the second pr of this kind after #125606
  • Loading branch information
bors committed May 28, 2024
2 parents 71213fd + 05fa647 commit d86e122
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion library/core/src/str/count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const UNROLL_INNER: usize = 4;

#[inline]
pub(super) fn count_chars(s: &str) -> usize {
if s.len() < USIZE_SIZE * UNROLL_INNER {
if cfg!(feature = "optimize_for_size") || s.len() < USIZE_SIZE * UNROLL_INNER {
// Avoid entering the optimized implementation for strings where the
// difference is not likely to matter, or where it might even be slower.
// That said, a ton of thought was not spent on the particular threshold
Expand Down

0 comments on commit d86e122

Please sign in to comment.