From ce19193172a9645ba2f7b9a4d0de83898f6e2332 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Edelbo?= Date: Thu, 22 Aug 2024 13:43:53 +0200 Subject: [PATCH] Optimize size of Node and Array The size of Node and Array decresed from 64 and 128 to 56 and 112. Some redundant code removed in Spec class. --- src/realm/array.hpp | 2 +- src/realm/node.hpp | 8 ++------ src/realm/search_index.hpp | 6 ------ src/realm/spec.cpp | 37 +++++++++---------------------------- src/realm/spec.hpp | 6 ------ src/realm/table.hpp | 8 -------- 6 files changed, 12 insertions(+), 55 deletions(-) diff --git a/src/realm/array.hpp b/src/realm/array.hpp index af6fee1efa6..54cec41b774 100644 --- a/src/realm/array.hpp +++ b/src/realm/array.hpp @@ -532,10 +532,10 @@ class Array : public Node, public ArrayParent { Getter m_getter = nullptr; // cached to avoid indirection const VTable* m_vtable = nullptr; - uint_least8_t m_width = 0; // Size of an element (meaning depend on type of array). int64_t m_lbound; // min number that can be stored with current m_width int64_t m_ubound; // max number that can be stored with current m_width + uint8_t m_width = 0; // Size of an element (meaning depend on type of array). bool m_is_inner_bptree_node; // This array is an inner node of B+-tree. bool m_has_refs; // Elements whose first bit is zero are refs to subarrays. bool m_context_flag; // Meaning depends on context. diff --git a/src/realm/node.hpp b/src/realm/node.hpp index 8bea1c9559d..8c4290b4b9f 100644 --- a/src/realm/node.hpp +++ b/src/realm/node.hpp @@ -243,11 +243,7 @@ class Node : public NodeHeader { void set_parent(ArrayParent* parent, size_t ndx_in_parent) noexcept { m_parent = parent; - m_ndx_in_parent = ndx_in_parent; - } - void set_ndx_in_parent(size_t ndx) noexcept - { - m_ndx_in_parent = ndx; + m_ndx_in_parent = unsigned(ndx_in_parent); } void clear_missing_parent_update() @@ -339,7 +335,7 @@ class Node : public NodeHeader { private: friend class NodeTree; ArrayParent* m_parent = nullptr; - size_t m_ndx_in_parent = 0; // Ignored if m_parent is null. + unsigned m_ndx_in_parent = 0; // Ignored if m_parent is null. bool m_missing_parent_update = false; void do_copy_on_write(size_t minimum_size = 0); diff --git a/src/realm/search_index.hpp b/src/realm/search_index.hpp index 4848e9b89c3..4c040b8f85e 100644 --- a/src/realm/search_index.hpp +++ b/src/realm/search_index.hpp @@ -116,7 +116,6 @@ class SearchIndex { bool is_attached() const noexcept; void set_parent(ArrayParent* parent, size_t ndx_in_parent) noexcept; size_t get_ndx_in_parent() const noexcept; - void set_ndx_in_parent(size_t ndx_in_parent) noexcept; void update_from_parent() noexcept; void refresh_accessor_tree(const ClusterColumn& target_column); ref_type get_ref() const noexcept; @@ -174,11 +173,6 @@ inline size_t SearchIndex::get_ndx_in_parent() const noexcept return m_root_array->get_ndx_in_parent(); } -inline void SearchIndex::set_ndx_in_parent(size_t ndx_in_parent) noexcept -{ - m_root_array->set_ndx_in_parent(ndx_in_parent); -} - inline void SearchIndex::update_from_parent() noexcept { m_root_array->update_from_parent(); diff --git a/src/realm/spec.cpp b/src/realm/spec.cpp index b2746f3c1c2..d14e6e50bbf 100644 --- a/src/realm/spec.cpp +++ b/src/realm/spec.cpp @@ -41,16 +41,13 @@ void Spec::init(MemRef mem) noexcept { m_top.init_from_mem(mem); size_t top_size = m_top.size(); - REALM_ASSERT(top_size > s_attributes_ndx && top_size <= s_spec_max_size); + // Since Core6 we will always have the column keys array + REALM_ASSERT(top_size == s_spec_max_size); m_types.init_from_ref(m_top.get_as_ref(s_types_ndx)); m_names.init_from_ref(m_top.get_as_ref(s_names_ndx)); m_attr.init_from_ref(m_top.get_as_ref(s_attributes_ndx)); - while (m_top.size() < s_spec_max_size) { - m_top.add(0); - } - // Enumkeys array is only there when there are StringEnum columns if (auto ref = m_top.get_as_ref(s_enum_keys_ndx)) { m_enumkeys.init_from_ref(ref); @@ -59,34 +56,22 @@ void Spec::init(MemRef mem) noexcept m_enumkeys.detach(); } - if (m_top.get_as_ref(s_col_keys_ndx) == 0) { - // This is an upgrade - create column key array - MemRef mem_ref = Array::create_empty_array(Array::type_Normal, false, m_top.get_alloc()); // Throws - m_keys.init_from_mem(mem_ref); - m_keys.update_parent(); - size_t num_cols = m_types.size(); - for (size_t i = 0; i < num_cols; i++) { - m_keys.add(i); - } - } - else { - m_keys.init_from_parent(); - } - + m_keys.init_from_parent(); update_internals(); } void Spec::update_internals() noexcept { - m_num_public_columns = 0; size_t n = m_types.size(); - for (size_t i = 0; i < n; ++i) { - if (ColumnType(int(m_types.get(i))) == col_type_BackLink) { - // Now we have no more public columns + m_num_public_columns = n; + // We normally have fewer backlink columns than public columns, so quicker to go backwards + for (size_t i = n; i; --i) { + if (ColumnType(int(m_types.get(i - 1))) != col_type_BackLink) { + // Now we have no more backlink columns. The rest must be public return; } - m_num_public_columns++; + m_num_public_columns--; } } @@ -207,8 +192,6 @@ void Spec::insert_column(size_t column_ndx, ColKey col_key, ColumnType type, Str if (m_enumkeys.is_attached() && type != col_type_BackLink) { m_enumkeys.insert(column_ndx, 0); } - - update_internals(); } void Spec::erase_column(size_t column_ndx) @@ -246,8 +229,6 @@ void Spec::erase_column(size_t column_ndx) m_types.erase(column_ndx); // Throws m_attr.erase(column_ndx); // Throws m_keys.erase(column_ndx); - - update_internals(); } void Spec::upgrade_string_to_enum(size_t column_ndx, ref_type keys_ref) diff --git a/src/realm/spec.hpp b/src/realm/spec.hpp index c9f3ff0c230..bd0d830da3c 100644 --- a/src/realm/spec.hpp +++ b/src/realm/spec.hpp @@ -81,7 +81,6 @@ class Spec { void destroy() noexcept; size_t get_ndx_in_parent() const noexcept; - void set_ndx_in_parent(size_t) noexcept; void verify() const; @@ -177,11 +176,6 @@ inline size_t Spec::get_ndx_in_parent() const noexcept return m_top.get_ndx_in_parent(); } -inline void Spec::set_ndx_in_parent(size_t ndx) noexcept -{ - m_top.set_ndx_in_parent(ndx); -} - inline ref_type Spec::get_ref() const noexcept { return m_top.get_ref(); diff --git a/src/realm/table.hpp b/src/realm/table.hpp index 80496b12fb9..b5e19db851b 100644 --- a/src/realm/table.hpp +++ b/src/realm/table.hpp @@ -838,8 +838,6 @@ class Table { util::Logger* get_logger() const noexcept; - void set_ndx_in_parent(size_t ndx_in_parent) noexcept; - /// Refresh the part of the accessor tree that is rooted at this /// table. void refresh_accessor_tree(); @@ -1340,12 +1338,6 @@ inline bool Table::is_link_type(ColumnType col_type) noexcept return col_type == col_type_Link; } -inline void Table::set_ndx_in_parent(size_t ndx_in_parent) noexcept -{ - REALM_ASSERT(m_top.is_attached()); - m_top.set_ndx_in_parent(ndx_in_parent); -} - inline size_t Table::colkey2spec_ndx(ColKey key) const { auto leaf_idx = key.get_index();