IndexedProperties.cpp 8.5 KB

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