-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonolithic_array.zig
426 lines (347 loc) · 11.7 KB
/
monolithic_array.zig
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
const std = @import("std");
const builtin = @import("builtin");
const assert = std.debug.assert;
const mem = std.mem;
const os = std.os;
const w = os.windows;
const warn = std.debug.warn;
inline fn pageCountForSize(size: usize) usize {
return (size + mem.page_size - 1) / mem.page_size;
}
/// Special array based on the plentiness of address space available to 64bit
/// processes.
/// Reserves enough memory pages from the OS to hold at most `max_count`
/// items. Pages are committed as needed when the array grows in size.
/// Advantages:
/// - item addresses are stable throughout the lifetime of the array
/// - no need to realloc, thus:
/// - no temporary 2n memory usage
/// - no need to copy items to their new storage when growing
/// - at most `page_size` memory overhead from unused items
/// Drawbacks:
/// - has to declare a maximum size upfront
/// - not suited to small arrays because of the page granularity
/// - no automatic geometric growth
pub fn MonolithicArray(comptime T: type) type {
// TODO support big sizes and alignments.
comptime assert(@alignOf(T) <= mem.page_size);
comptime assert(@sizeOf(T) <= mem.page_size);
return struct {
/// Always hold the actual pointer and length to existing items.
items: []T,
/// Total number of pages reserved.
reserved_pages: usize,
/// Number of pages currently used.
committed_pages: usize,
const Self = @This();
const ElementSize = @sizeOf(T);
const ElementsPerPage = mem.page_size / ElementSize;
inline fn pageCountForItems(count: usize) usize {
return pageCountForSize(count * ElementSize);
}
pub fn init(max_count: usize) !Self {
const page_count = pageCountForItems(max_count);
const alloc_size = page_count * mem.page_size;
const ptr = switch (builtin.os) {
.windows => try w.VirtualAlloc(
null,
alloc_size,
w.MEM_RESERVE,
w.PAGE_READWRITE,
),
else => @compileError("TODO"),
};
return Self{
.items = @ptrCast([*]T, @alignCast(@alignOf(T), ptr))[0..0],
.reserved_pages = page_count,
.committed_pages = 0,
};
}
pub fn deinit(self: *Self) void {
switch (builtin.os) {
.windows => w.VirtualFree(self.items.ptr, 0, w.MEM_RELEASE),
else => @compileError("TODO"),
}
}
pub fn size(self: *Self) usize {
return self.items.len;
}
pub fn capacity(self: *Self) usize {
return self.committed_pages * ElementsPerPage;
}
pub fn toSlice(self: *Self) []T {
return self.items;
}
pub fn toSliceConst(self: *Self) []const T {
return self.items;
}
fn grow(self: *Self, page_count: usize) !void {
// Start address of the first non-committed-yet page.
const ptr = @ptrToInt(self.items.ptr) + self.committed_pages * mem.page_size;
switch (builtin.os) {
// Calling directly kernel32 to bypass unexpectedError that
// prints a stack trace during tests.
.windows => _ = w.kernel32.VirtualAlloc(
@intToPtr(*c_void, ptr),
page_count * mem.page_size,
w.MEM_COMMIT,
w.PAGE_READWRITE,
) orelse return error.OutOfMemory,
else => @compileError("TODO"),
}
self.committed_pages += page_count;
}
pub fn reserve(self: *Self, wanted_capacity: usize) !void {
const wanted_page_count = pageCountForItems(wanted_capacity);
if (wanted_page_count > self.committed_pages) {
try self.grow(wanted_page_count - self.committed_pages);
}
}
pub fn append(self: *Self, item: T) !void {
const s = self.size();
if (self.capacity() == s) {
try self.grow(1);
}
self.items.len += 1;
self.items[s] = item;
}
pub fn appendAssumeCapacity(self: *Self, item: T) void {
assert(self.size() < self.capacity());
const s = self.items.len;
self.items.len += 1;
self.items[s] = item;
}
pub fn appendSlice(self: *Self, items: []const T) !void {
const s = self.size();
const wanted_size = s + items.len;
const cap = self.capacity();
if (wanted_size > cap) {
try self.grow(pageCountForItems(wanted_size - cap));
}
self.items.len = wanted_size;
mem.copy(T, self.items[s..], items);
}
pub fn remove(self: *Self, i: usize) T {
assert(i < self.size());
const s = self.items.len - 1;
const removed_elem = self.at(i);
if (i != s) {
self.items[i] = self.items[s];
}
self.items[s] = undefined;
self.items.len = s;
return removed_elem;
}
pub fn orderedRemove(self: *Self, i: usize) T {
assert(i < self.size());
const s = self.items.len - 1;
const removed_elem = self.at(i);
if (i != s) {
var j: usize = i;
while (j < s) : (j += 1) {
self.items[j] = self.items[j + 1];
}
}
self.items.len = s;
return removed_elem;
}
pub fn set(self: *Self, i: usize, item: T) void {
assert(i < self.size());
self.items[i] = item;
}
pub fn setOrError(self: *Self, i: usize, item: T) !void {
if (i >= self.size()) return error.OutOfBounds;
self.items[i] = item;
}
pub fn insert(self: *Self, i: usize, item: T) !void {
assert(i < self.size());
const s = self.size();
const new_size = s + 1;
if (self.capacity() == s) {
try self.grow(1);
}
self.items.len = new_size;
mem.copyBackwards(
T,
self.items[i + 1 .. new_size],
self.items[i..s],
);
self.items[i] = item;
}
pub fn insertSlice(self: *Self, items: []const T) !void {}
pub fn at(self: *const Self, i: usize) T {
return self.items[i];
}
};
}
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const expectError = std.testing.expectError;
test "init" {
var arr = try MonolithicArray(u32).init(1 << 32);
defer arr.deinit();
expectEqual(arr.size(), 0);
expectEqual(arr.capacity(), 0);
expectEqual(arr.committed_pages, 0);
}
test "append" {
var arr = try MonolithicArray(u32).init(1 << 32);
defer arr.deinit();
try arr.append(123);
try arr.append(456);
try arr.append(789);
expectEqual(arr.size(), 3);
expectEqual(arr.committed_pages, 1);
}
test "appendAssumeCapacity" {
var arr = try MonolithicArray(u32).init(1 << 32);
defer arr.deinit();
try arr.reserve(1); // force reserve for at least one item
var i: u32 = 0;
while (i < arr.capacity()) : (i += 1) {
arr.appendAssumeCapacity(i);
}
expectEqual(arr.size(), arr.capacity());
expectEqual(arr.committed_pages, 1);
}
test "appendSlice" {
var arr = try MonolithicArray(u32).init(1 << 32);
defer arr.deinit();
try arr.append(1);
try arr.append(2);
try arr.append(3);
try arr.appendSlice([_]u32{ 4, 5, 6 });
try arr.append(7);
expectEqual(arr.size(), 7);
for (arr.toSliceConst()) |i, j| {
expectEqual(i, @intCast(u32, j + 1));
}
}
test "at" {
var arr = try MonolithicArray(u32).init(1 << 32);
defer arr.deinit();
try arr.append(123);
try arr.append(456);
try arr.append(789);
expectEqual(arr.at(0), 123);
expectEqual(arr.at(1), 456);
expectEqual(arr.at(2), 789);
}
test "out of memory" {
const cap = mem.page_size / @sizeOf(u32);
var arr = try MonolithicArray(u32).init(cap);
defer arr.deinit();
var i: u32 = 0;
while (i < cap) : (i += 1) {
try arr.append(i);
}
expectError(error.OutOfMemory, arr.append(i));
}
test "reserve" {
var arr = try MonolithicArray(u32).init(1 << 32);
defer arr.deinit();
try arr.reserve(145);
expect(arr.capacity() >= 145);
expectEqual(arr.committed_pages, 1);
expectEqual(arr.committed_pages, 1);
expectEqual(arr.size(), 0);
}
test "reserve multiple pages" {
var arr = try MonolithicArray(u32).init(1 << 32);
defer arr.deinit();
const page_count = 3;
const capacity = (page_count * mem.page_size) / @sizeOf(u32);
try arr.reserve(capacity);
expectEqual(arr.capacity(), capacity);
expectEqual(arr.committed_pages, page_count);
expectEqual(arr.size(), 0);
}
test "reserve more pages than physical memory" {
// We can ask for 16TB and the OS will just comply.
const memory_size = 16 * 1000 * 1000 * 1000 * 1000;
const capacity = memory_size / @sizeOf(u32);
const page_count = pageCountForSize(memory_size);
var arr = try MonolithicArray(u32).init(capacity);
defer arr.deinit();
expectEqual(arr.capacity(), 0);
expectEqual(arr.size(), 0);
expectEqual(arr.reserved_pages, page_count);
}
test "grow" {
var arr = try MonolithicArray(u32).init(1 << 32);
defer arr.deinit();
const size = 1000 * 1000;
var i: u32 = 0;
while (i < size) : (i += 1) {
try arr.append(i);
}
expectEqual(arr.size(), size);
}
test "remove" {
var arr = try MonolithicArray(u32).init(1 << 32);
defer arr.deinit();
try arr.append(123);
try arr.append(456);
try arr.append(789);
try arr.append(147);
try arr.append(258);
try arr.append(369);
expectEqual(arr.remove(5), 369);
expectEqual(arr.remove(0), 123);
expectEqual(arr.remove(2), 789);
expectEqual(arr.size(), 3);
expectEqual(arr.at(0), 258);
expectEqual(arr.at(1), 456);
expectEqual(arr.at(2), 147);
}
test "orderedRemove" {
var arr = try MonolithicArray(u32).init(1 << 32);
defer arr.deinit();
try arr.append(123);
try arr.append(456);
try arr.append(789);
try arr.append(147);
try arr.append(258);
try arr.append(369);
expectEqual(arr.orderedRemove(5), 369);
expectEqual(arr.orderedRemove(0), 123);
expectEqual(arr.orderedRemove(0), 456);
expectEqual(arr.size(), 3);
expectEqual(arr.at(0), 789);
expectEqual(arr.at(1), 147);
expectEqual(arr.at(2), 258);
}
test "set" {
var arr = try MonolithicArray(u32).init(1 << 32);
defer arr.deinit();
try arr.append(0);
try arr.append(0);
try arr.append(0);
arr.set(0, 123);
arr.set(1, 456);
arr.set(2, 789);
expectEqual(arr.at(0), 123);
expectEqual(arr.at(1), 456);
expectEqual(arr.at(2), 789);
}
test "setOrError" {
var arr = try MonolithicArray(u32).init(1 << 32);
defer arr.deinit();
try arr.append(0);
expectError(error.OutOfBounds, arr.setOrError(1, 0));
expectError(error.OutOfBounds, arr.setOrError(2, 0));
expectError(error.OutOfBounds, arr.setOrError(123456, 0));
}
test "insert" {
var arr = try MonolithicArray(u32).init(1 << 32);
defer arr.deinit();
try arr.append(123);
try arr.append(456);
try arr.append(789);
try arr.insert(1, 159);
expectEqual(arr.size(), 4);
expectEqual(arr.at(0), 123);
expectEqual(arr.at(1), 159);
expectEqual(arr.at(2), 456);
expectEqual(arr.at(3), 789);
}