String-interning at compiletime for profiling

前端 未结 2 758
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-20 07:51

Context

I am working on an instrumenting profiler, that enables you to name different measurements by string. So for example:

MEASURE_SCOPE(text_re         


        
2条回答
  •  情话喂你
    2020-12-20 08:28

    Identical literal strings are not guaranty to be identical, but you can build type from it which can compare identical (without comparing string), something like:

    // Sequence of char
    template  struct char_sequence
    {
        template  using push_back = char_sequence;
    };
    
    // Remove all chars from char_sequence from '\0'
    template  struct strip_sequence;
    
    template 
    struct strip_sequence, Cs...>
    {
        using type = char_sequence;
    };
    
    template 
    struct strip_sequence, Cs2...>
    {
        using type = char_sequence;
    };
    
    template 
    struct strip_sequence, Cs2...>
    {
        using type = typename strip_sequence, Cs2..., C>::type;
    };
    
    // struct to create a aligned char array
    template  struct static_string;
    
    template 
    struct static_string>
    {
        static constexpr char str[sizeof...(Cs)] = {Cs...};
    };
    
    template 
    constexpr 
    char static_string>::str[sizeof...(Cs)];
    
    // helper to get the i_th character (`\0` for out of bound)
    template 
    constexpr char at(const char (&a)[N]) { return I < N ? a[I] : '\0'; }
    
    // helper to check if the c-string will not be truncated
    template 
    constexpr bool check_size(const char (&)[N])
    {
        static_assert(N <= max_size, "string too long");
        return N <= max_size;
    }
    
    // Helper macros to build char_sequence from c-string
    #define PUSH_BACK_8(S, I) \
        ::push_back(S)>::push_back(S)> \
        ::push_back(S)>::push_back(S)> \
        ::push_back(S)>::push_back(S)> \
        ::push_back(S)>::push_back(S)>
    
    #define PUSH_BACK_32(S, I) \
            PUSH_BACK_8(S, (I) + 0) PUSH_BACK_8(S, (I) + 8) \
            PUSH_BACK_8(S, (I) + 16) PUSH_BACK_8(S, (I) + 24)
    
    #define PUSH_BACK_128(S, I) \
        PUSH_BACK_32(S, (I) + 0) PUSH_BACK_32(S, (I) + 32) \
        PUSH_BACK_32(S, (I) + 64) PUSH_BACK_32(S, (I) + 96)
    
    // Macro to create char_sequence from c-string (limited to 128 chars)
    #define MAKE_CHAR_SEQUENCE(S) \
        strip_sequence \
        PUSH_BACK_128(S, 0) \
        >::type::template push_back(S) ? '\0' : '\0'>
    
    // Macro to return an static c-string
    #define MAKE_STRING(S) \
        aligned_string::str
    

    So

    MEASURE_SCOPE(MAKE_STRING("text_rendering_code"));
    

    would still return same pointer than you can compare directly.

    You can modify your Macro MEASURE_SCOPE to include directly MAKE_STRING.

    gcc has an extension to simplify MAKE_STRING:

    template 
    const char* operator ""_c() { return static_string{}::str; }
    

    and then

    MEASURE_SCOPE("text_rendering_code"_c);
    

提交回复
热议问题