llvm / llvm/llvm-project

Missed optimization: sub-optimal std::string::size codegen

Open
#172,460 2 comments 0 reactions 0 assignees View on GitHub
backend:X86 missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

std::string::size implementation:
https://github.com/llvm/llvm-project/blob/d2a097d6a36e28e6b2d8b2fcdb7c0fafe5e08e6a/libcxx/include/string#L1289-L1291

Currently clang generates:
```
movzx eax, byte ptr [rdi]
test al, 1
je .LBB1_1
mov rax, qword ptr [rdi + 8]
ret
.LBB1_1:
shr eax
ret
```
https://godbolt.org/z/Eq4x4rsY9

I think this could be (and it would be more efficient: less instructions/code, fewer instructions executed in the "long" case):
```
movzx eax, byte ptr [rdi]
shr eax
jnc .LBB1_1
mov rax, qword ptr [rdi + 8]
.LBB1_1:
ret
```

Contributor guide

Open the contributing guide

Research direction

Start with the std::string::size implementation in libcxx/include/string at the linked lines, then inspect the Godbolt example and compare Clang's generated assembly. Determine whether the proposed flag-based control flow is valid and consistently improves the long-string case. Done means the missed optimization is addressed with appropriate LLVM project validation.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.