Skip to content

Commit bfb4780

Browse files
committed
Improve the build times of dyaml.node
1 parent 2c915b3 commit bfb4780

File tree

2 files changed

+94
-37
lines changed

2 files changed

+94
-37
lines changed

source/dyaml/node.d

+61-3
Original file line numberDiff line numberDiff line change
@@ -2477,7 +2477,28 @@ struct Node
24772477
return this.value_.tryMatch!(
24782478
(inout bool v) @safe => v.to!TType,
24792479
(inout long v) @safe => v.to!TType,
2480-
(inout Node[] v) @trusted => v.to!TType,
2480+
(inout Node[] v) @trusted
2481+
{
2482+
static if (is(TType == string))
2483+
{
2484+
auto sink = appender!string();
2485+
sink ~= "[";
2486+
2487+
foreach(i, ref node; v)
2488+
{
2489+
if (i > 0)
2490+
sink ~= ", ";
2491+
node.toString(sink);
2492+
}
2493+
2494+
sink ~= "]";
2495+
return sink.data;
2496+
}
2497+
else
2498+
{
2499+
return v.to!TType;
2500+
}
2501+
},
24812502
(inout ubyte[] v) @safe => v.to!TType,
24822503
(inout string v) @safe => v.to!TType,
24832504
(inout Node.Pair[] v) @trusted => v.to!TType,
@@ -2510,17 +2531,54 @@ struct Node
25102531
this.value_.match!(
25112532
(const bool v) => formattedWrite(sink, v ? "true" : "false"),
25122533
(const long v) => formattedWrite(sink, "%s", v),
2513-
(const Node[] v) => formattedWrite(sink, "[%(%s, %)]", v),
2534+
(scope const Node[] v) @trusted
2535+
{
2536+
sink ~= "[";
2537+
2538+
foreach(i, ref node; v)
2539+
{
2540+
if (i > 0)
2541+
sink ~= ", ";
2542+
node.toString(sink);
2543+
}
2544+
2545+
sink ~= "]";
2546+
return 0;
2547+
},
25142548
(const ubyte[] v) => formattedWrite(sink, "%s", v),
25152549
(const string v) => formattedWrite(sink, `"%s"`, v),
25162550
(const Node.Pair[] v) => formattedWrite(sink, "{%(%s, %)}", v),
2517-
(const SysTime v) => formattedWrite(sink, "%s", v),
2551+
(const SysTime v)
2552+
{
2553+
v.toString(sink);
2554+
return 0;
2555+
},
25182556
(const YAMLNull v) => formattedWrite(sink, "%s", v),
25192557
(const YAMLMerge v) => formattedWrite(sink, "%s", v),
25202558
(const real v) => formattedWrite(sink, "%s", v),
25212559
(const YAMLInvalid v) => formattedWrite(sink, "%s", v),
25222560
);
25232561
}
2562+
2563+
@safe unittest
2564+
{
2565+
import std.array : appender;
2566+
auto output = appender!string();
2567+
auto seq1 = Node([1, 2, 3, 4, 5]);
2568+
2569+
seq1.toString(output);
2570+
assert(output.length > 0);
2571+
}
2572+
2573+
@safe unittest
2574+
{
2575+
import std.array : appender;
2576+
auto output = appender!string();
2577+
auto node = Node(SysTime(DateTime(2005, 6, 15, 20, 0, 0), UTC()));
2578+
2579+
node.toString(output);
2580+
assert(output.length > 0);
2581+
}
25242582
}
25252583

25262584
package:

source/dyaml/resolver.d

+33-34
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,42 @@ import dyaml.exception;
2727
/// Type of `regexes`
2828
private alias RegexType = Tuple!(string, "tag", const Regex!char, "regexp", string, "chars");
2929

30-
private immutable RegexType[] regexes = [
31-
RegexType("tag:yaml.org,2002:bool",
32-
regex(r"^(?:yes|Yes|YES|no|No|NO|true|True|TRUE" ~
33-
"|false|False|FALSE|on|On|ON|off|Off|OFF)$"),
34-
"yYnNtTfFoO"),
35-
RegexType("tag:yaml.org,2002:float",
36-
regex(r"^(?:[-+]?([0-9][0-9_]*)\\.[0-9_]*" ~
37-
"(?:[eE][-+][0-9]+)?|[-+]?(?:[0-9][0-9_]" ~
38-
"*)?\\.[0-9_]+(?:[eE][-+][0-9]+)?|[-+]?" ~
39-
"[0-9][0-9_]*(?::[0-5]?[0-9])+\\.[0-9_]" ~
40-
"*|[-+]?\\.(?:inf|Inf|INF)|\\." ~
41-
"(?:nan|NaN|NAN))$"),
42-
"-+0123456789."),
43-
RegexType("tag:yaml.org,2002:int",
44-
regex(r"^(?:[-+]?0b[0-1_]+" ~
45-
"|[-+]?0[0-7_]+" ~
46-
"|[-+]?(?:0|[1-9][0-9_]*)" ~
47-
"|[-+]?0x[0-9a-fA-F_]+" ~
48-
"|[-+]?[1-9][0-9_]*(?::[0-5]?[0-9])+)$"),
49-
"-+0123456789"),
50-
RegexType("tag:yaml.org,2002:merge", regex(r"^<<$"), "<"),
51-
RegexType("tag:yaml.org,2002:null",
52-
regex(r"^$|^(?:~|null|Null|NULL)$"), "~nN\0"),
53-
RegexType("tag:yaml.org,2002:timestamp",
54-
regex(r"^[0-9][0-9][0-9][0-9]-[0-9][0-9]-" ~
55-
"[0-9][0-9]|[0-9][0-9][0-9][0-9]-[0-9]" ~
56-
"[0-9]?-[0-9][0-9]?[Tt]|[ \t]+[0-9]" ~
57-
"[0-9]?:[0-9][0-9]:[0-9][0-9]" ~
58-
"(?:\\.[0-9]*)?(?:[ \t]*Z|[-+][0-9]" ~
59-
"[0-9]?(?::[0-9][0-9])?)?$"),
60-
"0123456789"),
61-
RegexType("tag:yaml.org,2002:value", regex(r"^=$"), "="),
30+
// build this at runtime, to prevent build time costs
31+
private __gshared immutable RegexType[] regexes;
32+
33+
shared static this() {
34+
__gshared string forceRT;
35+
RegexType[] regexArray;
36+
Regex!char toBuild;
37+
38+
toBuild = regex(r"^(?:yes|Yes|YES|no|No|NO|true|True|TRUE" ~ "|false|False|FALSE|on|On|ON|off|Off|OFF)$" ~ forceRT);
39+
regexArray ~= RegexType("tag:yaml.org,2002:bool", toBuild, "yYnNtTfFoO");
40+
41+
toBuild = regex(r"^(?:[-+]?([0-9][0-9_]*)\\.[0-9_]*" ~ "(?:[eE][-+][0-9]+)?|[-+]?(?:[0-9][0-9_]" ~ "*)?\\.[0-9_]+(?:[eE][-+][0-9]+)?|[-+]?" ~ "[0-9][0-9_]*(?::[0-5]?[0-9])+\\.[0-9_]" ~ "*|[-+]?\\.(?:inf|Inf|INF)|\\." ~ "(?:nan|NaN|NAN))$" ~ forceRT);
42+
regexArray ~= RegexType("tag:yaml.org,2002:float", toBuild, "-+0123456789.");
43+
44+
toBuild = regex(r"^(?:[-+]?0b[0-1_]+" ~ "|[-+]?0[0-7_]+" ~ "|[-+]?(?:0|[1-9][0-9_]*)" ~ "|[-+]?0x[0-9a-fA-F_]+" ~ "|[-+]?[1-9][0-9_]*(?::[0-5]?[0-9])+)$" ~ forceRT);
45+
regexArray ~= RegexType("tag:yaml.org,2002:int", toBuild, "-+0123456789");
46+
47+
toBuild = regex(r"^<<$" ~ forceRT);
48+
regexArray ~= RegexType("tag:yaml.org,2002:merge", toBuild, "<");
49+
50+
toBuild = regex(r"^$|^(?:~|null|Null|NULL)$" ~ forceRT);
51+
regexArray ~= RegexType("tag:yaml.org,2002:null", toBuild, "~nN\0");
52+
53+
toBuild = regex(r"^[0-9][0-9][0-9][0-9]-[0-9][0-9]-" ~ "[0-9][0-9]|[0-9][0-9][0-9][0-9]-[0-9]" ~ "[0-9]?-[0-9][0-9]?[Tt]|[ \t]+[0-9]" ~ "[0-9]?:[0-9][0-9]:[0-9][0-9]" ~ "(?:\\.[0-9]*)?(?:[ \t]*Z|[-+][0-9]" ~ "[0-9]?(?::[0-9][0-9])?)?$");
54+
regexArray ~= RegexType("tag:yaml.org,2002:timestamp", toBuild, "0123456789");
55+
56+
toBuild = regex(r"^=$");
57+
regexArray ~= RegexType("tag:yaml.org,2002:value", toBuild, "=");
6258

6359
//The following resolver is only for documentation purposes. It cannot work
6460
//because plain scalars cannot start with '!', '&', or '*'.
65-
RegexType("tag:yaml.org,2002:yaml", regex(r"^(?:!|&|\*)$"), "!&*"),
66-
];
61+
toBuild = regex(r"^(?:!|&|\*)$");
62+
regexArray ~= RegexType("tag:yaml.org,2002:yaml", toBuild, "!&*");
63+
64+
regexes = cast(immutable)regexArray;
65+
}
6766

6867
/**
6968
* Resolves YAML tags (data types).

0 commit comments

Comments
 (0)