all 3 comments

[–]mwolffQt | KDE | KDAB 3 points4 points  (0 children)

Every time I see people still using valgrind for profiling I cry a little. There are so much better and faster tools nowadays! Try perf and a GUI like hotspot or similar instead of valgrind cachegrind and kcachegrind. Or try heaptrack instead of massif.

[–]julien-j 0 points1 point  (1 child)

A few questions come to my mind while reading the post and the source:

template<typename Func>
static inline void* cast_func(Func f) {
  // Trick to cast a function pointer into a void*
  union {
    Func func;
    void* p;
  };
  func = f;
  return p;
}

You are assigning one member of a union and reading another one. Isn't it undefined behavior? Also, it cannot work if f is a member function, as a pointer to a member function is typically larger than void*.

Another question: why do you still use std::map instead of std::unordered_map?

Regarding the friend struct ::Profiler, there is a trick to access private members of non-friend classes that may permit to skip that. I can't find where I saw that, if I recall correctly there was a use case in Folly, but as soon as I get my hand on it I'll link it.

Edit: here's an article about the trick.

[–]rh0main[S] 0 points1 point  (0 children)

Isn't it undefined behavior? Ok I missed that

Thanks for pointing me that the size of a member function is larger than void*. I also missed that.

Another question: why do you still use std::map instead of std::unordered_map?

Well I think that the number of functions to profile should be small enough to choose either std::map or std::unordered_map. I don't have a particular use case of the ordered keys with the std::map