IndexedProperties.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/QuickSort.h>
  7. #include <LibJS/Runtime/Accessor.h>
  8. #include <LibJS/Runtime/IndexedProperties.h>
  9. namespace JS {
  10. constexpr size_t const SPARSE_ARRAY_HOLE_THRESHOLD = 200;
  11. constexpr size_t const LENGTH_SETTER_GENERIC_STORAGE_THRESHOLD = 4 * MiB;
  12. SimpleIndexedPropertyStorage::SimpleIndexedPropertyStorage(Vector<Value>&& initial_values)
  13. : IndexedPropertyStorage(IsSimpleStorage::Yes)
  14. , m_array_size(initial_values.size())
  15. , m_packed_elements(move(initial_values))
  16. {
  17. }
  18. bool SimpleIndexedPropertyStorage::has_index(u32 index) const
  19. {
  20. return inline_has_index(index);
  21. }
  22. Optional<ValueAndAttributes> SimpleIndexedPropertyStorage::get(u32 index) const
  23. {
  24. return inline_get(index);
  25. }
  26. void SimpleIndexedPropertyStorage::grow_storage_if_needed()
  27. {
  28. if (m_array_size <= m_packed_elements.size())
  29. return;
  30. if (m_array_size <= m_packed_elements.capacity()) {
  31. m_packed_elements.resize_and_keep_capacity(m_array_size);
  32. } else {
  33. // When the array is actually full grow storage by 25% at a time.
  34. m_packed_elements.resize_and_keep_capacity(m_array_size + (m_array_size / 4));
  35. }
  36. }
  37. void SimpleIndexedPropertyStorage::put(u32 index, Value value, PropertyAttributes attributes)
  38. {
  39. VERIFY(attributes == default_attributes);
  40. if (index >= m_array_size) {
  41. m_array_size = index + 1;
  42. grow_storage_if_needed();
  43. }
  44. m_packed_elements[index] = value;
  45. }
  46. void SimpleIndexedPropertyStorage::remove(u32 index)
  47. {
  48. VERIFY(index < m_array_size);
  49. m_packed_elements[index] = {};
  50. }
  51. ValueAndAttributes SimpleIndexedPropertyStorage::take_first()
  52. {
  53. m_array_size--;
  54. return { m_packed_elements.take_first(), default_attributes };
  55. }
  56. ValueAndAttributes SimpleIndexedPropertyStorage::take_last()
  57. {
  58. m_array_size--;
  59. auto last_element = m_packed_elements[m_array_size];
  60. m_packed_elements[m_array_size] = {};
  61. return { last_element, default_attributes };
  62. }
  63. bool SimpleIndexedPropertyStorage::set_array_like_size(size_t new_size)
  64. {
  65. m_array_size = new_size;
  66. m_packed_elements.resize_and_keep_capacity(new_size);
  67. return true;
  68. }
  69. GenericIndexedPropertyStorage::GenericIndexedPropertyStorage(SimpleIndexedPropertyStorage&& storage)
  70. : IndexedPropertyStorage(IsSimpleStorage::No)
  71. {
  72. m_array_size = storage.array_like_size();
  73. for (size_t i = 0; i < storage.m_packed_elements.size(); ++i) {
  74. auto value = storage.m_packed_elements[i];
  75. if (!value.is_empty())
  76. m_sparse_elements.set(i, { value, default_attributes });
  77. }
  78. }
  79. bool GenericIndexedPropertyStorage::has_index(u32 index) const
  80. {
  81. return m_sparse_elements.contains(index);
  82. }
  83. Optional<ValueAndAttributes> GenericIndexedPropertyStorage::get(u32 index) const
  84. {
  85. if (index >= m_array_size)
  86. return {};
  87. return m_sparse_elements.get(index);
  88. }
  89. void GenericIndexedPropertyStorage::put(u32 index, Value value, PropertyAttributes attributes)
  90. {
  91. if (index >= m_array_size)
  92. m_array_size = index + 1;
  93. m_sparse_elements.set(index, { value, attributes });
  94. }
  95. void GenericIndexedPropertyStorage::remove(u32 index)
  96. {
  97. VERIFY(index < m_array_size);
  98. m_sparse_elements.remove(index);
  99. }
  100. ValueAndAttributes GenericIndexedPropertyStorage::take_first()
  101. {
  102. VERIFY(m_array_size > 0);
  103. m_array_size--;
  104. auto indices = m_sparse_elements.keys();
  105. quick_sort(indices);
  106. auto it = m_sparse_elements.find(indices.first());
  107. auto first_element = it->value;
  108. m_sparse_elements.remove(it);
  109. return first_element;
  110. }
  111. ValueAndAttributes GenericIndexedPropertyStorage::take_last()
  112. {
  113. VERIFY(m_array_size > 0);
  114. m_array_size--;
  115. auto result = m_sparse_elements.get(m_array_size);
  116. if (!result.has_value())
  117. return {};
  118. m_sparse_elements.remove(m_array_size);
  119. return result.value();
  120. }
  121. bool GenericIndexedPropertyStorage::set_array_like_size(size_t new_size)
  122. {
  123. if (new_size == m_array_size)
  124. return true;
  125. if (new_size >= m_array_size) {
  126. m_array_size = new_size;
  127. return true;
  128. }
  129. bool any_failed = false;
  130. size_t highest_index = 0;
  131. HashMap<u32, ValueAndAttributes> new_sparse_elements;
  132. for (auto& entry : m_sparse_elements) {
  133. if (entry.key >= new_size) {
  134. if (entry.value.attributes.is_configurable())
  135. continue;
  136. else
  137. any_failed = true;
  138. }
  139. new_sparse_elements.set(entry.key, entry.value);
  140. highest_index = max(highest_index, entry.key);
  141. }
  142. if (any_failed)
  143. m_array_size = highest_index + 1;
  144. else
  145. m_array_size = new_size;
  146. m_sparse_elements = move(new_sparse_elements);
  147. return !any_failed;
  148. }
  149. IndexedPropertyIterator::IndexedPropertyIterator(IndexedProperties const& indexed_properties, u32 staring_index, bool skip_empty)
  150. : m_indexed_properties(indexed_properties)
  151. , m_index(staring_index)
  152. , m_skip_empty(skip_empty)
  153. {
  154. if (m_skip_empty) {
  155. m_cached_indices = m_indexed_properties.indices();
  156. skip_empty_indices();
  157. }
  158. }
  159. IndexedPropertyIterator& IndexedPropertyIterator::operator++()
  160. {
  161. m_index++;
  162. if (m_skip_empty)
  163. skip_empty_indices();
  164. return *this;
  165. }
  166. IndexedPropertyIterator& IndexedPropertyIterator::operator*()
  167. {
  168. return *this;
  169. }
  170. bool IndexedPropertyIterator::operator!=(IndexedPropertyIterator const& other) const
  171. {
  172. return m_index != other.m_index;
  173. }
  174. void IndexedPropertyIterator::skip_empty_indices()
  175. {
  176. for (size_t i = m_next_cached_index; i < m_cached_indices.size(); i++) {
  177. auto index = m_cached_indices[i];
  178. if (index < m_index)
  179. continue;
  180. m_index = index;
  181. m_next_cached_index = i + 1;
  182. return;
  183. }
  184. m_index = m_indexed_properties.array_like_size();
  185. }
  186. Optional<ValueAndAttributes> IndexedProperties::get(u32 index) const
  187. {
  188. if (!m_storage)
  189. return {};
  190. return m_storage->get(index);
  191. }
  192. void IndexedProperties::put(u32 index, Value value, PropertyAttributes attributes)
  193. {
  194. ensure_storage();
  195. if (m_storage->is_simple_storage() && (attributes != default_attributes || index > (array_like_size() + SPARSE_ARRAY_HOLE_THRESHOLD))) {
  196. switch_to_generic_storage();
  197. }
  198. m_storage->put(index, value, attributes);
  199. }
  200. void IndexedProperties::remove(u32 index)
  201. {
  202. VERIFY(m_storage);
  203. VERIFY(m_storage->has_index(index));
  204. m_storage->remove(index);
  205. }
  206. bool IndexedProperties::set_array_like_size(size_t new_size)
  207. {
  208. ensure_storage();
  209. auto current_array_like_size = array_like_size();
  210. // We can't use simple storage for lengths that don't fit in an i32.
  211. // Also, to avoid gigantic unused storage allocations, let's put an (arbitrary) 4M cap on simple storage here.
  212. // This prevents something like "a = []; a.length = 0x80000000;" from allocating 2G entries.
  213. if (m_storage->is_simple_storage()
  214. && (new_size > NumericLimits<i32>::max()
  215. || (current_array_like_size < LENGTH_SETTER_GENERIC_STORAGE_THRESHOLD && new_size > LENGTH_SETTER_GENERIC_STORAGE_THRESHOLD))) {
  216. switch_to_generic_storage();
  217. }
  218. return m_storage->set_array_like_size(new_size);
  219. }
  220. size_t IndexedProperties::real_size() const
  221. {
  222. if (!m_storage)
  223. return 0;
  224. if (m_storage->is_simple_storage()) {
  225. auto& packed_elements = static_cast<SimpleIndexedPropertyStorage const&>(*m_storage).elements();
  226. size_t size = 0;
  227. for (auto& element : packed_elements) {
  228. if (!element.is_empty())
  229. ++size;
  230. }
  231. return size;
  232. }
  233. return static_cast<GenericIndexedPropertyStorage const&>(*m_storage).size();
  234. }
  235. Vector<u32> IndexedProperties::indices() const
  236. {
  237. if (!m_storage)
  238. return {};
  239. if (m_storage->is_simple_storage()) {
  240. auto const& storage = static_cast<SimpleIndexedPropertyStorage const&>(*m_storage);
  241. auto const& elements = storage.elements();
  242. Vector<u32> indices;
  243. indices.ensure_capacity(storage.array_like_size());
  244. for (size_t i = 0; i < elements.size(); ++i) {
  245. if (!elements.at(i).is_empty())
  246. indices.unchecked_append(i);
  247. }
  248. return indices;
  249. }
  250. auto const& storage = static_cast<GenericIndexedPropertyStorage const&>(*m_storage);
  251. auto indices = storage.sparse_elements().keys();
  252. quick_sort(indices);
  253. return indices;
  254. }
  255. void IndexedProperties::switch_to_generic_storage()
  256. {
  257. if (!m_storage) {
  258. m_storage = make<GenericIndexedPropertyStorage>();
  259. return;
  260. }
  261. auto& storage = static_cast<SimpleIndexedPropertyStorage&>(*m_storage);
  262. m_storage = make<GenericIndexedPropertyStorage>(move(storage));
  263. }
  264. void IndexedProperties::ensure_storage()
  265. {
  266. if (!m_storage)
  267. m_storage = make<SimpleIndexedPropertyStorage>();
  268. }
  269. }