Skip to content

Define one objc_traits method per wrapper class - #92

Merged
maleadt merged 1 commit into
mainfrom
tb/objc-traits
Oct 29, 2026
Merged

maleadt merged 1 commit into
mainfrom
tb/objc-traits

Conversation

@maleadt

@maleadt maleadt commented Oct 28, 2026

Copy link
Copy Markdown
Member

@objcwrapper defines up to four trait methods per wrapper class (objc_parent, is_managed_wrapper, check_available and compare_by_pointer), and every method a package defines adds to its load time: Metal.jl has about 2640 of them.

This defines a single objc_traits(::Type{X}) method per class returning the traits as a NamedTuple, and makes the trait functions read from it. The traits are constant, so they still fold at compile time (generated code is unchanged), and the trait functions remain the interface: hand-written methods for individual classes keep taking precedence, which the added tests check, both directly and through behavior (property lookup through an overridden parent, == with an overridden comparison, an overridden availability check). One nuance: a broader hand-written trait method can now win where a macro-generated exact per-class method used to outrank it.

For Metal.jl this removes about 2000 methods, and loads its package image about 50 ms faster on Julia 1.13 (within noise on 1.12).

`@objcwrapper` defined up to four trait methods per class
(`objc_parent`, `is_managed_wrapper`, `check_available` and
`compare_by_pointer`), and every method a package defines adds to the
time it takes to load it; Metal.jl has about 2640 of them.

Instead define a single `objc_traits` method returning all traits as a
NamedTuple, and make the trait functions read from it. The traits are
constant, so they still fold at compile time, and the trait functions
remain the interface: hand-written methods for individual classes keep
taking precedence.

This removes about 2000 methods from Metal.jl, which loads about 50 ms
faster on Julia 1.13.
@maleadt
maleadt merged commit 79b0af9 into main Oct 29, 2026
32 of 36 checks passed
@maleadt
maleadt deleted the tb/objc-traits branch October 29, 2026 08:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant