Fixed-size OutBuffer that doesn't call .resize() automatically? (for database page buffers)

frame frame86 at live.com
Fri Aug 19 20:21:58 UTC 2022


On Friday, 19 August 2022 at 16:19:04 UTC, Gavin Ray wrote:
> 1. Calling `.toBytes()` on an `OutBuffer` will discard the 
> extra bytes allocated past what was reserved and used. But this 
> will still allocate the memory in the first place I guess (will 
> the compiler optimize this away?)

It does allocate when it needs to. It grows but never shrinks 
again.

> 2. Copy the `OutBuffer` class into a new 
> `FixedSizeOutBuffer(T)` and alter its behavior
>
> 3. Use `ubyte[PAGE_SIZE]` and manually write like below:

> ```d
>     static ubyte[4] bytes = new ubyte[4];
> ```
Looks still unnecessary - you are allocating thread local memory. 
Just use a static array.


> ```d
>     foreach (idx, ref slot; header.slots)
> ```
No need to reference `slot` here. You may prevent compiler 
optimizations.

> ```d
>     // Skip over free space
>     ubyte[] padding = new ubyte[header.freeSpacePointer];
>     padding[] = 0;
>     buf ~= padding;
> ```
Unnecessary, the initial value of `ubyte` is 0 and allocation is 
done automatically. Just set the slice length:
```d
buf.length += header.freeSpacePointer;
```

> ```d
>     foreach (idx, ref tuple; tuples)
> ```
Again, no need to reference

> ```d
>     move(buf.ptr, outbuf.ptr);
> ```
Not sure what that is. A simple
```d
outbuf[0 .. buf.length] = buf; // or whatever position to write 
in outbuf
```
should do it too.


More information about the Digitalmars-d-learn mailing list