aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/datastore/unique_store_string_allocator.cpp
blob: 9f2105b7f087f2520cd40db74c2a57845bf8d232 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "unique_store_string_allocator.hpp"
#include "buffer_type.hpp"
#include <vespa/vespalib/util/size_literals.h>

namespace vespalib::datastore {

namespace {

constexpr size_t NUM_ARRAYS_FOR_NEW_UNIQUESTORE_BUFFER = 1_Ki;
constexpr float ALLOC_GROW_FACTOR = 0.2;

}

namespace string_allocator {

std::vector<size_t> array_sizes = { 16, 24, 32, 40, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 256 };

const size_t small_string_entry_value_offset = UniqueStoreSmallStringEntry().value_offset();

uint32_t
get_type_id(size_t string_len)
{
    auto len =  small_string_entry_value_offset + string_len + 1;
    auto itr = std::lower_bound(array_sizes.cbegin(), array_sizes.cend(), len);
    if (itr != array_sizes.end()) {
        return itr - array_sizes.cbegin() + 1;
    } else {
        return 0;
    }
}

}

UniqueStoreSmallStringBufferType::UniqueStoreSmallStringBufferType(uint32_t array_size, uint32_t max_arrays, std::shared_ptr<vespalib::alloc::MemoryAllocator> memory_allocator)
    : BufferType<char>(array_size, 2u, max_arrays, NUM_ARRAYS_FOR_NEW_UNIQUESTORE_BUFFER, ALLOC_GROW_FACTOR),
      _memory_allocator(std::move(memory_allocator))
{
}

UniqueStoreSmallStringBufferType::~UniqueStoreSmallStringBufferType() = default;

void
UniqueStoreSmallStringBufferType::destroy_entries(void *, EntryCount)
{
    static_assert(std::is_trivially_destructible<UniqueStoreSmallStringEntry>::value,
                  "UniqueStoreSmallStringEntry must be trivially destructable");
}

void
UniqueStoreSmallStringBufferType::fallback_copy(void *newBuffer, const void *oldBuffer, EntryCount num_entries)
{
    static_assert(std::is_trivially_copyable<UniqueStoreSmallStringEntry>::value,
                  "UniqueStoreSmallStringEntry must be trivially copyable");
    if (num_entries > 0) {
        memcpy(newBuffer, oldBuffer, num_entries * getArraySize());
    }
}

void
UniqueStoreSmallStringBufferType::clean_hold(void *buffer, size_t offset, EntryCount num_entries, CleanContext)
{
    size_t array_size = getArraySize();
    void *e = static_cast<char *>(buffer) + offset * array_size;
    void *e_end = static_cast<char *>(e) + num_entries * array_size;
    while (e < e_end) {
        static_cast<UniqueStoreSmallStringEntry *>(e)->clean_hold(array_size);
        e = static_cast<char *>(e) + array_size;
    }
    assert(e == e_end);
}

const vespalib::alloc::MemoryAllocator*
UniqueStoreSmallStringBufferType::get_memory_allocator() const
{
    return _memory_allocator.get();
}

UniqueStoreExternalStringBufferType::UniqueStoreExternalStringBufferType(uint32_t array_size, uint32_t max_arrays, std::shared_ptr<vespalib::alloc::MemoryAllocator> memory_allocator)
    : BufferType<UniqueStoreEntry<std::string>>(array_size, 0u, max_arrays, NUM_ARRAYS_FOR_NEW_UNIQUESTORE_BUFFER, ALLOC_GROW_FACTOR),
      _memory_allocator(std::move(memory_allocator))
{
}

UniqueStoreExternalStringBufferType::~UniqueStoreExternalStringBufferType() = default;

void
UniqueStoreExternalStringBufferType::clean_hold(void *buffer, size_t offset, EntryCount num_entries, CleanContext cleanCtx)
{
    UniqueStoreEntry<std::string> *elem = static_cast<UniqueStoreEntry<std::string> *>(buffer) + offset;
    for (size_t i = 0; i < num_entries; ++i) {
        cleanCtx.extraBytesCleaned(elem->value().size() + 1);
        std::string().swap(elem->value());
        ++elem;
    }
}

const vespalib::alloc::MemoryAllocator*
UniqueStoreExternalStringBufferType::get_memory_allocator() const
{
    return _memory_allocator.get();
}

template class UniqueStoreStringAllocator<EntryRefT<22>>;
template class BufferType<UniqueStoreEntry<std::string>>;

}