Skip to content

Commit f22cc26

Browse files
authored
Improve std.regex wordMatcher build times by delaying its work from CT to RT if possible (#8699)
1 parent ac19f94 commit f22cc26

File tree

1 file changed

+22
-3
lines changed
  • std/regex/internal

1 file changed

+22
-3
lines changed

std/regex/internal/ir.d

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,29 @@ CharMatcher[CodepointSet] matcherCache;
4949
}
5050
}
5151

52-
@property ref wordMatcher()()
52+
// Force pure because that is needed
53+
// Templated so that we don't pull in std.uni wordCharacter unnecessarily.
54+
@property ref wordMatcher()() pure
5355
{
54-
static immutable CharMatcher matcher = CharMatcher(wordCharacter);
55-
return matcher;
56+
static auto actual()
57+
{
58+
static CharMatcher matcher;
59+
static bool haveMatcher;
60+
61+
if (!haveMatcher)
62+
{
63+
matcher = CharMatcher(wordCharacter);
64+
haveMatcher = true;
65+
}
66+
67+
return &matcher;
68+
}
69+
70+
// WORKAROUND: if the compiler won't memoize the output of the function for us,
71+
// we'll do it with pure and there will be casts and it'll be happy about it.
72+
// This is unfortunately needed to make std.regex as a whole faster to import & use
73+
// in build times ~500ms.
74+
return *(cast(immutable(CharMatcher)* function() @safe nothrow @nogc pure)&actual)();
5675
}
5776

5877
// some special Unicode white space characters

0 commit comments

Comments
 (0)