Skip to content

Commit

Permalink
[WIP] Changes to test compiler extension to detect NRVO
Browse files Browse the repository at this point in the history
Those changes are temporary and hopefully can be removed once
the compiler side has caught up.
  • Loading branch information
Geod24 committed Apr 6, 2020
1 parent 4bddb7a commit cbcd60b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"name": "unittest",
"targetName": "agora-unittests",
"excludedSourceFiles": ["source/agora/cli/*/main.d"],
"dflags": [ "-O" ],
"sourceFiles-posix": [
"source/scpp/build/DSizeChecks.o",
"source/scpp/build/DLayoutChecks.o"
Expand Down
20 changes: 14 additions & 6 deletions source/agora/common/Serializer.d
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ import std.meta;
import std.range;
import std.traits;

struct nrvo {}

/// Pedestrian usage of serialization
unittest
{
Expand Down Expand Up @@ -165,7 +167,8 @@ unittest
auto values = deserializeFull!(typeof(QT.values))(dg, opts);
auto i = deserializeFull!(typeof(QT.i))(dg, opts);
auto s = deserializeFull!(typeof(QT.s))(dg, opts);
return QT(values, s, i);
auto ret = QT(values, s, i);
return ret;
}
}

Expand Down Expand Up @@ -574,11 +577,14 @@ public T deserializeFull (T) (scope const(ubyte)[] data) @safe

/// Ditto
public T deserializeFull (T) (scope DeserializeDg dg,
const ref DeserializerOptions opts = DeserializerOptions.Default) @safe
const ref DeserializerOptions opts = DeserializerOptions.Default) @safe @nrvo
{
// Custom deserialization trumps everything
static if (hasFromBinaryFunction!T)
return T.fromBinary!T(dg, opts);
{
auto ret = T.fromBinary!T(dg, opts);
return ret;
}

// Static array needs to be handled before arrays
// Note: This might be optimizable for small types (char, ubyte)
Expand Down Expand Up @@ -685,9 +691,11 @@ public T deserializeFull (T) (scope DeserializeDg dg,
{
Target convert (Target) ()
{
return deserializeFull!Target(dg, opts);
auto x = deserializeFull!Target(dg, opts);
return x;
}
return T(staticMap!(convert, Fields!T));
auto enableNRVO = T(staticMap!(convert, Fields!T));
return enableNRVO;
}

else
Expand Down Expand Up @@ -1216,7 +1224,7 @@ public struct SelfRef (OrigType) if (is(OrigType == struct))
///
public static QT fromBinary (QT) (
scope DeserializeDg dg, const ref DeserializerOptions opts)
@safe
@safe @nrvo
{
auto ret = QT(deserializeFull!(typeof(QT.value))(dg, opts));
() @trusted { (cast()ret.self[0]) = &ret.value; }();
Expand Down

0 comments on commit cbcd60b

Please sign in to comment.