Skip to content

Add new direct delegation mode and use it in preset inheritance#111

Merged
soareschen merged 6 commits intomainfrom
direct-delegate
Jun 16, 2025
Merged

Add new direct delegation mode and use it in preset inheritance#111
soareschen merged 6 commits intomainfrom
direct-delegate

Conversation

@soareschen
Copy link
Collaborator

@soareschen soareschen commented Jun 16, 2025

Summary

This PR adds a new direct delegation for delegate_components! and presets to work more efficiently. The direct delegation mode works by delegating to the target's delegate of the same key, instead of the target itself.

Direct delegation can bring significant performance improvements, especially for presets and async functions, as it reduces the number of indirection calls generated from the generic code, thereby reducing the compile time.

New -> Delegation Syntax

A new delegation syntax -> is now supported, and can be used in place of : for direct delegation in delegate_components!.

Consider the following example:

delegate_components! {
    MyComponents {
        FooComponent: FooProvider,
        BarComponent -> BarProvider,
    }
}

The macro will generate the following code:

impl DelegateComponents<FooComponent> for MyComponents {
    type Provider = FooProvider;
}

impl DelegateComponents<BarComponent> for MyComponents 
where
    BarProvider: DelegateComponents<BarComponent>,
{
    type Provider = <BarProvider as DelegateComponents<BarComponent>>::Delegate;
}

As we can see, when direct delegation -> is used with BarComponnent, the macro will delegate to <BarProvider as DelegateComponents<BarComponent>>::Delegate instead of BarProvider. This requires that BarProvider itself to also contain a delegation entry for BarComponent, as compared to implementing the provider trait directly.

Direct Delegation in Preset Inheritance

The direct delegation mode is now used in preset inheritance. Since we know that the parent preset always contains the component maps instead of implementing the provider traits directly, the direct delegation mode would always work and thus can be used safely.

The use of direct delegation with presets significantly improves the compile time, especially when many nested levels of presets are used, such as in Hypershell.

Direct Delegation in #[cgp_context]

When preset inheritance is used in #[cgp_context], the direct delegation mode is also used. This result in the context implementation having zero overhead when using presets, as compared to defining the component mappings directly.

@soareschen soareschen merged commit abb4573 into main Jun 16, 2025
5 checks passed
@soareschen soareschen deleted the direct-delegate branch June 16, 2025 11:39
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