November 10, 2024

New memory layout visualization feature in Visual Studio for C++ wins envy from .NET developers • DEVCLASS

2 min read
rb_thumb

rbs-img

A new feature in Visual Studio 17.9 – the ability to visualize the memory layout of C++ classes, structs and unions – has .NET developers asking for the same feature in C#.

Visual Studio 17.9 is a forthcoming release for which Preview 1 was released in November and Preview 2 earlier this month. Developers were perhaps distracted by the UI refresh preview, but what counts for more is the updated tooling functionality.

One of these, from a user request which goes back to 2018, was the ability understand the memory layout of C++ objects. “For many types of industry it is critical to understand the memory layout of C++ objects, and sure there are rules that it follows that an experienced programmer could figure out, but making it easy [in] Visual Studio would be lowering the barrier to entry for writing efficient code,” explained developer Rob Brink.

The MSVC compiler has switches that report on memory layout – specifically “the /d1reportSingleClassLayoutAAA (where AAA is the type name) and /d1reportAllClassLayout commands,” as program manager Mryam Girmay explained in a post this week about the feature – but it is more helpful to have a report directly in the IDE while coding, rather than as a compiler output.

This arrived in Preview 1 in the form of a right-click tooltip that has a Memory Layout link, showing “how the data members are arranged in memory, including their padding, offsets, and sizes,” according to the initial description, which also warned that “This feature is currently in its experimental stage and will undergo gradual enhancements in the Visual Studio 2022 version 17.9 Preview channels.” It is important to be aware of padding, since it may be a clue to an optimization that reduces wasted memory.

Memory layout examined in Visual Studio 17.9

Girmay’s more detailed description shows an example where a class contains an object whose class has a virtual function, with “a hidden vptr pointing to a vtable for virtual methods. This vptr imposes a high alignment requirement.” The consequence is that the containing class occupies double its expected size “due to the 8-byte alignment that the vptr in the first class requires, which results in the compiler adding padding to the second class.”

The feature has limitations – Girmay states that the property keyword in C++/CLI is not fully supported, for example.

Girmay’s post prompted comments from .NET developers: “can we have that in .NET as well?” and “would love to see something similar for C#.”

The .NET world is very different from C++, with more hidden from the developer – but the general question of how to make code more memory-efficient always matters. For example, a game developer reports saving 15MB in a Unity game by changing a class to a struct. A similar tool would make this kind of improvement easier to find.

Efficient use of memory has always been important, and in the context of cloud computing can make a difference to cost as well as performance.

Source: devclass.com

Leave a Reply

Your email address will not be published. Required fields are marked *