ArgumentsInstancePool.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using Jint.Native;
  2. using Jint.Native.Function;
  3. using Jint.Runtime.Environments;
  4. namespace Jint.Pooling
  5. {
  6. /// <summary>
  7. /// Cache reusable <see cref="JsArguments" /> instances as we allocate them a lot.
  8. /// </summary>
  9. internal sealed class ArgumentsInstancePool
  10. {
  11. private const int PoolSize = 10;
  12. private readonly Engine _engine;
  13. private readonly ObjectPool<JsArguments> _pool;
  14. public ArgumentsInstancePool(Engine engine)
  15. {
  16. _engine = engine;
  17. _pool = new ObjectPool<JsArguments>(Factory, PoolSize);
  18. }
  19. private JsArguments Factory()
  20. {
  21. return new JsArguments(_engine)
  22. {
  23. _prototype = _engine.Realm.Intrinsics.Object.PrototypeObject
  24. };
  25. }
  26. public JsArguments Rent(JsValue[] argumentsList) => Rent(null, null, argumentsList, null, false);
  27. public JsArguments Rent(
  28. Function? func,
  29. Key[]? formals,
  30. JsValue[] argumentsList,
  31. DeclarativeEnvironment? env,
  32. bool hasRestParameter)
  33. {
  34. var obj = _pool.Allocate();
  35. obj.Prepare(func!, formals!, argumentsList, env!, hasRestParameter);
  36. return obj;
  37. }
  38. public void Return(JsArguments instance)
  39. {
  40. if (instance is null)
  41. {
  42. return;
  43. }
  44. _pool.Free(instance);
  45. }
  46. }
  47. }