ReferencePool.cs 991 B

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