stringBufferUtils.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Mark Knichel @mknichel
  4. */
  5. "use strict";
  6. let dualStringBufferCaching = true;
  7. /**
  8. * @returns {boolean} Whether the optimization to cache copies of both the
  9. * string and buffer version of source content is enabled. This is enabled by
  10. * default to improve performance but can consume more memory since values are
  11. * stored twice.
  12. */
  13. function isDualStringBufferCachingEnabled() {
  14. return dualStringBufferCaching;
  15. }
  16. /**
  17. * Enables an optimization to save both string and buffer in memory to avoid
  18. * repeat conversions between the two formats when they are requested. This
  19. * is enabled by default. This option can improve performance but can consume
  20. * additional memory since values are stored twice.
  21. *
  22. * @returns {void}
  23. */
  24. function enableDualStringBufferCaching() {
  25. dualStringBufferCaching = true;
  26. }
  27. /**
  28. * Disables the optimization to save both string and buffer in memory. This
  29. * may increase performance but should reduce memory usage in the Webpack
  30. * compiler.
  31. *
  32. * @returns {void}
  33. */
  34. function disableDualStringBufferCaching() {
  35. dualStringBufferCaching = false;
  36. }
  37. const interningStringMap = new Map();
  38. /**
  39. * Saves the string in a map to ensure that only one copy of the string exists
  40. * in memory at a given time. This is controlled by {@link enableStringInterning}
  41. * and {@link disableStringInterning}. Callers are expect to manage the memory
  42. * of the interned strings by calling {@link disableStringInterning} after the
  43. * compiler no longer needs to save the interned memory.
  44. *
  45. * @param {string} str A string to be interned.
  46. * @returns {string} The original string or a reference to an existing string of the same value if it has already been interned.
  47. */
  48. function internString(str) {
  49. if (
  50. !isStringInterningEnabled() ||
  51. !str ||
  52. str.length < 128 ||
  53. typeof str !== "string"
  54. ) {
  55. return str;
  56. }
  57. let internedString = interningStringMap.get(str);
  58. if (internedString === undefined) {
  59. internedString = str;
  60. interningStringMap.set(str, internedString);
  61. }
  62. return internedString;
  63. }
  64. let enableStringInterningRefCount = 0;
  65. /**
  66. * @returns {boolean} value
  67. */
  68. function isStringInterningEnabled() {
  69. return enableStringInterningRefCount > 0;
  70. }
  71. /**
  72. * Starts a memory optimization to avoid repeat copies of the same string in
  73. * memory by caching a single reference to the string. This can reduce memory
  74. * usage if the same string is repeated many times in the compiler, such as
  75. * when Webpack layers are used with the same files.
  76. *
  77. * {@link exitStringInterningRange} should be called when string interning is
  78. * no longer necessary to free up the memory used by the interned strings. If
  79. * {@link enterStringInterningRange} has been called multiple times, then
  80. * this method may not immediately free all the memory until
  81. * {@link exitStringInterningRange} has been called to end all string
  82. * interning ranges.
  83. *
  84. * @returns {void}
  85. */
  86. function enterStringInterningRange() {
  87. enableStringInterningRefCount++;
  88. }
  89. /**
  90. * Stops the current string interning range. Once all string interning ranges
  91. * have been exited, this method will free all the memory used by the interned
  92. * strings. This method should be called once for each time that
  93. * {@link enterStringInterningRange} was called.
  94. *
  95. * @returns {void}
  96. */
  97. function exitStringInterningRange() {
  98. if (--enableStringInterningRefCount <= 0) {
  99. interningStringMap.clear();
  100. enableStringInterningRefCount = 0;
  101. }
  102. }
  103. module.exports = {
  104. disableDualStringBufferCaching,
  105. enableDualStringBufferCaching,
  106. internString,
  107. isDualStringBufferCachingEnabled,
  108. enterStringInterningRange,
  109. exitStringInterningRange
  110. };