Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark RTTI methods as const / pure, so compiler could optimize subsequent calls, if necessary. #5049

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions lib/rtti.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,18 +296,26 @@ struct Base {
}; \
DECLARE_TYPEINFO_COMMON(T, ##__VA_ARGS__)

#define DECLARE_TYPEINFO_COMMON(T, ...) \
public: \
static constexpr T *rttiEnabledMarker(T *); \
using TypeInfo = P4::RTTI::TypeInfo<T, ##__VA_ARGS__>; \
[[nodiscard]] P4::RTTI::TypeId typeId() const noexcept override { return TypeInfo::id(); } \
[[nodiscard]] bool isA(P4::RTTI::TypeId typeId) const noexcept override { \
return TypeInfo::isA(typeId); \
} \
\
protected: \
[[nodiscard]] const void *toImpl(P4::RTTI::TypeId typeId) const noexcept override { \
return TypeInfo::isA(typeId) ? TypeInfo::dyn_cast(typeId, this) : nullptr; \
// Common typeinfo boilerplate methods. Note that they are marked pure / const, so consecutive
// calls to is<> / to<> on the same pointer could be eliminated by a compiler.
// - typeId() / isA are const as they do not access any global state, they
// always return the same value (for same input arguments)
// - toImpl() is pure. It only access global state via its arguments (this pointer). So for
// the same `this` the result is also the same.
#define DECLARE_TYPEINFO_COMMON(T, ...) \
public: \
static constexpr T *rttiEnabledMarker(T *); \
using TypeInfo = P4::RTTI::TypeInfo<T, ##__VA_ARGS__>; \
[[nodiscard, gnu::const]] P4::RTTI::TypeId typeId() const noexcept override { \
return TypeInfo::id(); \
} \
[[nodiscard, gnu::const]] bool isA(P4::RTTI::TypeId typeId) const noexcept override { \
return TypeInfo::isA(typeId); \
} \
\
protected: \
[[nodiscard, gnu::pure]] const void *toImpl(P4::RTTI::TypeId typeId) const noexcept override { \
return TypeInfo::isA(typeId) ? TypeInfo::dyn_cast(typeId, this) : nullptr; \
}

#endif /* LIB_RTTI_H_ */
Loading