ArrayExecutionContext.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. using System.Collections.Generic;
  2. using System.Text;
  3. using System.Threading;
  4. namespace Jint.Native.Array
  5. {
  6. /// <summary>
  7. /// Helper to cache common data structures needed in array access on a per thread basis.
  8. /// </summary>
  9. internal class ArrayExecutionContext
  10. {
  11. // cache key container for array iteration for less allocations
  12. private static readonly ThreadLocal<ArrayExecutionContext> _executionContext = new ThreadLocal<ArrayExecutionContext>(() => new ArrayExecutionContext());
  13. private List<uint> _keyCache;
  14. private JsValue[] _callArray1;
  15. private JsValue[] _callArray3;
  16. private JsValue[] _callArray4;
  17. private StringBuilder _stringBuilder;
  18. private ArrayExecutionContext()
  19. {
  20. }
  21. public List<uint> KeyCache => _keyCache = _keyCache ?? new List<uint>();
  22. public JsValue[] CallArray1 => _callArray1 = _callArray1 ?? new JsValue[1];
  23. public JsValue[] CallArray3 => _callArray3 = _callArray3 ?? new JsValue[3];
  24. public JsValue[] CallArray4 => _callArray4 = _callArray4 ?? new JsValue[4];
  25. public StringBuilder StringBuilder => _stringBuilder = _stringBuilder ?? new StringBuilder();
  26. public static ArrayExecutionContext Current => _executionContext.Value;
  27. }
  28. }