-
Notifications
You must be signed in to change notification settings - Fork 41
/
buffer.d
216 lines (175 loc) · 4.54 KB
/
buffer.d
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
// Written in the D programming language.
module msgpack.buffer;
//import std.traits;
import std.range;
version(Posix)
{
import core.sys.posix.sys.uio : iovec;
}
else
{
/**
* from core.sys.posix.sys.uio.iovec for compatibility with posix.
*/
struct iovec
{
void* iov_base;
size_t iov_len;
}
}
/**
* $(D RefBuffer) is a reference stored buffer for more efficient serialization
*
* Example:
* -----
* auto packer = packer(RefBuffer(16)); // threshold is 16
*
* // packs data
*
* writev(fd, cast(void*)packer.buffer.vector.ptr, packer.buffer.vector.length);
* -----
*/
struct RefBuffer
{
private:
static struct Chunk
{
ubyte[] data; // storing serialized value
size_t used; // used size of data
}
immutable size_t Threshold;
immutable size_t ChunkSize;
// for putCopy
Chunk[] chunks_; // memory chunk for buffer
size_t index_; // index for cunrrent chunk
// for putRef
iovec[] vecList_; // reference to large data or copied data.
public:
/**
* Constructs a buffer.
*
* Params:
* threshold = the threshold of writing value or stores reference.
* chunkSize = the default size of chunk for allocation.
*/
@safe
this(in size_t threshold, in size_t chunkSize = 8192)
{
Threshold = threshold;
ChunkSize = chunkSize;
chunks_.length = 1;
chunks_[index_].data.length = chunkSize;
}
/**
* Returns the buffer contents that excluding references.
*
* Returns:
* the non-contiguous copied contents.
*/
@property @safe
nothrow ubyte[] data()
{
ubyte[] result;
foreach (ref chunk; chunks_)
result ~= chunk.data[0..chunk.used];
return result;
}
/**
* Forwards to all buffer contents.
*
* Returns:
* the array of iovec struct that stores references.
*/
@property @safe
nothrow ref iovec[] vector() return
{
return vecList_;
}
/**
* Writes the argument to buffer and stores the reference of writed content
* if the argument size is smaller than threshold,
* otherwise stores the reference of argument directly.
*
* Params:
* value = the content to write.
*/
@safe
void put(in ubyte value)
{
ubyte[1] values = [value];
putCopy(values);
}
/// ditto
@safe
void put(in ubyte[] value)
{
if (value.length < Threshold)
putCopy(value);
else
putRef(value);
}
private:
/*
* Stores the reference of $(D_PARAM value).
*
* Params:
* value = the content to write.
*/
@trusted
void putRef(in ubyte[] value)
{
vecList_.length += 1;
vecList_[$ - 1] = iovec(cast(void*)value.ptr, value.length);
}
/*
* Writes $(D_PARAM value) to buffer and appends to its reference.
*
* Params:
* value = the contents to write.
*/
@trusted
void putCopy(in ubyte[] value)
{
/*
* Helper for expanding new space.
*/
void expand(in size_t size)
{
const newSize = size < ChunkSize ? ChunkSize : size;
index_++;
chunks_.length = 1;
chunks_[index_].data.length = newSize;
}
const size = value.length;
// lacks current chunk?
if (chunks_[index_].data.length - chunks_[index_].used < size)
expand(size);
const base = chunks_[index_].used; // start index
auto data = chunks_[index_].data[base..base + size]; // chunk to write
data[] = value[];
chunks_[index_].used += size;
// Optimization for avoiding iovec allocation.
if (vecList_.length && data.ptr == (vecList_[$ - 1].iov_base +
vecList_[$ - 1].iov_len))
vecList_[$ - 1].iov_len += size;
else
putRef(data);
}
}
unittest
{
static assert(isOutputRange!(RefBuffer, ubyte) &&
isOutputRange!(RefBuffer, ubyte[]));
auto buffer = RefBuffer(2, 4);
ubyte[] tests = [1, 2];
foreach (v; tests)
buffer.put(v);
buffer.put(tests);
assert(buffer.data == tests, "putCopy failed");
iovec[] vector = buffer.vector;
ubyte[] result;
assert(vector.length == 2, "Optimization failed");
foreach (v; vector)
result ~= (cast(ubyte*)v.iov_base)[0..v.iov_len];
assert(result == tests ~ tests);
}