Skip to content

Feature request: Slim source location without function name #1337

@jonwis

Description

@jonwis

Version

2.0.220131.2

Summary

The std::source_location is super useful, but very very noisy in terms of binary size. Turning it on for a certain OS binary increased its size from 1500kb to 2100kb, or about 40%. While the actual strings are likely to be optimized away into a cold section of the binary, the additional DLL size tradeoff is not appropriate. WIL, for instance, uses just __FILE__ and __LINE__ as the markers, and we've been very successful debugging with those.

There is no means to tell std::source_location to capture only file & line information.

I propose an option for winrt::source_location which uses a technique like

Reproducible example

#include <algorithm>
#include <source_location>

namespace winrt
{
    namespace details
    {
        struct source_location
        {
            const char* file;
            uint32_t line;
        };

        template<size_t length> struct string_literal {
            constexpr string_literal(const char(&str)[length]) {
                std::copy_n(str, length, value);
            }
            char value[length];
        };

        template<string_literal file, uint32_t line> struct source_loc
        {
            inline static constexpr source_location loc{file.value, line};
        };

        constexpr source_location from_std_location(std::source_location src)
        {
            source_location loc;
            loc.file = src.file_name();
            loc.line = src.line();
            return loc;
        }
    }
}

#define SOURCE_LOCATION_CURRENT winrt::details::source_loc<__FILE__, __LINE__>::loc
#define SOURCE_LOCATION_CURRENT_STD winrt::details::from_std_location(std::source_location::current())

Expected behavior

No response

Actual behavior

No response

Additional comments

No response

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions