aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/binsaver/mem_io.h
blob: 2a9e36fe681820aa183bd8d94fce9b5f0017a8d0 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#pragma once

#include "bin_saver.h"

namespace NMemIoInternals {
    class TMemoryStream: public IBinaryStream {
        TVector<char>& Data;
        ui64 Pos;

    public:
        TMemoryStream(TVector<char>* data, ui64 pos = 0)
            : Data(*data)
            , Pos(pos)
        {
        }
        ~TMemoryStream() override {
        } // keep gcc happy

        bool IsValid() const override {
            return true;
        }
        bool IsFailed() const override {
            return false;
        }

    private:
        int WriteImpl(const void* userBuffer, int size) override {
            if (size == 0)
                return 0;
            Y_ASSERT(size > 0);
            if (Pos + size > Data.size())
                Data.yresize(Pos + size);
            memcpy(&Data[Pos], userBuffer, size);
            Pos += size;
            return size;
        }
        int ReadImpl(void* userBuffer, int size) override {
            if (size == 0)
                return 0;
            Y_ASSERT(size > 0);
            int res = Min(Data.size() - Pos, (ui64)size);
            if (res)
                memcpy(userBuffer, &Data[Pos], res);
            Pos += res;
            return res;
        }
    };

    template <class T>
    inline void SerializeMem(bool bRead, TVector<char>* data, T& c, bool stableOutput = false) {
        if (IBinSaver::HasNonTrivialSerializer<T>(0u)) {
            TMemoryStream f(data);
            {
                IBinSaver bs(f, bRead, stableOutput);
                bs.Add(1, &c);
            }
        } else {
            if (bRead) {
                Y_ASSERT(data->size() == sizeof(T));
                c = *reinterpret_cast<T*>(&(*data)[0]);
            } else {
                data->yresize(sizeof(T));
                *reinterpret_cast<T*>(&(*data)[0]) = c;
            }
        }
    }

    ////////////////////////////////////////////////////////////////////////////
    class THugeMemoryStream: public IBinaryStream {
        TVector<TVector<char>>& Data;
        i64 Block, Pos;
        bool ShrinkOnRead;

        enum {
            MAX_BLOCK_SIZE = 1024 * 1024 // Aligned with cache size
        };

    public:
        THugeMemoryStream(TVector<TVector<char>>* data, bool shrinkOnRead = false)
            : Data(*data)
            , Block(0)
            , Pos(0)
            , ShrinkOnRead(shrinkOnRead)
        {
            Y_ASSERT(!data->empty());
        }

        ~THugeMemoryStream() override {
        } // keep gcc happy

        bool IsValid() const override {
            return true;
        }
        bool IsFailed() const override {
            return false;
        }

    private:
        int WriteImpl(const void* userDataArg, int sizeArg) override {
            if (sizeArg == 0)
                return 0;
            const char* userData = (const char*)userDataArg;
            i64 size = sizeArg;
            i64 newSize = Pos + size;
            if (newSize > Data[Block].ysize()) {
                while (newSize > MAX_BLOCK_SIZE) {
                    int maxWrite = MAX_BLOCK_SIZE - Pos;
                    Data[Block].yresize(MAX_BLOCK_SIZE);
                    if (maxWrite) {
                        memcpy(&Data[Block][Pos], userData, maxWrite);
                        userData += maxWrite;
                        size -= maxWrite;
                    }
                    ++Block;
                    Pos = 0;
                    Data.resize(Block + 1);
                    newSize = Pos + size;
                }
                Data[Block].yresize(newSize);
            }
            if (size) {
                memcpy(&Data[Block][Pos], userData, size);
            }
            Pos += size;
            return sizeArg;
        }
        int ReadImpl(void* userDataArg, int sizeArg) override {
            if (sizeArg == 0)
                return 0;

            char* userData = (char*)userDataArg;
            i64 size = sizeArg;
            i64 rv = 0;
            while (size > 0) {
                int curBlockSize = Data[Block].ysize();
                int maxRead = 0;
                if (Pos + size > curBlockSize) {
                    maxRead = curBlockSize - Pos;
                    if (maxRead) {
                        memcpy(userData, &Data[Block][Pos], maxRead);
                        userData += maxRead;
                        size -= maxRead;
                        rv += maxRead;
                    }
                    if (Block + 1 == Data.ysize()) {
                        memset(userData, 0, size);
                        return rv;
                    }
                    if (ShrinkOnRead) {
                        TVector<char>().swap(Data[Block]);
                    }
                    ++Block;
                    Pos = 0;
                } else {
                    memcpy(userData, &Data[Block][Pos], size);
                    Pos += size;
                    rv += size;
                    return rv;
                }
            }
            return rv;
        }
    };

    template <class T>
    inline void SerializeMem(bool bRead, TVector<TVector<char>>* data, T& c, bool stableOutput = false) {
        if (data->empty()) {
            data->resize(1);
        }
        THugeMemoryStream f(data);
        {
            IBinSaver bs(f, bRead, stableOutput);
            bs.Add(1, &c);
        }
    }
}

template <class T>
inline void SerializeMem(const TVector<char>& data, T& c) {
    if (IBinSaver::HasNonTrivialSerializer<T>(0u)) {
        TVector<char> tmp(data);
        SerializeFromMem(&tmp, c);
    } else {
        Y_ASSERT(data.size() == sizeof(T));
        c = *reinterpret_cast<const T*>(&data[0]);
    }
}

template <class T, class D>
inline void SerializeToMem(D* data, T& c, bool stableOutput = false) {
    NMemIoInternals::SerializeMem(false, data, c, stableOutput);
}

template <class T, class D>
inline void SerializeFromMem(D* data, T& c, bool stableOutput = false) {
    NMemIoInternals::SerializeMem(true, data, c, stableOutput);
}

// Frees memory in (*data)[i] immediately upon it's deserialization, thus keeps low overall memory consumption for data + object.
template <class T>
inline void SerializeFromMemShrinkInput(TVector<TVector<char>>* data, T& c) {
    if (data->empty()) {
        data->resize(1);
    }
    NMemIoInternals::THugeMemoryStream f(data, true);
    {
        IBinSaver bs(f, true, false);
        bs.Add(1, &c);
    }
    data->resize(0);
    data->shrink_to_fit();
}