Skip to content

gobject: Register a global with the object/interface GType

Christian Hergert requested to merge chergert/gtype-escape-hatch into main

This makes the G_DEFINE_TYPE(), G_DEFINE_INTERFACE() and similar variants provide a TypeName##_type_id global similar to how the private offset is stored as a global.

What this allows for, is for applications and libraries which have high demand on the type system to have an escape hatch to improve performance.

It has been observed that most of the type checking comes from inside the same module that implements the type. Therefore, if that source file can access the GType without an external call to the get_type() function, the overhead can be reduced to an address load.

For example, this is used in libdex heavily to ensure that get_type() do not show up in performance profiles. This is done by redefining the type macro to use the global variable.

#undef FOO_TYPE_BAR
#define FOO_TYPE_BAR FooBar_type_id

The one place where you need to be careful of this is the new function which may cause the registration of the type if global registration at library/application startup is not performed.

Additionally, if using new-style G_DECLARE_* macros, you may also want to override the IS_TYPE() macro to use the fast path.

#define FOO_IS_BAR(obj) G_TYPE_CHECK_INSTANCE_TYPE(obj, FOO_TYPE_BAR)

On this system, this can have a wild performance implication. For example, the type check for GDBusMessage when dragging a window around in GNOME Shell and activating the overview had a whopping 2.96% of samples.

Edited by Christian Hergert

Merge request reports