-
Notifications
You must be signed in to change notification settings - Fork 621
/
ByteBlockPool.cs
439 lines (395 loc) · 16 KB
/
ByteBlockPool.cs
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
427
428
429
430
431
432
433
434
435
436
437
438
439
using J2N.Collections.Generic.Extensions;
using J2N.Numerics;
using Lucene.Net.Diagnostics;
using Lucene.Net.Support;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace Lucene.Net.Util
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/// <summary>
/// Class that Posting and PostingVector use to write byte
/// streams into shared fixed-size <see cref="T:byte[]"/> arrays. The idea
/// is to allocate slices of increasing lengths. For
/// example, the first slice is 5 bytes, the next slice is
/// 14, etc. We start by writing our bytes into the first
/// 5 bytes. When we hit the end of the slice, we allocate
/// the next slice and then write the address of the new
/// slice into the last 4 bytes of the previous slice (the
/// "forwarding address").
/// <para/>
/// Each slice is filled with 0's initially, and we mark
/// the end with a non-zero byte. This way the methods
/// that are writing into the slice don't need to record
/// its length and instead allocate a new slice once they
/// hit a non-zero byte.
/// <para/>
/// @lucene.internal
/// </summary>
public sealed class ByteBlockPool
{
public static readonly int BYTE_BLOCK_SHIFT = 15;
public static readonly int BYTE_BLOCK_SIZE = 1 << BYTE_BLOCK_SHIFT;
public static readonly int BYTE_BLOCK_MASK = BYTE_BLOCK_SIZE - 1;
/// <summary>
/// Abstract class for allocating and freeing byte
/// blocks.
/// </summary>
public abstract class Allocator
{
protected readonly int m_blockSize;
protected Allocator(int blockSize)
{
this.m_blockSize = blockSize;
}
public abstract void RecycleByteBlocks(byte[][] blocks, int start, int end); // LUCENENT TODO: API - Change to use IList<byte[]>
public virtual void RecycleByteBlocks(IList<byte[]> blocks)
{
var b = blocks.ToArray();
RecycleByteBlocks(b, 0, b.Length);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual byte[] GetByteBlock()
{
return new byte[m_blockSize];
}
}
/// <summary>
/// A simple <see cref="Allocator"/> that never recycles. </summary>
public sealed class DirectAllocator : Allocator
{
public DirectAllocator()
: this(BYTE_BLOCK_SIZE)
{
}
public DirectAllocator(int blockSize)
: base(blockSize)
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override void RecycleByteBlocks(byte[][] blocks, int start, int end)
{
}
}
/// <summary>
/// A simple <see cref="Allocator"/> that never recycles, but
/// tracks how much total RAM is in use.
/// </summary>
public class DirectTrackingAllocator : Allocator
{
private readonly Counter bytesUsed;
public DirectTrackingAllocator(Counter bytesUsed)
: this(BYTE_BLOCK_SIZE, bytesUsed)
{
}
public DirectTrackingAllocator(int blockSize, Counter bytesUsed)
: base(blockSize)
{
this.bytesUsed = bytesUsed;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override byte[] GetByteBlock()
{
bytesUsed.AddAndGet(m_blockSize);
return new byte[m_blockSize];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override void RecycleByteBlocks(byte[][] blocks, int start, int end)
{
bytesUsed.AddAndGet(-((end - start) * m_blockSize));
for (var i = start; i < end; i++)
{
blocks[i] = null;
}
}
}
/// <summary>
/// Array of buffers currently used in the pool. Buffers are allocated if
/// needed don't modify this outside of this class.
/// </summary>
[WritableArray]
[SuppressMessage("Microsoft.Performance", "CA1819", Justification = "Lucene's design requires some writable array properties")]
public byte[][] Buffers
{
get => buffers;
set => buffers = value;
}
private byte[][] buffers = new byte[10][];
/// <summary>
/// index into the buffers array pointing to the current buffer used as the head </summary>
private int bufferUpto = -1; // Which buffer we are upto
/// <summary>
/// Where we are in head buffer </summary>
public int ByteUpto { get; set; }
/// <summary>
/// Current head buffer
/// </summary>
[WritableArray]
[SuppressMessage("Microsoft.Performance", "CA1819", Justification = "Lucene's design requires some writable array properties")]
public byte[] Buffer
{
get => buffer;
set => buffer = value;
}
private byte[] buffer;
/// <summary>
/// Current head offset </summary>
public int ByteOffset { get; set; }
private readonly Allocator allocator;
public ByteBlockPool(Allocator allocator)
{
// set defaults
ByteUpto = BYTE_BLOCK_SIZE;
ByteOffset = -BYTE_BLOCK_SIZE;
this.allocator = allocator;
}
/// <summary>
/// Resets the pool to its initial state reusing the first buffer and fills all
/// buffers with <c>0</c> bytes before they reused or passed to
/// <see cref="Allocator.RecycleByteBlocks(byte[][], int, int)"/>. Calling
/// <see cref="ByteBlockPool.NextBuffer()"/> is not needed after reset.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Reset()
{
Reset(true, true);
}
/// <summary>
/// Expert: Resets the pool to its initial state reusing the first buffer. Calling
/// <see cref="ByteBlockPool.NextBuffer()"/> is not needed after reset. </summary>
/// <param name="zeroFillBuffers"> if <c>true</c> the buffers are filled with <tt>0</tt>.
/// this should be set to <c>true</c> if this pool is used with slices. </param>
/// <param name="reuseFirst"> if <c>true</c> the first buffer will be reused and calling
/// <see cref="ByteBlockPool.NextBuffer()"/> is not needed after reset if the
/// block pool was used before ie. <see cref="ByteBlockPool.NextBuffer()"/> was called before. </param>
public void Reset(bool zeroFillBuffers, bool reuseFirst)
{
if (bufferUpto != -1)
{
// We allocated at least one buffer
if (zeroFillBuffers)
{
for (int i = 0; i < bufferUpto; i++)
{
// Fully zero fill buffers that we fully used
Arrays.Fill(buffers[i], (byte)0);
}
// Partial zero fill the final buffer
Arrays.Fill(buffers[bufferUpto], 0, ByteUpto, (byte)0);
}
if (bufferUpto > 0 || !reuseFirst)
{
int offset = reuseFirst ? 1 : 0;
// Recycle all but the first buffer
allocator.RecycleByteBlocks(buffers, offset, 1 + bufferUpto);
Arrays.Fill(buffers, offset, 1 + bufferUpto, null);
}
if (reuseFirst)
{
// Re-use the first buffer
bufferUpto = 0;
ByteUpto = 0;
ByteOffset = 0;
buffer = buffers[0];
}
else
{
bufferUpto = -1;
ByteUpto = BYTE_BLOCK_SIZE;
ByteOffset = -BYTE_BLOCK_SIZE;
buffer = null;
}
}
}
/// <summary>
/// Advances the pool to its next buffer. This method should be called once
/// after the constructor to initialize the pool. In contrast to the
/// constructor a <see cref="ByteBlockPool.Reset()"/> call will advance the pool to
/// its first buffer immediately.
/// </summary>
public void NextBuffer()
{
if (1 + bufferUpto == buffers.Length)
{
var newBuffers = new byte[ArrayUtil.Oversize(buffers.Length + 1, RamUsageEstimator.NUM_BYTES_OBJECT_REF)][];
Array.Copy(buffers, 0, newBuffers, 0, buffers.Length);
buffers = newBuffers;
}
buffer = buffers[1 + bufferUpto] = allocator.GetByteBlock();
bufferUpto++;
ByteUpto = 0;
ByteOffset += BYTE_BLOCK_SIZE;
}
/// <summary>
/// Allocates a new slice with the given size.</summary>
/// <seealso cref="ByteBlockPool.FIRST_LEVEL_SIZE"/>
public int NewSlice(int size)
{
if (ByteUpto > BYTE_BLOCK_SIZE - size)
{
NextBuffer();
}
int upto = ByteUpto;
ByteUpto += size;
buffer[ByteUpto - 1] = 16;
return upto;
}
// Size of each slice. These arrays should be at most 16
// elements (index is encoded with 4 bits). First array
// is just a compact way to encode X+1 with a max. Second
// array is the length of each slice, ie first slice is 5
// bytes, next slice is 14 bytes, etc.
/// <summary>
/// An array holding the offset into the <see cref="ByteBlockPool.LEVEL_SIZE_ARRAY"/>
/// to quickly navigate to the next slice level.
/// </summary>
public static readonly int[] NEXT_LEVEL_ARRAY = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 9 };
/// <summary>
/// An array holding the level sizes for byte slices.
/// </summary>
public static readonly int[] LEVEL_SIZE_ARRAY = new int[] { 5, 14, 20, 30, 40, 40, 80, 80, 120, 200 };
/// <summary>
/// The first level size for new slices </summary>
/// <seealso cref="ByteBlockPool.NewSlice(int)"/>
public static readonly int FIRST_LEVEL_SIZE = LEVEL_SIZE_ARRAY[0];
/// <summary>
/// Creates a new byte slice with the given starting size and
/// returns the slices offset in the pool.
/// </summary>
public int AllocSlice(byte[] slice, int upto)
{
int level = slice[upto] & 15;
int newLevel = NEXT_LEVEL_ARRAY[level];
int newSize = LEVEL_SIZE_ARRAY[newLevel];
// Maybe allocate another block
if (ByteUpto > BYTE_BLOCK_SIZE - newSize)
{
NextBuffer();
}
int newUpto = ByteUpto;
int offset = newUpto + ByteOffset;
ByteUpto += newSize;
// Copy forward the past 3 bytes (which we are about
// to overwrite with the forwarding address):
buffer[newUpto] = slice[upto - 3];
buffer[newUpto + 1] = slice[upto - 2];
buffer[newUpto + 2] = slice[upto - 1];
// Write forwarding address at end of last slice:
slice[upto - 3] = (byte)offset.TripleShift(24);
slice[upto - 2] = (byte)offset.TripleShift(16);
slice[upto - 1] = (byte)offset.TripleShift(8);
slice[upto] = (byte)offset;
// Write new level:
buffer[ByteUpto - 1] = (byte)(16 | newLevel);
return newUpto + 3;
}
// Fill in a BytesRef from term's length & bytes encoded in
// byte block
public void SetBytesRef(BytesRef term, int textStart)
{
var bytes = term.Bytes = buffers[textStart >> BYTE_BLOCK_SHIFT];
var pos = textStart & BYTE_BLOCK_MASK;
if ((bytes[pos] & 0x80) == 0)
{
// length is 1 byte
term.Length = bytes[pos];
term.Offset = pos + 1;
}
else
{
// length is 2 bytes
term.Length = (bytes[pos] & 0x7f) + ((bytes[pos + 1] & 0xff) << 7);
term.Offset = pos + 2;
}
if (Debugging.AssertsEnabled) Debugging.Assert(term.Length >= 0);
}
/// <summary>
/// Appends the bytes in the provided <see cref="BytesRef"/> at
/// the current position.
/// </summary>
public void Append(BytesRef bytes)
{
var length = bytes.Length;
if (length == 0)
{
return;
}
int offset = bytes.Offset;
int overflow = (length + ByteUpto) - BYTE_BLOCK_SIZE;
do
{
if (overflow <= 0)
{
Array.Copy(bytes.Bytes, offset, buffer, ByteUpto, length);
ByteUpto += length;
break;
}
else
{
int bytesToCopy = length - overflow;
if (bytesToCopy > 0)
{
Array.Copy(bytes.Bytes, offset, buffer, ByteUpto, bytesToCopy);
offset += bytesToCopy;
length -= bytesToCopy;
}
NextBuffer();
overflow = overflow - BYTE_BLOCK_SIZE;
}
} while (true);
}
/// <summary>
/// Reads bytes bytes out of the pool starting at the given offset with the given
/// length into the given byte array at offset <c>off</c>.
/// <para>Note: this method allows to copy across block boundaries.</para>
/// </summary>
public void ReadBytes(long offset, byte[] bytes, int off, int length)
{
if (length == 0)
{
return;
}
var bytesOffset = off;
var bytesLength = length;
var bufferIndex = (int)(offset >> BYTE_BLOCK_SHIFT);
var buffer = buffers[bufferIndex];
var pos = (int)(offset & BYTE_BLOCK_MASK);
var overflow = (pos + length) - BYTE_BLOCK_SIZE;
do
{
if (overflow <= 0)
{
Array.Copy(buffer, pos, bytes, bytesOffset, bytesLength);
break;
}
else
{
int bytesToCopy = length - overflow;
Array.Copy(buffer, pos, bytes, bytesOffset, bytesToCopy);
pos = 0;
bytesLength -= bytesToCopy;
bytesOffset += bytesToCopy;
buffer = buffers[++bufferIndex];
overflow = overflow - BYTE_BLOCK_SIZE;
}
} while (true);
}
}
}