Using core.reflect to rewrite code

Stefan Koch uplink.coder at googlemail.com
Thu Aug 12 08:17:27 UTC 2021


On Thursday, 12 August 2021 at 02:12:23 UTC, max haughton wrote:
> On Wednesday, 11 August 2021 at 23:58:21 UTC, Stefan Koch wrote:
>> On Tuesday, 10 August 2021 at 23:51:29 UTC, Stefan Koch wrote:
>>> [...]
>>
>> I've manged to improve the performance of using core.reflect 
>> by roughly 2x because I realized that class literals created 
>> by core.reflect don't need "scrubbing".
>>
>> [...]
>
> Can you profile it vs. a template metaprogramming solution?

TL;DR
The template solution [1] takes 60% more time and 80% more memory
compared to the core.reflect solution.
While being more difficult to write and therefore being less 
useful.

I have written a template solution, while it does not offer the 
same functionality.
It should provide a reasonable lower bound for one.
The template only collect structs and functions.
Writing the correct filtering and constructing the string is a 
task for another day :)




Here are the results:
```
uplink at uplink-black:~/d/dmd(core_reflect)$ hyperfine 
"generated/linux/release/64/dmd -c testTemplate.d" 
"generated/linux/release/64/dmd -c testCollector.d"
Benchmark #1: generated/linux/release/64/dmd -c testTemplate.d
   Time (mean ± σ):      28.5 ms ±   2.3 ms    [User: 21.9 ms, 
System: 6.8 ms]
   Range (min … max):    21.1 ms …  33.2 ms    95 runs

Benchmark #2: generated/linux/release/64/dmd -c testCollector.d
   Time (mean ± σ):      21.7 ms ±   2.7 ms    [User: 16.5 ms, 
System: 5.4 ms]
   Range (min … max):    13.8 ms …  27.6 ms    147 runs

Summary
   'generated/linux/release/64/dmd -c testCollector.d' ran
     1.31 ± 0.19 times faster than 'generated/linux/release/64/dmd 
-c testTemplate.d'
```

and a little proxy for memory use as well as the output

```
uplink at uplink-black:~/d/dmd(core_reflect)$ /usr/bin/time 
generated/linux/release/64/dmd -c testTemplate.d
tuple(getName, getOrdinal, setName, unrelated)
(Ctx)
0.02user 0.00system 0:00.03elapsed 96%CPU (0avgtext+0avgdata 
25824maxresident)k
0inputs+8outputs (0major+5191minor)pagefaults 0swaps
```

```
uplink at uplink-black:~/d/dmd(core_reflect)$ /usr/bin/time 
generated/linux/release/64/dmd -c testCollector.d
class CtxWrapper {
     const(char*) getName () {
         getName(ctx);
     }

     uint getOrdinal () {
         getOrdinal(ctx);
     }

     void setName (const(char*) name) {
         setName(ctx, name);
     }

}

0.02user 0.00system 0:00.02elapsed 104%CPU (0avgtext+0avgdata 
14364maxresident)k
0inputs+40outputs (0major+2245minor)pagefaults 0swaps
```

template solution benchmarked:
[1] 
https://gist.github.com/UplinkCoder/c2838252c55c9fdf4fc526e2a8c5ce7e

core.reflect solution benchmarked:
[2] 
https://gist.github.com/UplinkCoder/93cb06e4921ab4c96752c6325e03e42d


More information about the Digitalmars-d mailing list