• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-core / finn.schiermer-andersen_89

04 Jun 2024 02:04PM UTC coverage: 90.651% (-0.03%) from 90.685%
finn.schiermer-andersen_89

Pull #7654

Evergreen

finnschiermer
optimized string cache gc
Pull Request #7654: Fsa/string interning

102644 of 180648 branches covered (56.82%)

1005 of 1125 new or added lines in 15 files covered. (89.33%)

154 existing lines in 21 files now uncovered.

217953 of 240431 relevant lines covered (90.65%)

7671710.15 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.33
/src/realm/string_compressor.hpp
1
/*************************************************************************
2
 *
3
 * Copyright 2016 Realm Inc.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 **************************************************************************/
18

19
#ifndef REALM_STRING_COMPRESSOR_HPP
20
#define REALM_STRING_COMPRESSOR_HPP
21

22
#include <realm/utilities.hpp>
23
#include <vector>
24

25
using CompressionSymbol = uint16_t;
26
using CompressedString = std::vector<CompressionSymbol>;
27
struct CompressedStringView {
28
    CompressionSymbol* data = 0;
29
    uint32_t size = 0;
NEW
30
    CompressedStringView() = default;
×
31
    CompressedStringView(CompressionSymbol* c_ptr, size_t s)
32
        : data(c_ptr)
1,998,771✔
33
        , size(uint32_t(s))
1,998,771✔
34
    {
4,068,156✔
35
    }
4,068,156✔
36
    explicit CompressedStringView(CompressedString& cs)
37
        : data(cs.data())
571,470✔
38
        , size(uint32_t(cs.size()))
571,470✔
39
    {
1,146,528✔
40
    }
1,146,528✔
41
    bool operator==(CompressedStringView& other)
42
    {
1,146,525✔
43
        if (size != other.size)
1,146,525✔
NEW
44
            return false;
×
45
        for (size_t i = 0; i < size; ++i) {
1,011,134,799✔
46
            if (data[i] != other.data[i])
1,009,988,274✔
NEW
47
                return false;
×
48
        }
1,009,988,274✔
49
        return true;
1,146,525✔
50
    }
1,146,525✔
51
};
52

53
namespace realm {
54

55
class ArrayUnsigned;
56
class Array;
57
class Allocator;
58

59
class StringCompressor {
60
public:
61
    StringCompressor(Allocator& alloc, Array& parent, size_t index, bool writable);
62
    void refresh(bool writable);
63
    ~StringCompressor();
64

65
    int compare(CompressedStringView& A, CompressedStringView& B);
66
    int compare(StringData sd, CompressedStringView& B);
67

68
    CompressedString compress(StringData, bool learn);
69
    std::string decompress(CompressedStringView& c_str);
70

71
private:
72
    struct SymbolDef {
73
        CompressionSymbol id = 0;
74
        CompressionSymbol expansion_a = 0;
75
        CompressionSymbol expansion_b = 0;
76
    };
77

78
    struct ExpandedSymbolDef {
79
        SymbolDef def;
80
        std::string_view expansion;
81
        // ^ points into storage managed by m_expansion_storage
82
        // we need the following 2 values to facilitate rollback of allocated storage
83
        uint32_t storage_index;  // index into m_expansion_storage
84
        uint32_t storage_offset; // offset into block.
85
    };
86

87
    void rebuild_internal();
88
    void expand_compression_map();
89
    void add_expansion(SymbolDef def);
90
    std::vector<ExpandedSymbolDef> m_symbols; // map from symbol -> symbolpair, 2 elements pr entry
91
    std::vector<SymbolDef> m_compression_map; // perfect hash from symbolpair to its symbol
92

93
    std::unique_ptr<ArrayUnsigned> m_data;
94
    constexpr static size_t storage_chunk_size = 4096;
95
    std::vector<std::string> m_expansion_storage;
96
};
97

98
} // namespace realm
99

100
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc