Phobos Unittest

Steven Schveighoffer schveiguy at gmail.com
Sun Sep 5 15:08:45 UTC 2021


On 9/4/21 4:05 PM, Per Nordlöw wrote:
> On Saturday, 4 September 2021 at 13:12:49 UTC, Steven Schveighoffer wrote:
>> Note that lexing and parsing is extremely quick, and I wouldn't focus 
>> on trying to trim this out, you won't get much performance out of that.
>>
>> -Steve
> 
> For the record, a D file containing only `import std;` type checks via
> 
> ```sh
> time dmd import_std.d -o-
> ```
> 
> in 0.20s whereas
> 
> ```sh
> time dmd import_std.d -o-
> ```
> 
> in 0.45s on my ThreadRipper.

I doubt that's because of the parsing. I've [gone 
down](https://github.com/schveiguy/dmd/tree/debugunitteststuff) these 
kinds of rabbit holes. It's one of the reasons I tried to remove ALL 
version(unittest) blocks from phobos that import other modules. But you 
just can't remove them all. And any unittests inside templates are going 
to compile and get included, even if you don't instantiate anything. I 
also discovered that CTFE initializers run, even if you don't use the 
imported symbol, and even if you don't need type inference. Can't 
remember if that got fixed.

I think you would find you could probably attribute the 0.25s there to a 
few modules that do things when unittests are turned on.

According to 
https://github.com/dlang/dmd/blob/1ae5fee06ddd0599fb187595f8b0cebf8c840ebd/src/dmd/parse.d#L642-L683, 
if unittests aren't turned on, indeed the parser simplifies its parsing 
of the unittest. It skips over the unittest block, and doesn't allocate 
any AST nodes for it. But that difference I don't think is going to be 
the cause for a significant slowdown.

One can construct a test to check the parsing:

1. Generate a huge source file with 10,000 (or maybe more?) non-trivial 
unittests. You need enough to move the needle on parsing.
2. Import that file, and build the main file with and without the 
-unittest flag.
3. This should not actually compile the unittests, but just parse them.

Using phobos as a test case is rife with things that may be contributing 
besides the parser.

-Steve


More information about the Digitalmars-d-learn mailing list