7

D2752R0: Static storage for braced initializers

 2 years ago
source link: https://quuxplusone.github.io/draft/d2752-static-storage-for-braced-initializers.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Static storage for braced initializers

Abstract

Initializing a vector from a braced-initializer-list like copies the data from static storage to a backing array on the stack, and thence into the vector. This wastes CPU cycles, but more importantly, it wastes stack space. We propose to eliminate the waste by permitting a ’s backing array to exist in static storage.

Table of Contents

1. Changelog

    • Initial draft.

2. Background

[dcl.init.list]/5–6 says:

An object of type is constructed from an initializer list as if the implementation generated and materialized a prvalue of type “array of N ”, where N is the number of elements in the initializer list. Each element of that array is copy-initialized with the corresponding element of the initializer list, and the object is constructed to refer to that array.

[Example 12:

The initialization will be implemented in a way roughly equivalent to this: assuming that the implementation can construct an object with a pair of pointers. —end example] [...]

[Note 6: The implementation is free to allocate the array in read-only memory if an explicit array with the same initializer can be so allocated. —end note]

In December 2022, Jason Merrill observed ([CoreReflector]) that this note isn’t saying much. Consider the following translation unit:

Can the backing array for be allocated in static storage? No, because 's implementation might look like this:

A conforming C++23 implementation must compile this code in such a way that the two temporary backing arrays — the one pointed to by during the first recursive call to , and the one pointed to by during the second recursive call to — have distinct addresses, because that would also be true of "an explicit array [variable]" in the scope of .

All three of GCC, Clang, and MSVC compile this code in a conforming manner. (Godbolt.)

Expert programmers tend to understand that when they write

they’re getting a copy of the data from its original storage into the heap-allocated STL container: obviously the data has to get from point A to point B somehow. But even expert programmers are surprised to learn that there are actually two copies happening here — one from static storage onto the stack, and another from stack to heap!

Worse: [P1967R9] (adopted for C++26) allows programmers to write

Suppose "2mb-image.png" contains 2MB of data. Then the function that initializes here will create a temporary backing array of size 2MB. That is, we’re adding 2MB to the stack frame of that function. Suddenly your function’s stack frame is 2MB larger than you expected! And this applies even if the "function" in question is the compiler-generated initialization routine for a dynamically initialized global variable . You might not even control the function whose stack frame is blowing up.

This kind of thing was always possible pre-P1967, but was hard to hit in human-generated code, because braced initializer lists were generally short. But post-P1967, this is easy to hit. I think this stack-frame blowup is going to become a well-known problem unless we solve it first.

2.1. Workaround

This code creates a 2MB backing array on the stack frame of the function that initializes :

This code does not:

So the latter is a workaround. But it shouldn’t be necessary to work around this issue; we should fix it instead.

3. Solution

Essentially we want the semantics of braced initializer lists to match the semantics of string literals ([lex.string]/9).

We want to encourage tomorrow’s compilers to avoid taking up any stack space for constant-initialized initializer lists. If possible I’d like to mandate they not take any stack space, but I don’t currently know how to specify that. Quality implementations will take advantage of this permission anyway.

We want to permit tomorrow’s compilers to share backing arrays with elements in common, just like today’s compilers can share string literals. High-quality implementations might take advantage of this permission, even though mainstream compilers probably won’t bother.

We even intend to permit tomorrow’s compilers to share backing arrays between static and dynamic initializer lists. It would take a really smart compiler to exploit this new permission, but we have no reason to forbid it. For example:

We do not intend to permit accessing a backing array outside of its lifetime, even when it happens to be stored in static storage.

We do not intend to interfere with [class.base.init]/11, which makes it not just UB but actually ill-formed to bind a reference member to a temporary. (CWG 1696, from 2014, seems to be related. As of December 2022, Clang diagnoses this example; GCC doesn’t; MSVC gives a possibly unrelated error message.)

We do not intend to permit tomorrow’s compiler to defer or omit the side effects of constructor or destructor calls involved with the creation of a backing array. In practice, we expect compilers to "static-fy" backing arrays of types that are trivially destructible, and not to "static-fy" anything else.

We do not intend to cause any new race conditions when is mixed with . Since the contents of a backing array are never modified, I don’t see any way that static-fying a backing array could interfere with multithreading; but I mention it specifically here in case someone sees a problem that I don’t.

4. Implementation experience

I have an experimental patch against Clang trunk; see [Patch]. It is likely incomplete and buggy; it has not received attention from anyone but myself.

4.1. Implications for ABI vendors

So that inline functions will agree on the names of their hidden backing-array variables, I think it’s possible that each ABI (Itanium, Microsoft) will have to agree on a mangling scheme for those hidden variables, just as each ABI has agreed on manglings for the names of (1) guard variables for thread-safe static initialization; (2) backing variables for structured bindings; (3) lambda closure types. On the other hand, these static backing arrays behave very much like string literals, which are implemented with non-external symbols and need no mangling; so maybe there is no need for different TUs to agree on their names.

5. Proposed wording relative to the current C++23 draft

Modify [dcl.init.list]/5–6 as follows:

An object of type is constructed from an initializer list as if the implementation generated and materialized a prvalue an object of type “array of N ”, where N is the number of elements in the initializer list ; this is called the initializer list’s backing array . Each element of that array the backing array is copy-initialized with the corresponding element of the initializer list, and the object is constructed to refer to that array.

[Example 12:

The initialization will be implemented in a way roughly equivalent to this: assuming that the implementation can construct an object with a pair of pointers. —end example]

Whether two backing arrays with the same contents are distinct (that is, are stored in nonoverlapping objects) is unspecified.

The backing array has the same lifetime as any other temporary object, except that initializing an object from the array extends the lifetime of the array exactly like binding a reference to a temporary.

[Example 12: The initialization will be implemented in a way roughly equivalent to this: assuming that the implementation can construct an object with a pair of pointers. —end example]

[Example 13:

For and , the object is a parameter in a function call, so the array created for has full-expression lifetime. For , the object is a variable, so the array persists for the lifetime of the variable. For , the object is initialized in the constructor’s ctor-initializer as if by binding a temporary array to a reference member, so the program is ill-formed. —end example]

[Note 6: The implementation is free to allocate the array in read-only memory if an explicit array with the same initializer can be so allocated. —end note]

5.1. Addition to Annex C

We might want to add something to Annex C [diff.cpp20.expr], since technically this is a breaking change; but on the other hand we don’t actually expect anyone to notice. Their code should just silently get faster.

6. Acknowledgments

  • Thanks to Jason Merrill for the original issue, and to Andrew Tomazos for recommending Arthur write this paper.

References

Informative References

[CoreReflector] Jason Merrill. [isocpp-core] initializer_list and read-only memory. December 2022. URL: https://lists.isocpp.org/core/2022/12/13625.php [P1967R9] JeanHeyd Meneide. #embed - a scannable, tooling-friendly binary resource inclusion mechanism. October 2022. URL: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p1967r9.html [Patch] Arthur O'Dwyer. Implement P2752R0 Static storage for braced initializers. January 2023. URL: https://github.com/Quuxplusone/llvm-project/tree/p2752


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK