Repository: microsoft/terminal
File: src/renderer/atlas/AtlasEngine.h (C/C++)

Code before:
                {
                    ::operator delete(data);
                }
                else
                {
                    ::operator delete(data, static_cast<std::align_val_t>(Alignment));
                }
            }
#pragma warning(pop)

            void destroy() noexcept
            {
                std::destroy_n(_data, _size);
                deallocate(_data);
            }

            T* _data = nullptr;
            size_t _size = 0;
        };

        // This structure works similar to how std::string works:
        // You can think of a std::string as a structure consisting of:
        //   char*  data;
        //   size_t size;
        //   size_t capacity;
        // where data is some backing memory allocated on the heap.
        //
        // But std::string employs an optimization called "small string optimization" (SSO).
        // To simplify things it could be explained as:
        // If the string capacity is small, then the characters are stored inside the "data"
        // pointer and you make sure to set the lowest bit in the pointer one way or another.
        // Heap allocations are always aligned by at least 4-8 bytes on any platform.
        // If the address of the "data" pointer is not even you know data is stored inline.
        template<typename T>
        union SmallObjectOptimizer
        {
            static_assert(std::is_trivially_copyable_v<T>);
            static_assert(std::has_unique_object_representations_v<T>);

            T* allocated = nullptr;
            T inlined;

            constexpr SmallObjectOptimizer() = default;

            SmallObjectOptimizer(const SmallObjectOptimizer& other) = delete;
            SmallObjectOptimizer& operator=(const SmallObjectOptimizer& other) = delete;

            SmallObjectOptimizer(SmallObjectOptimizer&& other) noexcept
            {
                memcpy(this, &other, std::max(sizeof(allocated), sizeof(inlined)));
                other.allocated = nullptr;

Review comment (bug):
These 3 `Buffer` changes should've been here all along. The 3 corresponding STL functions already do exactly the optimal thing: nothing for trivial types (i.e. leave the memory uninitialized) and properly construct/destruct the types if they're proper classes. And in case of `std::uninitialized_copy_n` in particular it already uses `memmove` for trivial types internally. Perfect!
This change allows us to construct a `Buffer<iterator>` which is an array of non-trivial types.

BTW/FYI: I didn't use `std::vector` and wrote `Buffer` instead, because I want to convey that these things explicitly _don't_ hold resizeable contents (in general). For instance I didn't want a `push_back` method on my viewport arrays - that wouldn't make sense so that always felt a bit wrong to me. It also doesn't initialize contents to zero, allowing rapid creation/destruction and you can specify a custom alignment (improves rendering perf by about 20% over `std::vector` IIRC).

Code after:
                    return static_cast<T*>(::operator new(size * sizeof(T)));
                }
                else
                {
                    return static_cast<T*>(::operator new(size * sizeof(T), static_cast<std::align_val_t>(Alignment)));
                }
            }

            static void deallocate(T* data) noexcept
            {
                if constexpr (Alignment <= __STDCPP_DEFAULT_NEW_ALIGNMENT__)
                {
                    ::operator delete(data);
                }
                else
                {
                    ::operator delete(data, static_cast<std::align_val_t>(Alignment));
                }
            }
#pragma warning(pop)

            void destroy() noexcept
            {
                std::destroy_n(_data, _size);
                deallocate(_data);
            }

            T* _data = nullptr;
            size_t _size = 0;
        };

        // This structure works similar to how std::string works:
        // You can think of a std::string as a structure consisting of:
        //   char*  data;
        //   size_t size;
        //   size_t capacity;
        // where data is some backing memory allocated on the heap.
        //
        // But std::string employs an optimization called "small string optimization" (SSO).
        // To simplify things it could be explained as:
        // If the string capacity is small, then the characters are stored inside the "data"
        // pointer and you make sure to set the lowest bit in the pointer one way or another.
        // Heap allocations are always aligned by at least 4-8 bytes on any platform.
        // If the address of the "data" pointer is not even you know data is stored inline.
        template<typename T>
        union SmallObjectOptimizer
        {
            static_assert(std::is_trivially_copyable_v<T>);
            static_assert(std::has_unique_object_representations_v<T>);

            T* allocated = nullptr;