Commit 71b35b8
authored
Rollup merge of #157224 - LiosK:round_char_boundary, r=Mark-Simulacrum
Manually unroll loop in `str::floor_char_boundary`
This commit manually unrolls the while loops to at most three iterations, exploiting the UTF-8 invariant that a character is at most four bytes long.
Benefits:
- Prevents LLVM from generating unbounded unrolled code when `index` is statically known - e.g., `s.floor_char_boundary(20)` previously could emit up to 20 repeated loop bodies.
- Eliminates the check at `index - 3`: the UTF-8 invariant guarantees that if `index - 2` is not a character boundary, `index - 3` must be, so the third iteration needs no conditional.
- Allows out-of-order CPUs to issue all three byte loads in parallel, since their offsets are statically known.
This commit will close #149466 with simpler code. The optimizer appears to be able to eliminate bound checks and panic paths.2 files changed
Lines changed: 51 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2431 | 2431 | | |
2432 | 2432 | | |
2433 | 2433 | | |
| 2434 | + | |
| 2435 | + | |
| 2436 | + | |
| 2437 | + | |
| 2438 | + | |
| 2439 | + | |
| 2440 | + | |
| 2441 | + | |
| 2442 | + | |
| 2443 | + | |
2434 | 2444 | | |
2435 | 2445 | | |
2436 | 2446 | | |
| |||
2477 | 2487 | | |
2478 | 2488 | | |
2479 | 2489 | | |
| 2490 | + | |
| 2491 | + | |
| 2492 | + | |
| 2493 | + | |
| 2494 | + | |
| 2495 | + | |
| 2496 | + | |
| 2497 | + | |
| 2498 | + | |
| 2499 | + | |
2480 | 2500 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| |||
421 | 422 | | |
422 | 423 | | |
423 | 424 | | |
424 | | - | |
425 | | - | |
426 | | - | |
427 | | - | |
428 | | - | |
429 | | - | |
430 | | - | |
431 | | - | |
432 | | - | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
433 | 432 | | |
434 | | - | |
435 | | - | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
436 | 436 | | |
437 | | - | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
438 | 447 | | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
439 | 454 | | |
440 | 455 | | |
441 | 456 | | |
| |||
467 | 482 | | |
468 | 483 | | |
469 | 484 | | |
470 | | - | |
471 | | - | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
472 | 488 | | |
473 | 489 | | |
474 | | - | |
475 | 490 | | |
476 | 491 | | |
477 | | - | |
| 492 | + | |
478 | 493 | | |
479 | 494 | | |
480 | 495 | | |
| |||
0 commit comments