< Summary

Information
Class: Trailblazer.Pathing.GuidePool<T>
Assembly: Trailblazer
File(s): /home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Pathing/Search/Guide/GuidePool.cs
Line coverage
96%
Covered lines: 26
Uncovered lines: 1
Coverable lines: 27
Total lines: 64
Line coverage: 96.2%
Branch coverage
66%
Covered branches: 8
Total branches: 12
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)50%44100%
Rent()100%22100%
Release(...)75%4487.5%
Destroy(...)50%22100%
Clear()100%11100%

File(s)

/home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Pathing/Search/Guide/GuidePool.cs

#LineLine coverage
 1using SwiftCollections;
 2using System;
 3
 4namespace Trailblazer.Pathing;
 5
 6/// <summary>
 7/// Small synchronized guide-wrapper pool used by hot cache-hit paths.
 8/// </summary>
 9internal sealed class GuidePool<T> where T : class, IGuide
 10{
 11    private const int DefaultInitialCapacity = 128;
 12    private const int DefaultMaxSize = 256;
 13
 288414    private readonly object _sync = new();
 15    private readonly Func<T> _create;
 16    private readonly Action<T> _reset;
 17    private readonly SwiftStack<T> _available;
 18    private readonly int _maxSize;
 19
 288420    public GuidePool(Func<T> create, Action<T> reset)
 21    {
 288422        _create = create ?? throw new ArgumentNullException(nameof(create));
 288423        _reset = reset ?? throw new ArgumentNullException(nameof(reset));
 288424        _available = new SwiftStack<T>(DefaultInitialCapacity);
 288425        _maxSize = DefaultMaxSize;
 288426    }
 27
 28    public T Rent()
 29    {
 167630        lock (_sync)
 31        {
 167632            if (_available.Count > 0)
 154333                return _available.Pop();
 13334        }
 35
 13336        return _create();
 154337    }
 38
 39    public void Release(T guide)
 40    {
 161641        if (guide == null)
 042            return;
 43
 161644        _reset(guide);
 45
 161646        lock (_sync)
 47        {
 161648            if (_available.Count < _maxSize)
 161649                _available.Push(guide);
 161650        }
 161651    }
 52
 53    public void Destroy(T guide)
 54    {
 855        if (guide != null)
 856            _reset(guide);
 857    }
 58
 59    public void Clear()
 60    {
 12061        lock (_sync)
 12062            _available.Clear();
 12063    }
 64}