The cost of doing compile time introspection
Biotronic via Digitalmars-d
digitalmars-d at puremagic.com
Wed May 10 07:03:58 PDT 2017
On Wednesday, 10 May 2017 at 11:45:05 UTC, Moritz Maxeiner wrote:
[CTFE slow]
First, as you may know, Stefan Koch is working on an improved
CTFE engine that will hopefully make things a lot better.
As for making the code faster right now, could this be done with
mixin templates instead?
Something like:
import functions = llvm.functions.link;
import std.meta, std.traits;
template isCFunction(alias member)
{
static if (isFunction!(member) &&
(functionLinkage!(member) == "C" ||
functionLinkage!(member) == "Windows")) {
enum isCFunction = true;
} else {
enum isCFunction = false;
}
}
template CFunctions(alias scope_)
{
alias GetSymbol(string member) =
AliasSeq!(__traits(getMember, scope_, member));
alias CFunctions = Filter!(isCFunction, staticMap!(GetSymbol,
__traits(allMembers, scope_)));
}
mixin template declareStubsImpl(T...)
{
static if (T.length == 0) {
} else {
mixin("extern (C) typeof(T[0])* "~__traits(identifier,
T[0])~";");
mixin declareStubsImpl!(T[1..$]);
}
}
mixin template declareStubs(alias scope_)
{
mixin declareStubsImpl!(CFunctions!scope_);
}
mixin declareStubs!functions;
(tests indicate this compiles but doesn't run, and I'm helping
you on company time, so I can't fix everything right now :p)
> if the new static foreach works the way I suspect, then that
> part could essentially be replaced (at module scope) with
> something like
>
> ---
> static foreach (fn; CFunctions!functions) {
> typeof(functions.fn)* __traits(identifier, fn);
> }
A few things here - functions.fn would not do what you want, and
neither would __traits(identifier).
functions.fn would treat "fn" like a part of name, not a string
value, so this will make the poor compiler barf.
__traits(identifier, fn) expects fn to be a symbol, while here
it's a string. In fact, it's exactly the string you want __traits
to return.
Lastly, you'll still need a mixin, whether it's for
__traits(identifier, fn) or just fn - they're just strings.
Something like this:
static foreach (fn; CFunctions!functions) {
mixin("typeof(__traits(getMember, functions, fn))* "~fn~";");
}
More information about the Digitalmars-d
mailing list