Skip to content

Commit

Permalink
Optimize size of Node and Array
Browse files Browse the repository at this point in the history
The size of Node and Array decresed from 64 and 128 to 56 and 112.
Some redundant code removed in Spec class.
  • Loading branch information
jedelbo committed Aug 22, 2024
1 parent 67f0fa2 commit ce19193
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/realm/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 2 additions & 6 deletions src/realm/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 0 additions & 6 deletions src/realm/search_index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
37 changes: 9 additions & 28 deletions src/realm/spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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--;
}
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 0 additions & 6 deletions src/realm/spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down
8 changes: 0 additions & 8 deletions src/realm/table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit ce19193

Please sign in to comment.