Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance Improvements Suggestion: Avoid runtime checks for this.GetService<T>() #171

Open
rafaelsc opened this issue Jan 28, 2024 · 0 comments

Comments

@rafaelsc
Copy link

rafaelsc commented Jan 28, 2024

Currently, Jab uses runtime checks to find the correct service factory inside GetService<T>, this could be done by the Jab in compile time improving the runtime performance.

So instead of this code to all new instances when have depencies:

public T GetService<T>() => this is IServiceProvider<T> provider ? provider.GetService() : throw CreateServiceNotFoundException<T>();

Jab.Performance.Basic.Mixed.IMix1 IServiceProvider<Jab.Performance.Basic.Mixed.IMix1>.GetService()
{
    Jab.Performance.Basic.Mixed.Mix1 service = new Jab.Performance.Basic.Mixed.Mix1(this.GetService<Jab.Performance.Basic.Singleton.ISingleton1>(), this.GetService<Jab.Performance.Basic.Transient.ITransient1>());
    TryAddDisposable(service);
    return service;
}

Update this code to avoid the this is IServiceProvider<T> runtime check.

Jab.Performance.Basic.Mixed.IMix1 IServiceProvider<Jab.Performance.Basic.Mixed.IMix1>.GetService()
{
    ISingleton1 singleton1 = ((IServiceProvider<ISingleton1>)this).GetService();
    ITransient1 transient1 = ((IServiceProvider<ITransient1>)this).GetService();
    Jab.Performance.Basic.Mixed.Mix1 service = new Jab.Performance.Basic.Mixed.Mix1(singleton1, transient1);
    TryAddDisposable(service);
    return service;
}

See: #168 (comment)

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

No branches or pull requests

1 participant