Skip to content

Commit 22d15d9

Browse files
authored
No Unique PTRs for string interner and string compressor (#7807)
* No unique ptrs for string interner + limit number of interners * point fix client-reset test * code review
1 parent 9f4d51c commit 22d15d9

File tree

10 files changed

+150
-179
lines changed

10 files changed

+150
-179
lines changed

src/realm/array.cpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -314,18 +314,6 @@ void Array::destroy_children(size_t offset, bool ro_only) noexcept
314314
}
315315
}
316316

317-
// size_t Array::get_byte_size() const noexcept
318-
//{
319-
// const auto header = get_header();
320-
// auto num_bytes = get_byte_size_from_header(header);
321-
// auto read_only = m_alloc.is_read_only(m_ref) == true;
322-
// auto capacity = get_capacity_from_header(header);
323-
// auto bytes_ok = num_bytes <= capacity;
324-
// REALM_ASSERT(read_only || bytes_ok);
325-
// REALM_ASSERT_7(m_alloc.is_read_only(m_ref), ==, true, ||, num_bytes, <=, get_capacity_from_header(header));
326-
// return num_bytes;
327-
// }
328-
329317
ref_type Array::do_write_shallow(_impl::ArrayWriterBase& out) const
330318
{
331319
// here we might want to compress the array and write down.
@@ -607,14 +595,6 @@ void Array::do_ensure_minimum_width(int_fast64_t value)
607595
}
608596
}
609597

610-
size_t Array::size() const noexcept
611-
{
612-
// in case the array is in compressed format. Never read directly
613-
// from the header the size, since it will result very likely in a cache miss.
614-
// For compressed arrays m_size should always be kept updated, due to init_from_mem
615-
return m_size;
616-
}
617-
618598
bool Array::compress_array(Array& arr) const
619599
{
620600
if (m_integer_compressor.get_encoding() == NodeHeader::Encoding::WTypBits) {

src/realm/array.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,6 @@ class Array : public Node, public ArrayParent {
210210
update_width_cache_from_header();
211211
}
212212

213-
size_t size() const noexcept;
214-
215213
bool is_empty() const noexcept
216214
{
217215
return size() == 0;

src/realm/array_string.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <realm/array_string.hpp>
2020
#include <realm/impl/array_writer.hpp>
21+
#include <realm/string_interner.hpp>
2122
#include <realm/spec.hpp>
2223
#include <realm/mixed.hpp>
2324

@@ -549,5 +550,4 @@ ref_type ArrayString::write(_impl::ArrayWriterBase& out, StringInterner* interne
549550
auto retval = interned.write(out, false, false, out.compress);
550551
interned.destroy();
551552
return retval;
552-
// return m_arr->write(out, true, false, false);
553553
}

src/realm/node.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
#include <realm/node_header.hpp>
2323
#include <realm/alloc.hpp>
24-
#include <realm/string_interner.hpp>
2524

2625
#include <iostream>
2726

@@ -352,6 +351,7 @@ class ArrayWriterBase;
352351
}
353352

354353
/// Base class for all nodes holding user data
354+
class StringInterner;
355355
class ArrayPayload {
356356
public:
357357
virtual ~ArrayPayload();

src/realm/string_compressor.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,35 @@
1717
**************************************************************************/
1818

1919
#include <realm/string_compressor.hpp>
20+
#include <realm/string_interner.hpp>
2021
#include <realm/string_data.hpp>
2122

22-
#include <realm/array_unsigned.hpp>
23-
2423
#include <iostream>
2524
namespace realm {
2625

2726
StringCompressor::StringCompressor(Allocator& alloc, Array& parent, size_t index, bool writable)
27+
: m_data(alloc)
2828
{
2929
m_compression_map.resize(16); // start with a very small compression map
3030
m_symbols.reserve(65536);
31-
m_data = std::make_unique<ArrayUnsigned>(alloc);
32-
m_data->set_parent(&parent, index);
31+
m_data.set_parent(&parent, index);
3332
refresh(writable);
3433
}
3534

3635
void StringCompressor::refresh(bool writable)
3736
{
3837
// we assume that compressors are only created from a valid parent.
3938
// String interners in 'dead' mode should never instantiate a string compressor.
40-
if (m_data->get_ref_from_parent() == 0) {
39+
if (m_data.get_ref_from_parent() == 0) {
4140
REALM_ASSERT(writable);
42-
m_data->create(0, 65535);
43-
m_data->update_parent();
41+
m_data.create(0, 65535);
42+
m_data.update_parent();
4443
}
4544
else {
46-
if (m_data->is_attached())
47-
m_data->update_from_parent();
45+
if (m_data.is_attached())
46+
m_data.update_from_parent();
4847
else
49-
m_data->init_from_ref(m_data->get_ref_from_parent());
48+
m_data.init_from_ref(m_data.get_ref_from_parent());
5049
}
5150
rebuild_internal();
5251
}
@@ -111,7 +110,7 @@ void StringCompressor::expand_compression_map()
111110

112111
void StringCompressor::rebuild_internal()
113112
{
114-
auto num_symbols = m_data->size();
113+
auto num_symbols = m_data.size();
115114
if (num_symbols == m_symbols.size())
116115
return;
117116
if (num_symbols < m_symbols.size()) {
@@ -132,7 +131,7 @@ void StringCompressor::rebuild_internal()
132131
}
133132
// we have new symbols to add
134133
for (size_t i = m_symbols.size(); i < num_symbols; ++i) {
135-
auto pair = m_data->get(i);
134+
auto pair = m_data.get(i);
136135
SymbolDef def;
137136
def.id = (CompressionSymbol)(i + 256);
138137
def.expansion_a = 0xFFFF & (pair >> 16);
@@ -198,13 +197,13 @@ CompressedString StringCompressor::compress(StringData sd, bool learn)
198197
if (m_symbols.size() < (65536 - 256) && learn) {
199198
// define a new symbol for this entry and use it.
200199
REALM_ASSERT_DEBUG(m_compression_map[hash].id == 0);
201-
REALM_ASSERT_DEBUG(m_symbols.size() == m_data->size());
202-
REALM_ASSERT_DEBUG(m_data->is_attached());
200+
REALM_ASSERT_DEBUG(m_symbols.size() == m_data.size());
201+
REALM_ASSERT_DEBUG(m_data.is_attached());
203202
CompressionSymbol id = (CompressionSymbol)(256 + m_symbols.size());
204203
SymbolDef def{id, from[0], from[1]};
205204
m_compression_map[hash] = def;
206205
add_expansion(def);
207-
m_data->add(((uint64_t)from[0]) << 16 | from[1]);
206+
m_data.add(((uint64_t)from[0]) << 16 | from[1]);
208207
// std::cerr << id << " = {" << from[0] << ", " << from[1] << "}" << std::endl;
209208
*to++ = id;
210209
from += 2;

src/realm/string_compressor.hpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
#ifndef REALM_STRING_COMPRESSOR_HPP
2020
#define REALM_STRING_COMPRESSOR_HPP
2121

22+
#include <realm/array_unsigned.hpp>
2223
#include <realm/utilities.hpp>
2324
#include <vector>
2425

2526
using CompressionSymbol = uint16_t;
2627
using CompressedString = std::vector<CompressionSymbol>;
28+
2729
struct CompressedStringView {
2830
CompressionSymbol* data = 0;
2931
uint32_t size = 0;
@@ -51,11 +53,6 @@ struct CompressedStringView {
5153
};
5254

5355
namespace realm {
54-
55-
class ArrayUnsigned;
56-
class Array;
57-
class Allocator;
58-
5956
class StringCompressor {
6057
public:
6158
StringCompressor(Allocator& alloc, Array& parent, size_t index, bool writable);
@@ -90,7 +87,7 @@ class StringCompressor {
9087
std::vector<ExpandedSymbolDef> m_symbols; // map from symbol -> symbolpair, 2 elements pr entry
9188
std::vector<SymbolDef> m_compression_map; // perfect hash from symbolpair to its symbol
9289

93-
std::unique_ptr<ArrayUnsigned> m_data;
90+
ArrayUnsigned m_data;
9491
constexpr static size_t storage_chunk_size = 4096;
9592
std::vector<std::string> m_expansion_storage;
9693
};

0 commit comments

Comments
 (0)