Home >Backend Development >C++ >How Does Libc 's Short String Optimization (SSO) Work?
Delving into Short String Optimization in Libc
In libc , short string optimization (SSO) is employed to minimize the memory footprint and increase performance for short strings. This article delves into the nuts and bolts of SSO implementation in libc to clarify its mechanics.
Criteria for SSO Eligibility
The length threshold for SSO eligibility depends on the target architecture. For 32-bit machines, SSO is enabled for strings of up to 10 characters, while for 64-bit machines, it extends to strings of up to 22 characters. This is determined by the memory layout of the string class, specifically the allocation space available for data storage without the need for a separate allocation.
Differentiating Short and Long Strings
Libc distinguishes between short and long strings using a flag within the member variable that stores the string size. For short strings, this flag is set to 0, indicating that the size field contains the size directly. For long strings, the flag is set to 1, and the size field is disabled, using __long_mask to work around the flag.
Accessing Data in Short and Long Strings
For short strings, the size can be retrieved by shifting the size field by 1 to accommodate the is_long flag. In contrast, long strings use a separate member variable to store the capacity, accessed via getters and setters that work around the is_long bit using __long_mask.
Capacity of Short Strings
The capacity of short strings, determined by __min_cap, is calculated based on the available memory size and the allocated word size. On 32-bit machines, the capacity is 10 characters, while on 64-bit machines, it is 22 characters. This ensures that short strings can fully utilize the available memory without requiring an external allocation.
Alternative String Layout
Libc provides a configurable option, LIBCPP_ABI_ALTERNATE_STRING_LAYOUT, which rearranges the member variables of the long string struct. This is intended to improve performance by placing __data at the beginning of the struct for better alignment. However, this is an experimental feature that should be used with caution due to ABI compatibility concerns.
The above is the detailed content of How Does Libc 's Short String Optimization (SSO) Work?. For more information, please follow other related articles on the PHP Chinese website!