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: Removing the TryAddDisposable() #170

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

Comments

@rafaelsc
Copy link

Currently, Jab uses runtime checks to check if the newly created instance is a IDisposable or IAsyncDisposable, this could be done by the Jab in compile time improving the runtime performance.

So instead of this code to all new instances:

Jab.Performance.Basic.Transient.ITransient1 IServiceProvider<Jab.Performance.Basic.Transient.ITransient1>.GetService()
{
    Jab.Performance.Basic.Transient.Transient1 service = new Jab.Performance.Basic.Transient.Transient1();
    TryAddDisposable(service);
    return service;
}

private global::System.Collections.Generic.List<object>? _disposables;
private void TryAddDisposable(object? value)
{
    if (value is global::System.IDisposable || value is System.IAsyncDisposable)
        lock (this)
        {
            (_disposables ??= new global::System.Collections.Generic.List<object>()).Add(value);
        }
}

Update to this code only for new instances from IDisposable or IAsyncDisposable.

Jab.Performance.Basic.Transient.ITransient1 IServiceProvider<Jab.Performance.Basic.Transient.ITransient1>.GetService()
{
    Jab.Performance.Basic.Transient.Transient1 service = new Jab.Performance.Basic.Transient.Transient1();
    _disposables.Add(value);
    return service;
}

private global::System.Collections.Concurrent.ConcurrentBag<IDisposable> _disposables = [];

and similar changes for IAsyncDisposable.

See: #168 (comment)

@rafaelsc rafaelsc changed the title Performance Improvements Suggestion: Removing the TryAddDisposable() Performance Improvements Suggestion: Removing the TryAddDisposable() Jan 28, 2024
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