ReferencePool.cs 877 B

12345678910111213141516171819202122232425262728293031323334353637
  1. using Jint.Native;
  2. using Jint.Runtime;
  3. namespace Jint.Pooling;
  4. /// <summary>
  5. /// Cache reusable <see cref="Reference" /> instances as we allocate them a lot.
  6. /// </summary>
  7. internal sealed class ReferencePool
  8. {
  9. private const int PoolSize = 10;
  10. private readonly ObjectPool<Reference> _pool;
  11. public ReferencePool()
  12. {
  13. _pool = new ObjectPool<Reference>(Factory, PoolSize);
  14. }
  15. private static Reference Factory()
  16. {
  17. return new Reference(JsValue.Undefined, JsString.Empty, false, null);
  18. }
  19. public Reference Rent(JsValue baseValue, JsValue name, bool strict, JsValue? thisValue)
  20. {
  21. return _pool.Allocate().Reassign(baseValue, name, strict, thisValue);
  22. }
  23. public void Return(Reference? reference)
  24. {
  25. if (reference == null)
  26. {
  27. return;
  28. }
  29. _pool.Free(reference);
  30. }
  31. }