Skip to content

Commit

Permalink
minimize std.conv usage
Browse files Browse the repository at this point in the history
  • Loading branch information
9il committed Oct 24, 2018
1 parent 59f1c06 commit 1977c09
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 95 deletions.
2 changes: 1 addition & 1 deletion source/stdx/allocator/building_blocks/affix_allocator.d
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The following methods are defined if $(D Allocator) defines them, and forward to
struct AffixAllocator(Allocator, Prefix, Suffix = void)
{
import mir.utility : min;
import std.conv : emplace;
import stdx.allocator.internal : emplace;
import stdx.allocator : IAllocator, theAllocator;
import stdx.allocator.common : stateSize, forwardToMember,
roundUpToMultipleOf, alignedAt, alignDownTo, roundUpToMultipleOf,
Expand Down
2 changes: 1 addition & 1 deletion source/stdx/allocator/building_blocks/allocator_list.d
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ called `factory`.
*/
struct AllocatorList(Factory, BookkeepingAllocator = GCAllocator)
{
import std.conv : emplace;
import stdx.allocator.internal : emplace;
import stdx.allocator.building_blocks.stats_collector
: StatsCollector, Options;
import std.traits : hasMember;
Expand Down
16 changes: 6 additions & 10 deletions source/stdx/allocator/building_blocks/bitmapped_block.d
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ block size to the constructor.
struct BitmappedBlock(size_t theBlockSize, uint theAlignment = platformAlignment,
ParentAllocator = NullAllocator)
{
import std.conv : text;
import std.traits : hasMember;
import stdx.allocator.internal : Ternary;
import std.typecons : tuple, Tuple;
Expand Down Expand Up @@ -117,7 +116,7 @@ struct BitmappedBlock(size_t theBlockSize, uint theAlignment = platformAlignment
{
alias parent = ParentAllocator.instance;
}
private uint _blocks;
private size_t _blocks;
private BitVector _control;
private void[] _payload;
private size_t _startIdx;
Expand Down Expand Up @@ -162,8 +161,7 @@ struct BitmappedBlock(size_t theBlockSize, uint theAlignment = platformAlignment
immutable ulong totalBits = data.length * 8;
immutable ulong bitsPerBlock = blockSize * 8 + 1;
// Get a first estimate
import std.conv : to;
_blocks = to!uint(totalBits / bitsPerBlock);
_blocks = cast(size_t)(totalBits / bitsPerBlock);

// Reality is a bit more complicated, iterate until a good number of
// blocks found.
Expand Down Expand Up @@ -424,7 +422,7 @@ struct BitmappedBlock(size_t theBlockSize, uint theAlignment = platformAlignment

private void[] smallAlloc(uint blocks)
{
assert(blocks >= 2 && blocks <= 64, text(blocks));
assert(blocks >= 2 && blocks <= 64);
foreach (i; _startIdx .. _control.rep.length)
{
// Test within the current 64-bit word
Expand Down Expand Up @@ -526,8 +524,7 @@ struct BitmappedBlock(size_t theBlockSize, uint theAlignment = platformAlignment
return false;
}
// Expansion successful
assert(p.ptr == b.ptr + blocksOld * blockSize,
text(p.ptr, " != ", b.ptr + blocksOld * blockSize));
assert(p.ptr == b.ptr + blocksOld * blockSize);
b = b.ptr[0 .. b.length + delta];
return true;
}
Expand Down Expand Up @@ -710,7 +707,7 @@ struct BitmappedBlock(size_t theBlockSize, uint theAlignment = platformAlignment

@system unittest
{
static void testAllocateAll(size_t bs)(uint blocks, uint blocksAtATime)
static void testAllocateAll(size_t bs)(size_t blocks, uint blocksAtATime)
{
import mir.utility : min;
assert(bs);
Expand Down Expand Up @@ -859,7 +856,6 @@ struct BitmappedBlockWithInternalPointers(
size_t theBlockSize, uint theAlignment = platformAlignment,
ParentAllocator = NullAllocator)
{
import std.conv : text;
import stdx.allocator.internal : Ternary;
@system unittest
{
Expand Down Expand Up @@ -902,7 +898,7 @@ struct BitmappedBlockWithInternalPointers(
immutable bits = len.roundUpToMultipleOf(64);
void[] b = _allocStart.rep;
if (!_heap.reallocate(b, bits / 8)) return false;
assert(b.length * 8 == bits, text(b.length * 8, " != ", bits));
assert(b.length * 8 == bits);
_allocStart = BitVector(cast(ulong[]) b);
assert(_allocStart.rep.length * 64 == bits);
_allocStart.rep[oldLength .. $] = ulong.max;
Expand Down
9 changes: 2 additions & 7 deletions source/stdx/allocator/building_blocks/free_list.d
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ struct FreeList(ParentAllocator,
size_t minSize, size_t maxSize = minSize,
Flag!"adaptive" adaptive = No.adaptive)
{
import std.conv : text;
import std.exception : enforce;
import std.traits : hasMember;
import stdx.allocator.internal : Ternary;
Expand Down Expand Up @@ -242,9 +241,7 @@ struct FreeList(ParentAllocator,
{
if (freeListEligible(bytes))
{
assert(parent.goodAllocSize(max) == max,
text("Wrongly configured freelist: maximum should be ",
parent.goodAllocSize(max), " instead of ", max));
assert(parent.goodAllocSize(max) == max, "Wrongly configured freelist maximum value");
return max;
}
}
Expand Down Expand Up @@ -633,8 +630,7 @@ struct ContiguousFreeList(ParentAllocator,
if (support.ptr <= b.ptr && b.ptr < support.ptr + support.length)
{
// we own this guy
import std.conv : text;
assert(fl.freeListEligible(b.length), text(b.length));
assert(fl.freeListEligible(b.length));
assert(allocated);
--allocated;
// Put manually in the freelist
Expand Down Expand Up @@ -754,7 +750,6 @@ $(D expand) is defined to forward to $(D ParentAllocator.expand)
struct SharedFreeList(ParentAllocator,
size_t minSize, size_t maxSize = minSize, size_t approxMaxNodes = unbounded)
{
import std.conv : text;
import std.exception : enforce;
import std.traits : hasMember;

Expand Down
50 changes: 0 additions & 50 deletions source/stdx/allocator/building_blocks/free_tree.d
Original file line number Diff line number Diff line change
Expand Up @@ -141,35 +141,6 @@ struct FreeTree(ParentAllocator)
recurse(root);
}

private string formatSizes()
{
string result = "(";
void recurse(Node* n)
{
if (!n)
{
result ~= "_";
return;
}
import std.conv : to;
result ~= to!string(n.size);
for (auto sis = n.sibling; sis; sis = sis.sibling)
{
result ~= "+moar";
}
if (n.left || n.right)
{
result ~= " (";
recurse(n.left);
result ~= ' ';
recurse(n.right);
result ~= ")";
}
}
recurse(root);
return result ~= ")";
}

private static void rotate(ref Node* parent, bool toRight)
{
assert(parent);
Expand Down Expand Up @@ -332,27 +303,6 @@ struct FreeTree(ParentAllocator)
return true;
}

@system unittest // test a few simple configurations
{
import stdx.allocator.gc_allocator;
FreeTree!GCAllocator a;
auto b1 = a.allocate(10000);
auto b2 = a.allocate(20000);
auto b3 = a.allocate(30000);
assert(b1.ptr && b2.ptr && b3.ptr);
a.deallocate(b1);
a.deallocate(b3);
a.deallocate(b2);
assert(a.formatSizes == "(20480 (12288 32768))", a.formatSizes);

b1 = a.allocate(10000);
assert(a.formatSizes == "(20480 (_ 32768))", a.formatSizes);
b1 = a.allocate(30000);
assert(a.formatSizes == "(20480)", a.formatSizes);
b1 = a.allocate(20000);
assert(a.formatSizes == "(_)", a.formatSizes);
}

@system unittest // build a complex free tree
{
import stdx.allocator.gc_allocator, std.range;
Expand Down
2 changes: 1 addition & 1 deletion source/stdx/allocator/building_blocks/kernighan_ritchie.d
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ it actually returns memory to the operating system when possible.
auto p = cast(KRRegion!()* ) store.ptr;
import core.stdc.string : memcpy;
import std.algorithm.mutation : move;
import std.conv : emplace;
import stdx.allocator.internal : emplace;

memcpy(p, &alloc, alloc.sizeof);
emplace(&alloc);
Expand Down
5 changes: 2 additions & 3 deletions source/stdx/allocator/building_blocks/region.d
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,6 @@ hot memory is used first.
struct InSituRegion(size_t size, size_t minAlign = platformAlignment)
{
import mir.utility : max;
import std.conv : to;
import std.traits : hasMember;
import stdx.allocator.internal : Ternary;

Expand Down Expand Up @@ -567,8 +566,8 @@ struct InSituRegion(size_t size, size_t minAlign = platformAlignment)
InSituRegion!(4096, 1) r1;
auto a = r1.allocate(2001);
assert(a.length == 2001);
import std.conv : text;
assert(r1.available == 2095, text(r1.available));
import std.conv : to;
assert(r1.available == 2095, r1.available.to!string);

InSituRegion!(65_536, 1024*4) r2;
assert(r2.available <= 65_536);
Expand Down
19 changes: 7 additions & 12 deletions source/stdx/allocator/building_blocks/stats_collector.d
Original file line number Diff line number Diff line change
Expand Up @@ -529,14 +529,13 @@ public:
*/
void reportStatistics(R)(auto ref R output)
{
import std.conv : to;
import std.traits : EnumMembers;
foreach (e; EnumMembers!Options)
{
foreach (member; __traits(allMembers, Options))
{{
enum e = __traits(getMember, Options, member);
static if ((flags & e) && e != Options.numAll
&& e != Options.bytesAll && e != Options.all)
output.write(e.to!string, ":", mixin(e.to!string), '\n');
}
output.write(member, ":", e, '\n');
}}
}

static if (perCallFlags)
Expand All @@ -561,7 +560,7 @@ public:
Format to a string such as:
$(D mymodule.d(655): [numAllocate:21, numAllocateOK:21, bytesAllocated:324202]).
*/
string toString() const
string toString()() const
{
import std.conv : text, to;
auto result = text(file, "(", line, "): [");
Expand Down Expand Up @@ -611,11 +610,7 @@ public:

private PerCallStatistics* statsAt(string f, uint n, opts...)()
{
import std.array : array;
import std.range : repeat;

static PerCallStatistics s = { f, n, [ opts ],
repeat(0UL, opts.length).array };
static PerCallStatistics s = { f, n, [ opts ], new ulong[opts.length] };
static bool inserted;

if (!inserted)
Expand Down
2 changes: 1 addition & 1 deletion source/stdx/allocator/common.d
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ package void[] roundUpToAlignment()(void[] b, uint a)
Like `a / b` but rounds the result up, not down.
*/
@safe @nogc nothrow pure
package size_t divideRoundUp(size_t a, size_t b)
package size_t divideRoundUp()(size_t a, size_t b)
{
assert(b);
return (a + b - 1) / b;
Expand Down
2 changes: 1 addition & 1 deletion source/stdx/allocator/internal.d
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,7 @@ pure nothrow @safe /* @nogc */ unittest
import core.stdc.stdlib : malloc;
void[] buf = malloc(Node.sizeof)[0 .. Node.sizeof];

import std.conv : emplace;
import stdx.allocator.internal : emplace;
const Node* n = emplace!(const Node)(buf, 42, null, 10);
assert(n.payload == 42);
assert(n.next == null);
Expand Down
16 changes: 8 additions & 8 deletions source/stdx/allocator/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ private IAllocator setupThreadAllocator()() nothrow @nogc @safe
}

assert(!_threadAllocator);
import std.conv : emplace;
import stdx.allocator.internal : emplace;
static ulong[stateSize!(ThreadAllocator).divideRoundUp(ulong.sizeof)] _threadAllocatorState;
_threadAllocator = () @trusted { return emplace!(ThreadAllocator)(_threadAllocatorState[]); } ();
return _threadAllocator;
Expand Down Expand Up @@ -1060,7 +1060,7 @@ T[] makeArray(T, Allocator)(auto ref Allocator alloc, size_t length,
}
}
}
import std.conv : emplace;
import stdx.allocator.internal : emplace;
for (; i < length; ++i)
{
emplace!T(&result[i], init);
Expand Down Expand Up @@ -1583,7 +1583,7 @@ if (isInputRange!R)
for (; !range.empty; range.popFront, toFill.popFront)
{
assert(!toFill.empty);
import std.conv : emplace;
import stdx.allocator.internal : emplace;
emplace!T(&toFill.front, range.front);
}
assert(toFill.empty);
Expand All @@ -1603,7 +1603,7 @@ if (isInputRange!R)
array = cast(T[]) buf;
return false;
}
import std.conv : emplace;
import stdx.allocator.internal : emplace;
emplace!T(buf[$ - T.sizeof .. $], range.front);
}

Expand Down Expand Up @@ -2001,7 +2001,7 @@ statically-typed allocator.)
CAllocatorImpl!A allocatorObject(A)(auto ref A a)
if (!isPointer!A)
{
import std.conv : emplace;
import stdx.allocator.internal : emplace;
static if (stateSize!A == 0)
{
enum s = stateSize!(CAllocatorImpl!A).divideRoundUp(ulong.sizeof);
Expand Down Expand Up @@ -2043,7 +2043,7 @@ if (!isPointer!A)
CAllocatorImpl!(A, Yes.indirect) allocatorObject(A)(A* pa)
{
assert(pa);
import std.conv : emplace;
import stdx.allocator.internal : emplace;
auto state = pa.allocate(stateSize!(CAllocatorImpl!(A, Yes.indirect)));
import std.traits : hasMember;
static if (hasMember!(A, "deallocate"))
Expand Down Expand Up @@ -2095,7 +2095,7 @@ statically-typed allocator.)
shared(CSharedAllocatorImpl!A) sharedAllocatorObject(A)(auto ref A a)
if (!isPointer!A)
{
import std.conv : emplace;
import stdx.allocator.internal : emplace;
static if (stateSize!A == 0)
{
enum s = stateSize!(CSharedAllocatorImpl!A).divideRoundUp(ulong.sizeof);
Expand Down Expand Up @@ -2130,7 +2130,7 @@ if (!isPointer!A)
shared(CSharedAllocatorImpl!(A, Yes.indirect)) sharedAllocatorObject(A)(A* pa)
{
assert(pa);
import std.conv : emplace;
import stdx.allocator.internal : emplace;
auto state = pa.allocate(stateSize!(CSharedAllocatorImpl!(A, Yes.indirect)));
import std.traits : hasMember;
static if (hasMember!(A, "deallocate"))
Expand Down

0 comments on commit 1977c09

Please sign in to comment.