microsoft / microsoft/STL

<string>: Consider retuning the Small String Optimization

Open
#295 17 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

performance vNext
Dominant language
C++
Stars
11.1k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

Currently, our basic_string implementation can store up to 15 narrow characters, or 7 wide characters, before dynamically allocating memory:

https://github.com/microsoft/STL/blob/58bb49d63d92e7a0346a05af29816aeea6b4cf0f/stl/inc/xstring#L2134-L2135
https://github.com/microsoft/STL/blob/58bb49d63d92e7a0346a05af29816aeea6b4cf0f/stl/inc/xstring#L2185-L2196

We should consider retuning this optimization, given platform evolution over the last 20 years.

@BillyONeal commented: "Note that libc++'s implementation is a full pointer narrower than ours and stores up to 22 characters instead of 15; optimally small."

Related note: std::function's Small Functor Optimization was retuned before VS 2015 froze binary compatibility, using the heuristic that a function object storing a std::string should be considered small (regardless of architecture or debug mode). We don't necessarily have to retune std::function again, but we should at least check that the heuristic is still satisfied.

Also tracked by Microsoft-internal VSO-154236 / AB#154236.

vNext note: Resolving this issue will require breaking binary compatibility. We won't be able to accept pull requests for this issue until the vNext branch is available. See #169 for more information.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the referenced implementation in stl/inc/xstring, especially the cited basic_string layout and capacity logic. Compare its small-string assumptions with libc++ and inspect the std::function small-functor heuristic described in the issue. This work must wait for the vNext branch and is complete only when the optimization is retuned while accounting for the required binary-compatibility break.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend, performance
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.