aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/interconnect/outgoing_stream.h
blob: 0306295b52148d80cd0d82af7c7ee80e1c7eeb8b (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
213
214
215
216
217
218
219
220
221
222
223
224
#pragma once

#include <library/cpp/actors/core/event_load.h>
#include <library/cpp/actors/util/rc_buf.h>
#include <library/cpp/containers/stack_vector/stack_vec.h>
#include <deque>

namespace NInterconnect {

    template<size_t TotalSize>
    class TOutgoingStreamT {
        static constexpr size_t BufferSize = TotalSize - sizeof(ui32) * 2;

        struct TBuffer {
            char Data[BufferSize];
            ui32 RefCount;
            ui32 Index;

            struct TDeleter {
                void operator ()(TBuffer *buffer) const {
                    free(buffer);
                }
            };
        };

        static_assert(sizeof(TBuffer) == TotalSize);

        struct TSendChunk {
            TContiguousSpan Span;
            TBuffer *Buffer;
        };

        std::vector<std::unique_ptr<TBuffer, typename TBuffer::TDeleter>> Buffers;
        TBuffer *AppendBuffer = nullptr;
        size_t AppendOffset = BufferSize; // into the last buffer
        std::deque<TSendChunk> SendQueue;
        size_t SendQueuePos = 0;
        size_t SendOffset = 0;

    public:
        size_t CalculateOutgoingSize() const {
            size_t res = 0;
            for (const TSendChunk& chunk : SendQueue) {
                res += chunk.Span.size();
            }
            return res;
        }

        size_t CalculateUnsentSize() const {
            size_t res = 0;
            for (auto it = SendQueue.begin() + SendQueuePos; it != SendQueue.end(); ++it) {
                res += it->Span.size();
            }
            return res - SendOffset;
        }

        TMutableContiguousSpan AcquireSpanForWriting(size_t maxLen) {
            if (AppendOffset == BufferSize) { // we have no free buffer, allocate one
                Buffers.emplace_back(static_cast<TBuffer*>(malloc(sizeof(TBuffer))));
                AppendBuffer = Buffers.back().get();
                Y_VERIFY(AppendBuffer);
                AppendBuffer->RefCount = 1; // through AppendBuffer pointer
                AppendBuffer->Index = Buffers.size() - 1;
                AppendOffset = 0;
            }
            return {AppendBuffer->Data + AppendOffset, Min(maxLen, BufferSize - AppendOffset)};
        }

        void Append(TContiguousSpan span) {
            TBuffer *buffer = nullptr;
            if (AppendBuffer && span.data() == AppendBuffer->Data + AppendOffset) { // the only valid case to use previously acquired span
                buffer = AppendBuffer;
                AppendOffset += span.size();
                Y_VERIFY_DEBUG(AppendOffset <= BufferSize);
                if (AppendOffset != BufferSize) {
                    ++buffer->RefCount;
                }
            } else {
#ifndef NDEBUG
                // ensure this span does not point into any existing buffer part
                const char *begin = span.data();
                const char *end = span.data() + span.size();
                for (const auto& buffer : Buffers) {
                    const char *bufferBegin = buffer->Data;
                    const char *bufferEnd = bufferBegin + BufferSize;
                    if (bufferBegin < end && begin < bufferEnd) {
                        Y_FAIL();
                    }
                }
#endif
            }

            if (!SendQueue.empty()) {
                auto& back = SendQueue.back();
                if (back.Span.data() + back.Span.size() == span.data()) { // check if it is possible just to extend the last span
                    if (SendQueuePos == SendQueue.size()) {
                        --SendQueuePos;
                        SendOffset = back.Span.size();
                    }
                    back.Span = {back.Span.data(), back.Span.size() + span.size()};
                    DropBufferReference(buffer);
                    return;
                }
            }

            if (buffer) {
                ++buffer->RefCount;
            }
            SendQueue.push_back(TSendChunk{span, buffer});
            DropBufferReference(buffer);
        }

        void Write(TContiguousSpan in) {
            while (in.size()) {
                auto outChunk = AcquireSpanForWriting(in.size());
                Append(outChunk);
                memcpy(outChunk.data(), in.data(), outChunk.size());
                in = in.SubSpan(outChunk.size(), Max<size_t>());
            }
        }

        using TBookmark = TStackVec<TMutableContiguousSpan, 2>;

        TBookmark Bookmark(size_t len) {
            TBookmark bookmark;

            while (len) {
                const auto span = AcquireSpanForWriting(len);
                Append(span);
                bookmark.push_back(span);
                len -= span.size();
            }

            return bookmark;
        }

        void WriteBookmark(TBookmark&& bookmark, TContiguousSpan in) {
            for (auto& outChunk : bookmark) {
                Y_VERIFY(outChunk.size() <= in.size());
                memcpy(outChunk.data(), in.data(), outChunk.size());
                in = in.SubSpan(outChunk.size(), Max<size_t>());
            }
        }

        void Rewind() {
            SendQueuePos = 0;
            SendOffset = 0;
        }

        template<typename T>
        void ProduceIoVec(T& container, size_t maxItems) {
            size_t offset = SendOffset;
            for (auto it = SendQueue.begin() + SendQueuePos; it != SendQueue.end() && std::size(container) < maxItems; ++it) {
                const TContiguousSpan span = it->Span.SubSpan(offset, Max<size_t>());
                container.push_back(NActors::TConstIoVec{span.data(), span.size()});
                offset = 0;
            }
        }

        void Advance(size_t numBytes) { // called when numBytes portion of data has been sent
            Y_VERIFY_DEBUG(numBytes == 0 || SendQueuePos != SendQueue.size());
            SendOffset += numBytes;
            for (auto it = SendQueue.begin() + SendQueuePos; SendOffset && it->Span.size() <= SendOffset; ++SendQueuePos, ++it) {
                SendOffset -= it->Span.size();
                Y_VERIFY_DEBUG(SendOffset == 0 || SendQueuePos != SendQueue.size() - 1);
            }
        }

        void DropFront(size_t numBytes) { // drops first numBytes from the queue, freeing buffers when necessary
            while (numBytes) {
                Y_VERIFY_DEBUG(!SendQueue.empty());
                auto& front = SendQueue.front();
                if (numBytes < front.Span.size()) {
                    front.Span = front.Span.SubSpan(numBytes, Max<size_t>());
                    if (SendQueuePos == 0) {
                        Y_VERIFY_DEBUG(numBytes <= SendOffset);
                        SendOffset -= numBytes;
                    }
                    break;
                } else {
                    numBytes -= front.Span.size();
                }
                Y_VERIFY_DEBUG(!front.Buffer || (front.Span.data() >= front.Buffer->Data &&
                    front.Span.data() + front.Span.size() <= front.Buffer->Data + BufferSize));
                DropBufferReference(front.Buffer);
                SendQueue.pop_front();
                if (SendQueuePos) {
                    --SendQueuePos;
                } else {
                    SendOffset = 0;
                }
            }
        }

        template<typename T>
        void ScanLastBytes(size_t numBytes, T&& callback) const {
            auto it = SendQueue.end();
            ssize_t offset = -numBytes;
            while (offset < 0) {
                Y_VERIFY_DEBUG(it != SendQueue.begin());
                const TSendChunk& chunk = *--it;
                offset += chunk.Span.size();
            }
            for (; it != SendQueue.end(); ++it, offset = 0) {
                callback(it->Span.SubSpan(offset, Max<size_t>()));
            }
        }

    private:
        void DropBufferReference(TBuffer *buffer) {
            if (buffer && !--buffer->RefCount) {
                const size_t index = buffer->Index;
                auto& cell = Buffers[index];
                Y_VERIFY_DEBUG(cell.get() == buffer);
                std::swap(cell, Buffers.back());
                cell->Index = index;
                Buffers.pop_back();
            }
        }
    };

    using TOutgoingStream = TOutgoingStreamT<262144>;

} // NInterconnect