glfw: use comptime magic to remove InternalUserPointer and associated overhead #162
No reviewers
Labels
No labels
CI
all
basisu
blog
bug
build
contributor-friendly
core
correctness
deferred
dev
direct3d-headers
docs
driver-os-issue
duplicate
dxcompiler
editor
examples
experiment
feature-idea
feedback
flac
freetype
gamemode
gkurve
glfw
gpu
gpu-dawn
harfbuzz
help welcome
in-progress
infrastructure
invalid
libmach
linux-audio-headers
long-term
mach
mach.gfx
mach.math
mach.physics
mach.testing
model3d
needs-triage
object
opengl-headers
opus
os/linux
os/macos
os/wasm
os/windows
package-manager
priority
proposal
proposal-accepted
question
roadmap
slipped
stability
sysaudio
sysgpu
sysjs
validating-fix
vulkan-zig-generated
wayland-headers
website
wontfix
wrench
www
x11-headers
xcode-frameworks
zig-update
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
hexops/mach!162
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "no_hidden_allocation"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This PR changes the way glfw callback functions are implemented.
Improvements
Before
Inside Window.zig
fromthere is an allocation of aInternalUserPointerwith an allocator not explicitly provided by the caller.After
No hidden allocation anywhere. (atleast in the zig wrapper's code)
Before
Due to using the user pointer functionality provided by glfw to carry around function callbacks, the API to work with the end users actual user pointer generates more code.
After
getUserPointer&setUserPointerare trivial passthrough functions.Before
When an event occurs that the user has set a callback for the below takes place before the users callback code is reached:
from)After
When an event occurs that the user has set a callback for the below takes place before the users callback code is reached:
There is no second call through a runtime function pointer due to a very nice thing about functions that are taken as comptime arguments, they are statically determined at compile time and calling them results in the same codegen as calling a function directly. Due to this we can even force the function to be fully inlined essentially turning the c wrapper function into a prelude of the users callback function with no additional function call overhead.
API Change
The change to take the functions as comptime arguments does prevent runtime known funcion pointers from being provided, however in almost every situation a truly runtime function pointer is rare.
The most common reason a function pointer is runtime known is due to deciding between different implementations based on a runtime value, however this can easily work with the new API:
Or the user could decide to install a callback that calls a runtime known function pointer, this would be very similar to how the current API is implemented, however the choice to do this would be in the users hands.
I like this change. I squinted at this part of the library for a bit, but never thought of a solution like this, aside from maybe allowing the user to provide an allocator.
Thanks so much for this, this looks incredible. Super nice solution! I wouldn't have thought of this :)