tint: Add TypeInfo as a field instead of a virtual

Removes the overhead of a virtual function call, at the expense of a larger object.

Change-Id: I81d783d4a69b16a69143d4b313f8d90d5df88a33
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/114080
Commit-Queue: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Auto-Submit: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2022-12-14 11:22:23 +00:00 committed by Dawn LUCI CQ
parent 188ed1793a
commit 167a7da051

View File

@ -339,7 +339,7 @@ class CastableBase {
CastableBase& operator=(const CastableBase& other) = default;
/// @returns the TypeInfo of the object
virtual const tint::TypeInfo& TypeInfo() const = 0;
inline const tint::TypeInfo& TypeInfo() const { return *type_info_; }
/// @returns true if this object is of, or derives from the class `TO`
template <typename TO>
@ -381,6 +381,9 @@ class CastableBase {
protected:
CastableBase() = default;
/// The type information for the object
const tint::TypeInfo* type_info_ = nullptr;
};
/// Castable is a helper to derive `CLASS` from `BASE`, automatically
@ -405,9 +408,6 @@ class CastableBase {
template <typename CLASS, typename BASE = CastableBase>
class Castable : public BASE {
public:
// Inherit the `BASE` class constructors.
using BASE::BASE;
/// A type alias for `CLASS` to easily access the `BASE` class members.
/// Base actually aliases to the Castable instead of `BASE` so that you can
/// use Base in the `CLASS` constructor.
@ -416,8 +416,12 @@ class Castable : public BASE {
/// A type alias for `BASE`.
using TrueBase = BASE;
/// @returns the TypeInfo of the object
const tint::TypeInfo& TypeInfo() const override { return TypeInfo::Of<CLASS>(); }
/// Constructor
/// @param args the arguments to forward to the base class.
template <typename... ARGS>
inline explicit Castable(ARGS&&... args) : TrueBase(std::forward<ARGS>(args)...) {
this->type_info_ = &TypeInfo::Of<CLASS>();
}
/// @returns true if this object is of, or derives from the class `TO`
/// @see CastFlags