stringBufferUtils.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * @returns {void}
  22. */
  23. function enableDualStringBufferCaching() {
  24. dualStringBufferCaching = true;
  25. }
  26. /**
  27. * Disables the optimization to save both string and buffer in memory. This
  28. * may increase performance but should reduce memory usage in the Webpack
  29. * compiler.
  30. * @returns {void}
  31. */
  32. function disableDualStringBufferCaching() {
  33. dualStringBufferCaching = false;
  34. }
  35. const interningStringMap = new Map();
  36. let enableStringInterningRefCount = 0;
  37. /**
  38. * @returns {boolean} value
  39. */
  40. function isStringInterningEnabled() {
  41. return enableStringInterningRefCount > 0;
  42. }
  43. /**
  44. * Starts a memory optimization to avoid repeat copies of the same string in
  45. * memory by caching a single reference to the string. This can reduce memory
  46. * usage if the same string is repeated many times in the compiler, such as
  47. * when Webpack layers are used with the same files.
  48. *
  49. * {@link exitStringInterningRange} should be called when string interning is
  50. * no longer necessary to free up the memory used by the interned strings. If
  51. * {@link enterStringInterningRange} has been called multiple times, then
  52. * this method may not immediately free all the memory until
  53. * {@link exitStringInterningRange} has been called to end all string
  54. * interning ranges.
  55. * @returns {void}
  56. */
  57. function enterStringInterningRange() {
  58. enableStringInterningRefCount++;
  59. }
  60. /**
  61. * Stops the current string interning range. Once all string interning ranges
  62. * have been exited, this method will free all the memory used by the interned
  63. * strings. This method should be called once for each time that
  64. * {@link enterStringInterningRange} was called.
  65. * @returns {void}
  66. */
  67. function exitStringInterningRange() {
  68. if (--enableStringInterningRefCount <= 0) {
  69. interningStringMap.clear();
  70. enableStringInterningRefCount = 0;
  71. }
  72. }
  73. /**
  74. * Saves the string in a map to ensure that only one copy of the string exists
  75. * in memory at a given time. This is controlled by {@link enableStringInterning}
  76. * and {@link disableStringInterning}. Callers are expect to manage the memory
  77. * of the interned strings by calling {@link disableStringInterning} after the
  78. * compiler no longer needs to save the interned memory.
  79. * @param {string} str A string to be interned.
  80. * @returns {string} The original string or a reference to an existing string of the same value if it has already been interned.
  81. */
  82. function internString(str) {
  83. if (
  84. !isStringInterningEnabled() ||
  85. !str ||
  86. str.length < 128 ||
  87. typeof str !== "string"
  88. ) {
  89. return str;
  90. }
  91. let internedString = interningStringMap.get(str);
  92. if (internedString === undefined) {
  93. internedString = str;
  94. interningStringMap.set(str, internedString);
  95. }
  96. return internedString;
  97. }
  98. module.exports = {
  99. disableDualStringBufferCaching,
  100. enableDualStringBufferCaching,
  101. internString,
  102. isDualStringBufferCachingEnabled,
  103. enterStringInterningRange,
  104. exitStringInterningRange,
  105. };