Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7866071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:16:57+00:00 2026-06-03T00:16:57+00:00

In a C++ question about optimization and code style , several answers referred to

  • 0

In a C++ question about optimization and code style, several answers referred to “SSO” in the context of optimizing copies of std::string. What does SSO mean in that context?

Clearly not “single sign on”. “Shared string optimization”, perhaps?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:16:59+00:00Added an answer on June 3, 2026 at 12:16 am

    Background / Overview

    Operations on automatic variables (“from the stack”, which are variables that you create without calling malloc / new) are generally much faster than those involving the free store (“the heap”, which are variables that are created using new). However, the size of automatic arrays is fixed at compile time, but the size of arrays from the free store is not. Moreover, the stack size is limited (typically a few MiB), whereas the free store is only limited by your system’s memory.

    SSO is the Short / Small String Optimization. A std::string typically stores the string as a pointer to the free store (“the heap”), which gives similar performance characteristics as if you were to call new char [size]. This prevents a stack overflow for very large strings, but it can be slower, especially with copy operations. As an optimization, many implementations of std::string create a small automatic array, something like char [20]. If you have a string that is 20 characters or smaller (given this example, the actual size varies), it stores it directly in that array. This avoids the need to call new at all, which speeds things up a bit.

    EDIT:

    I wasn’t expecting this answer to be quite so popular, but since it is, let me give a more realistic implementation, with the caveat that I’ve never actually read any implementation of SSO “in the wild”.

    Implementation details

    At the minimum, a std::string needs to store the following information:

    • The size
    • The capacity
    • The location of the data

    The size could be stored as a std::string::size_type or as a pointer to the end. The only difference is whether you want to have to subtract two pointers when the user calls size or add a size_type to a pointer when the user calls end. The capacity can be stored either way as well.

    You don’t pay for what you don’t use.

    First, consider the naive implementation based on what I outlined above:

    class string {
    public:
        // all 83 member functions
    private:
        std::unique_ptr<char[]> m_data;
        size_type m_size;
        size_type m_capacity;
        std::array<char, 16> m_sso;
    };
    

    For a 64-bit system, that generally means that std::string has 24 bytes of ‘overhead’ per string, plus another 16 for the SSO buffer (16 chosen here instead of 20 due to padding requirements). It wouldn’t really make sense to store those three data members plus a local array of characters, as in my simplified example. If m_size <= 16, then I will put all of the data in m_sso, so I already know the capacity and I don’t need the pointer to the data. If m_size > 16, then I don’t need m_sso. There is absolutely no overlap where I need all of them. A smarter solution that wastes no space would look something a little more like this (untested, example purposes only):

    class string {
    public:
        // all 83 member functions
    private:
        size_type m_size;
        union {
            class {
                // This is probably better designed as an array-like class
                std::unique_ptr<char[]> m_data;
                size_type m_capacity;
            } m_large;
            std::array<char, sizeof(m_large)> m_small;
        };
    };
    

    I’d assume that most implementations look more like this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is an open question towards learning about memory optimization and code optimization for
I was reading a question about c# code optimization and one solution was to
Naive question about Java syntax. What does <T> T accept(ObjectVisitorEx<T> visitor); mean? What would
I'm optimizing my website according to Google's site optimization standards: http://code.google.com/speed/page-sp...mageDimensions For those who
I've been reading about about code optimization (to be more precise, code optimization for
I have a question about structure padding and memory alignment optimizations regarding structures in
Question about subclassing in matlab, under the new class system. I've got class A
Question about GridView sorting in VB.NET: I have a GridView with AutoGenerateColumns = True
Question about logics here: What's the most elegant way to make the menu appear/disappear
Question about assignments and variables (* For example *) SP = SparseArray[{},5] or SP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.