Skip to content

Commit 53d4d02

Browse files
authored
Json serde d (#359)
1 parent 8e559d6 commit 53d4d02

File tree

4 files changed

+107
-5
lines changed

4 files changed

+107
-5
lines changed

bench/algorithm/json-serde/2.d

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import asdf.asdf;
2+
import std;
3+
4+
struct Properties {
5+
string name;
6+
}
7+
8+
static struct Numeric {
9+
import mir.algebraic: Variant;
10+
import asdf: SerdeException;
11+
12+
static union types {
13+
double floating;
14+
int integer;
15+
}
16+
alias Union = Variant!(
17+
types
18+
);
19+
20+
Union data;
21+
22+
alias data this;
23+
24+
static foreach (T; Union.AllowedTypes)
25+
this(T v) @safe pure nothrow @nogc { data = v; }
26+
27+
void serialize(S)(ref S serializer) const
28+
{
29+
import asdf: serializeValue;
30+
import mir.algebraic: visit;
31+
32+
data.visit!(
33+
(int v) => serializer.serializeValue(v),
34+
(double v) => serializer.serializeValue(v),
35+
);
36+
}
37+
38+
SerdeException deserializeFromAsdf(Asdf asdfData)
39+
{
40+
import asdf : deserialize, deserializeValue;
41+
42+
double tmp;
43+
if (auto exc = deserializeValue(asdfData, tmp))
44+
return exc;
45+
if (cast(int) tmp == tmp)
46+
data = cast(int) tmp;
47+
else
48+
data = tmp;
49+
return null;
50+
}
51+
}
52+
53+
struct Geometry {
54+
alias Coord = Numeric[2][];
55+
56+
string type;
57+
Coord[] coordinates;
58+
}
59+
60+
struct Feature {
61+
string type;
62+
Properties properties;
63+
Geometry geometry;
64+
}
65+
66+
struct GeoData {
67+
string type;
68+
Feature[] features;
69+
}
70+
71+
void printHash(string data)
72+
{
73+
import std.digest.md;
74+
75+
auto md5 = new MD5Digest();
76+
auto hash = md5.digest(data);
77+
writeln(toHexString!(LetterCase.lower)(hash));
78+
}
79+
80+
void main(string[] args)
81+
{
82+
import asdf.serialization: deserialize, serializeToJson;
83+
84+
auto fileName = args.length > 1 ? args[1] : "sample";
85+
auto n = args.length > 2 ? args[2].to!int() : 10;
86+
auto jsonText = readText(fileName ~ ".json");
87+
auto json = deserialize!GeoData(jsonText);
88+
printHash(serializeToJson(json));
89+
90+
auto array = Array!GeoData(json);
91+
array.reserve(n);
92+
foreach(_; 1..n)
93+
array.insertBack(deserialize!GeoData(jsonText));
94+
printHash(serializeToJson(array[]));
95+
}

bench/algorithm/knucleotide/1-m.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ ubyte[] readInput(string[] args)
154154
char key = '>';
155155
ubyte[] res;
156156
auto app = appender(&res);
157-
app.reserve(65_536);
157+
app.reserve(2_500_120);
158158
auto file = File(args[1]);
159159
byte x = 3;
160160
foreach (line; file.byLine())

bench/bench_d.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ problems:
5454
- name: knucleotide
5555
source:
5656
- 1-m.d
57+
- name: json-serde
58+
source:
59+
- 2.d
5760
compiler_version_command:
5861
compiler_version_regex:
5962
runtime_version_parameter:
@@ -68,7 +71,7 @@ environments:
6871
include: d
6972
include_sub_dir: source
7073
# build: dmd -O -release -of=out/app app.d
71-
build: dub build --build=release --compiler=dmd -v
74+
build: dub build -b release-nobounds -a x86_64 --combined --compiler=dmd -v
7275
out_dir: out
7376
run_cmd: app
7477
- os: linux
@@ -79,6 +82,6 @@ environments:
7982
include: d
8083
include_sub_dir: source
8184
# build: ldc2 -O -release -of=out/app app.d
82-
build: dub build --build=release --compiler=ldc2 -v
85+
build: dub build -b release-nobounds -a x86_64 --combined --compiler=ldc2 -v
8386
out_dir: out
8487
run_cmd: app

bench/include/d/dub.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
"root"
44
],
55
"copyright": "Copyright © 2021, root",
6-
"dependencies": {"intel-intrinsics": "~>1.10.8"},
6+
"dependencies": {
7+
"asdf": "~>0.7.15",
8+
"intel-intrinsics": "1.11.1",
9+
"mir-algorithm": {"version": "468377999b43fe370c2f59a40257183475f04c55", "repository": "git+https://github.com/cyrusmsk/mir-algorithm.git", "default": true},
10+
},
711
"description": "A minimal D application.",
812
"license": "MIT",
913
"name": "app",
1014
"targetPath": "out",
1115
"dflags-dmd": ["-mcpu=avx2"],
12-
"dflags-ldc": ["-mattr=+avx2,+ssse3,+sse4.1", "-mcpu=broadwell"]
16+
"dflags-ldc": ["-mattr=+avx2,+sse3,+ssse3,+sse4.1,+sse4.2", "-mcpu=broadwell"]
1317
}

0 commit comments

Comments
 (0)