Skip to content

Commit 3c9fe90

Browse files
committed
improve scopedBuffer
1 parent 9af158d commit 3c9fe90

File tree

7 files changed

+91
-154
lines changed

7 files changed

+91
-154
lines changed

source/mir/algorithm/iteration.d

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4119,37 +4119,29 @@ template rcfilter(alias pred = "a", alias map = "a")
41194119
auto rcfilter(Range)(Range r)
41204120
if (isIterable!Range && (!isSlice!Range || DimensionCount!Range == 1))
41214121
{
4122-
import core.lifetime: forward;
4123-
import mir.primitives: isInputRange;
4124-
import mir.rc.array: RCArray;
4125-
4126-
if (false)
4122+
import core.lifetime: forward;
4123+
import mir.appender: scopedBuffer;
4124+
import mir.primitives: isInputRange;
4125+
import mir.rc.array: RCArray;
4126+
4127+
alias T = typeof(map(r.front));
4128+
auto buffer = scopedBuffer!T;
4129+
foreach (ref e; r)
4130+
{
4131+
if (pred(e))
41274132
{
4128-
auto p = pred(r.front);
4129-
auto e = map(r.front);
4130-
r.popFront;
4131-
auto d = r.empty;
4133+
static if (__traits(isSame, naryFun!"a", map))
4134+
buffer.put(forward!e);
4135+
else
4136+
buffer.put(map(forward!e));
41324137
}
4133-
return () @trusted
4134-
{
4135-
import mir.appender: ScopedBuffer;
4136-
alias T = typeof(map(r.front));
4137-
ScopedBuffer!T buffer = void;
4138-
buffer.initialize;
4139-
foreach (ref e; r)
4140-
{
4141-
if (pred(e))
4142-
{
4143-
static if (__traits(isSame, naryFun!"a", map))
4144-
buffer.put(forward!e);
4145-
else
4146-
buffer.put(map(forward!e));
4147-
}
4148-
}
4149-
auto ret = RCArray!T(buffer.length);
4150-
buffer.moveDataAndEmplaceTo(ret[]);
4151-
return ret;
4152-
} ();
4138+
}
4139+
return () @trusted
4140+
{
4141+
auto ret = RCArray!T(buffer.length);
4142+
buffer.moveDataAndEmplaceTo(ret[]);
4143+
return ret;
4144+
} ();
41534145
}
41544146

41554147
/// ditto

source/mir/appender.d

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ private extern(C) @system nothrow @nogc pure void* memcpy(scope void* s1, scope
1414

1515
///
1616
struct ScopedBuffer(T, size_t bytes = 4096)
17-
if (bytes)
17+
if (bytes && T.sizeof <= bytes)
1818
{
1919
import std.traits: Unqual, isMutable, isStaticArray, isIterable, hasElaborateAssign, isAssignable, isArray;
2020
import mir.primitives: hasLength;
@@ -229,12 +229,19 @@ struct ScopedBuffer(T, size_t bytes = 4096)
229229
}
230230
}
231231

232+
/// ditto
233+
auto scopedBuffer(T, size_t bytes = 4096)() @trusted
234+
{
235+
ScopedBuffer!(T, bytes) buffer = void;
236+
buffer.initialize;
237+
return buffer;
238+
}
239+
232240
///
233-
@trusted pure nothrow @nogc
241+
@safe pure nothrow @nogc
234242
version (mir_test) unittest
235243
{
236-
ScopedBuffer!char buf = void;
237-
buf.initialize;
244+
auto buf = scopedBuffer!char;
238245
buf.put('c');
239246
buf.put("str");
240247
assert(buf.data == "cstr");
@@ -244,11 +251,10 @@ version (mir_test) unittest
244251
}
245252

246253
/// immutable
247-
@trusted pure nothrow @nogc
254+
@safe pure nothrow @nogc
248255
version (mir_test) unittest
249256
{
250-
ScopedBuffer!(immutable char) buf = void;
251-
buf.initialize;
257+
auto buf = scopedBuffer!(immutable char);
252258
buf.put('c');
253259
buf.put("str");
254260
assert(buf.data == "cstr");
@@ -260,7 +266,7 @@ version (mir_test) unittest
260266
@safe pure nothrow @nogc
261267
version (mir_test) unittest
262268
{
263-
ScopedBuffer!(char, 3) buf;
269+
auto buf = scopedBuffer!(char, 3);
264270
buf.put('c');
265271
buf.put("str");
266272
assert(buf.data == "cstr");

source/mir/array/allocation.d

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -112,47 +112,27 @@ if ((isInputRange!Range || isIterable!Range) && !isInfinite!Range && !isStaticAr
112112
}
113113
else
114114
{
115-
import mir.appender: ScopedBuffer;
115+
import mir.appender: scopedBuffer;
116+
import std.array: uninitializedArray;
117+
118+
auto a = scopedBuffer!(Unqual!E);
116119

117-
if (false)
120+
static if (isInputRange!Range)
121+
for (; !r.empty; r.popFront)
122+
a.put(r.front);
123+
else
124+
static if (isPointer!Range)
118125
{
119-
ScopedBuffer!(Unqual!E) a;
120-
static if (isInputRange!Range)
121-
for (; !r.empty; r.popFront)
122-
a.put(r.front);
123-
else
124-
static if (isPointer!Range)
125-
{
126-
foreach (e; *r)
127-
a.put(forward!e);
128-
}
129-
else
130-
{
131-
foreach (e; r)
132-
a.put(forward!e);
133-
}
126+
foreach (e; *r)
127+
a.put(forward!e);
128+
}
129+
else
130+
{
131+
foreach (e; r)
132+
a.put(forward!e);
134133
}
135134

136-
return () @trusted {
137-
ScopedBuffer!(Unqual!E) a = void;
138-
a.initialize;
139-
140-
static if (isInputRange!Range)
141-
for (; !r.empty; r.popFront)
142-
a.put(r.front);
143-
else
144-
static if (isPointer!Range)
145-
{
146-
foreach (e; *r)
147-
a.put(forward!e);
148-
}
149-
else
150-
{
151-
foreach (e; r)
152-
a.put(forward!e);
153-
}
154-
155-
import std.array: uninitializedArray;
135+
return () @trusted {
156136
auto ret = uninitializedArray!(Unqual!E[])(a.length);
157137
a.moveDataAndEmplaceTo(ret);
158138
return ret;

source/mir/format.d

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -70,33 +70,17 @@ string text(string separator = "", A...)(auto ref A args)
7070
}
7171
else
7272
{
73-
import mir.appender: ScopedBuffer;
74-
if (false)
73+
import mir.appender: scopedBuffer;
74+
auto buffer = scopedBuffer!char;
75+
foreach (i, ref arg; args)
7576
{
76-
ScopedBuffer!char buffer;
77-
foreach (i, ref arg; args)
77+
buffer.print(arg);
78+
static if (separator.length && i + 1 < args.length)
7879
{
79-
buffer.print(arg);
80-
static if (separator.length && i + 1 < args.length)
81-
{
82-
buffer.printStaticString!char(separator);
83-
}
80+
buffer.printStaticString!char(separator);
8481
}
85-
return buffer.data.idup;
8682
}
87-
return () @trusted {
88-
ScopedBuffer!char buffer = void;
89-
buffer.initialize;
90-
foreach (i, ref arg; args)
91-
{
92-
buffer.print(arg);
93-
static if (separator.length && i + 1 < args.length)
94-
{
95-
buffer.printStaticString!char(separator);
96-
}
97-
}
98-
return buffer.data.idup;
99-
} ();
83+
return buffer.data.idup;
10084
}
10185
}
10286

@@ -546,8 +530,8 @@ version (mir_test) unittest
546530
yes,
547531
}
548532

549-
import mir.appender: ScopedBuffer;
550-
ScopedBuffer!char w;
533+
import mir.appender: scopedBuffer;
534+
auto w = scopedBuffer!char;
551535
w.print(Flag.yes);
552536
assert(w.data == "yes", w.data);
553537
}
@@ -573,8 +557,8 @@ ref W print(C = char, W)(scope return ref W w, bool c)
573557
@safe pure nothrow @nogc
574558
version (mir_test) unittest
575559
{
576-
import mir.appender: ScopedBuffer;
577-
ScopedBuffer!char w;
560+
import mir.appender: scopedBuffer;
561+
auto w = scopedBuffer!char;
578562
assert(w.print(true).data == `true`, w.data);
579563
w.reset;
580564
assert(w.print(false).data == `false`, w.data);
@@ -608,8 +592,8 @@ ref W print(C = char, W, V, K)(scope return ref W w, scope const V[K] c)
608592
@safe pure
609593
version (mir_test) unittest
610594
{
611-
import mir.appender: ScopedBuffer;
612-
ScopedBuffer!char w;
595+
import mir.appender: scopedBuffer;
596+
auto w = scopedBuffer!char;
613597
w.print(["a": 1, "b": 2]);
614598
assert(w.data == `["a": 1, "b": 2]` || w.data == `["b": 2, "a": 1]`, w.data);
615599
}
@@ -639,8 +623,8 @@ ref W print(C = char, W, T)(scope return ref W w, scope const(T)[] c)
639623
@safe pure nothrow @nogc
640624
version (mir_test) unittest
641625
{
642-
import mir.appender: ScopedBuffer;
643-
ScopedBuffer!char w;
626+
import mir.appender: scopedBuffer;
627+
auto w = scopedBuffer!char;
644628
string[2] array = ["a\na", "b"];
645629
assert(w.print(array[]).data == `["a\na", "b"]`, w.data);
646630
}
@@ -691,8 +675,8 @@ ref W print(C = char, W)(scope return ref W w, char c)
691675
@safe pure nothrow @nogc
692676
version (mir_test) unittest
693677
{
694-
import mir.appender: ScopedBuffer;
695-
ScopedBuffer!char w;
678+
import mir.appender: scopedBuffer;
679+
auto w = scopedBuffer!char;
696680
assert(w
697681
.print('\n')
698682
.print('\'')
@@ -902,8 +886,8 @@ version (mir_test) unittest
902886
static struct F { scope const(char)[] toString()() const return { return "f"; } }
903887
static struct G { const(char)[] s = "g"; alias s this; }
904888

905-
import mir.appender: ScopedBuffer;
906-
ScopedBuffer!char w;
889+
import mir.appender: scopedBuffer;
890+
auto w = scopedBuffer!char;
907891
assert(stringBuf() << A() << S() << D() << F() << G() << getData == "asdfg");
908892
}
909893

@@ -1061,7 +1045,7 @@ ref W printZeroPad(C = char, W, I)(scope return ref W w, const I c, size_t minim
10611045
version (mir_test) unittest
10621046
{
10631047
import mir.appender;
1064-
ScopedBuffer!char w;
1048+
auto w = scopedBuffer!char;
10651049

10661050
w.printZeroPad(-123, 5);
10671051
w.put(' ');

source/mir/math/sum.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ struct Summator(T, Summation summation)
514514
import mir.math.ieee: signbit;
515515
private:
516516
enum F M = (cast(F)(2)) ^^ (T.max_exp - 1);
517-
ScopedBuffer!(F, 8) partials;
517+
auto partials = scopedBuffer!(F, 8 * T.sizeof);
518518
//sum for NaN and infinity.
519519
F s = summationInitValue!F;
520520
//Overflow Degree. Count of 2^^F.max_exp minus count of -(2^^F.max_exp)

source/mir/ndslice/topology.d

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3383,26 +3383,18 @@ template rcmap(fun...)
33833383
if (!hasAsSlice!Range && !isSlice!Range && !is(Range : T[], T))
33843384
{
33853385
import core.lifetime: forward;
3386+
import mir.appender: scopedBuffer;
33863387
import mir.primitives: isInputRange;
33873388
import mir.rc.array: RCArray;
3388-
3389-
if (false)
3389+
alias T = typeof(f(r.front));
3390+
auto buffer = scopedBuffer!T;
3391+
while (!r.empty)
33903392
{
3391-
auto e = f(r.front);
3393+
buffer.put(f(r.front));
33923394
r.popFront;
3393-
auto d = r.empty;
33943395
}
33953396
return () @trusted
33963397
{
3397-
import mir.appender: ScopedBuffer;
3398-
alias T = typeof(f(r.front));
3399-
ScopedBuffer!T buffer = void;
3400-
buffer.initialize;
3401-
while (!r.empty)
3402-
{
3403-
buffer.put(f(r.front));
3404-
r.popFront;
3405-
}
34063398
auto ret = RCArray!T(buffer.length, false);
34073399
buffer.moveDataAndEmplaceTo(ret[]);
34083400
return ret;

source/mir/rc/array.d

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -415,38 +415,21 @@ template rcarray(T)
415415
}
416416
else
417417
{
418-
import mir.appender: ScopedBuffer;
418+
import mir.appender: scopedBuffer;
419419
import mir.conv: emplaceRef;
420-
if (false)
421-
{
422-
ScopedBuffer!T a;
423-
static if (isInputRange!Range)
424-
for (; !range.empty; range.popFront)
425-
a.put(range.front);
426-
else
427-
static if (isPointer!Range)
428-
foreach (e; *range)
429-
a.put(e);
430-
else
431-
foreach (e; range)
432-
a.put(e);
433-
scope values = a.data;
434-
auto ret = RCArray!T(values.length, false);
435-
}
420+
auto a = scopedBuffer!T;
421+
static if (isInputRange!Range)
422+
for (; !range.empty; range.popFront)
423+
a.put(range.front);
424+
else
425+
static if (isPointer!Range)
426+
foreach (e; *range)
427+
a.put(e);
428+
else
429+
foreach (e; range)
430+
a.put(e);
431+
scope values = a.data;
436432
return ()@trusted {
437-
ScopedBuffer!T a = void;
438-
a.initialize;
439-
static if (isInputRange!Range)
440-
for (; !range.empty; range.popFront)
441-
a.put(range.front);
442-
else
443-
static if (isPointer!Range)
444-
foreach (e; *range)
445-
a.put(e);
446-
else
447-
foreach (e; range)
448-
a.put(e);
449-
scope values = a.data;
450433
auto ret = RCArray!T(values.length, false);
451434
a.moveDataAndEmplaceTo(ret[]);
452435
return ret;

0 commit comments

Comments
 (0)