Impact of void pointers in performance

Hi,
I wondered about what the impact of collections based on void pointers (widely used in Glib) was on performance.

In the past, I had read some articles (for example Using void* in Generic C Programming may be Inefficient | Attractive Chaos) which suggested a significant negative impact on performance.

I wanted to ask if this is still the case, or if compilers today apply particular optimizations and what the consequences are on GTK and Gnome.

1 Like

That article basically seems to argue that writing a data structure which is generic is less performant than writing one which is specific to an element data type. That’s true. Specialising your data structure to the type of the data it’s storing will make it faster, either through allowing inlining of element-specific function calls, or reduced pointer dereferences (although there’s a tradeoff there with efficient use of the cache).

The significant downside of writing specialised data structures is that you have to write a lot more specialised code for different data types, or end up with a lot more code generated in your binary (e.g. with C++ templates). That has downsides in maintainer time and development time, or in efficient use of the cache and binary size.

There might be places in GNOME where use of a generic data structure is a performance bottleneck, but I doubt it’s any of the low-hanging fruit for performance optimisation. I wouldn’t worry about it unless you have some sysprof or cachegrind results to hand which show a problem in a specific application/library.

2 Likes