[Bug d/124846] phobos count([]char, char) poorly optimized

witold.baryluk+gcc at gmail dot com gcc-bugzilla@gcc.gnu.org
Sat Apr 11 13:44:00 GMT 2026


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124846

--- Comment #2 from Witold Baryluk <witold.baryluk+gcc at gmail dot com> ---
My bad.

The issue is actually string auto-decoding.

If I use

```
auto mob(char[] s) {
  import std.string : representation;
  import std.algorithm.searching : count;

  auto n = s.representation.count('1') - 1;
  s[0 .. n] = '1';
  return s;
}
```

it produces perfect code.

The issue stems from this piece of code:


    alias T = ElementType!R; //For narrow strings forces dchar iteration
    foreach (T elem; haystack)


expanding to 

    foreach (dchar elem; haystack)

when R is []char.


Using `representation` (which casts to `ubyte[]`), solves the problem. As is
changing the input array to be `ubyte[]` instead.


More information about the Gcc-bugs mailing list