Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

c++ - String-interning at compiletime for profiling

Context

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

MEASURE_SCOPE(text_rendering_code);
...
MEASURE_SCOPE(password_hashing);
...
MEASURE_START(system_call);
...
MEASURE_STOP(system_call);

where the macros would be defined like this:

#define MEASURE_START(name) save_start_event(get_timestamp(), #name);
#define MEASURE_STOP(name) save_stop_event(get_timestamp(), #name);
#define MEASURE_SCOPE(name) Profiling_Class object##name (#name);

class Profiling_Class{
    string name;
    Profiling_Class(string name){
        this->name = name; 
        save_start_event(get_timestamp(), name);
    }
    ~Profiling_Class(){save_end_event(get_timestamp(), this->name);}
}

save_start_event and save_end_event would just put the timestamp along with the name into some global buffer for later use (exporting measurements and such).

The problem is this: saving the name of a measurement along with the measurement itself is very inefficient. There also needs to happen a lot of work to pair MEASURE_START and MEASURE_STOP, because checking if their names are same requires a string-compare. A much better solution would be to intern the string, i.e. have some array somewhere that holds all the strings:

std::vector<string> = {"text_rendering_code", "password_hashing", "system_call"};

and substitue the string in the measurement-macros with the index of the string in the array:

MEASURE_SCOPE(0);
...
MEASURE_SCOPE(1);
...
MEASURE_START(2);
...
MEASURE_STOP(2);

This way requires less storage, and checking if names match becomes a simple integer-compare. On the other hand, it's very unfriendly towards the user, since he has to know in advance the index of the name he wants to give his measurement.

Question

Is there a way to preserve the nice usage of MEASURE_SCOPE(text_rendering_code) and substitute this with the more efficient MEASURE_SCOPE(0) automatically? This would require building the name-array at compile time, effectively interning the strings. Is this possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 <char...Cs> struct char_sequence
{
    template <char C> using push_back = char_sequence<Cs..., C>;
};

// Remove all chars from char_sequence from ''
template <typename, char...> struct strip_sequence;

template <char...Cs>
struct strip_sequence<char_sequence<>, Cs...>
{
    using type = char_sequence<Cs...>;
};

template <char...Cs, char...Cs2>
struct strip_sequence<char_sequence<'', Cs...>, Cs2...>
{
    using type = char_sequence<Cs2...>;
};

template <char...Cs, char C, char...Cs2>
struct strip_sequence<char_sequence<C, Cs...>, Cs2...>
{
    using type = typename strip_sequence<char_sequence<Cs...>, Cs2..., C>::type;
};

// struct to create a aligned char array
template <typename chars> struct static_string;

template <char...Cs>
struct static_string<char_sequence<Cs...>>
{
    static constexpr char str[sizeof...(Cs)] = {Cs...};
};

template <char...Cs>
constexpr 
char static_string<char_sequence<Cs...>>::str[sizeof...(Cs)];

// helper to get the i_th character (`` for out of bound)
template <std::size_t I, std::size_t N>
constexpr char at(const char (&a)[N]) { return I < N ? a[I] : ''; }

// helper to check if the c-string will not be truncated
template <std::size_t max_size, std::size_t N>
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<at<(I) + 0>(S)>::push_back<at<(I) + 1>(S)> 
    ::push_back<at<(I) + 2>(S)>::push_back<at<(I) + 3>(S)> 
    ::push_back<at<(I) + 4>(S)>::push_back<at<(I) + 5>(S)> 
    ::push_back<at<(I) + 6>(S)>::push_back<at<(I) + 7>(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<char_sequence<> 
    PUSH_BACK_128(S, 0) 
    >::type::template push_back<check_size<128>(S) ? '' : ''>

// Macro to return an static c-string
#define MAKE_STRING(S) 
    aligned_string<MAKE_CHAR_SEQUENCE(S)>::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 <typename CHAR, CHAR... cs>
const char* operator ""_c() { return static_string<cs...>{}::str; }

and then

MEASURE_SCOPE("text_rendering_code"_c);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...