Jump to page: 1 2
Thread overview
0 cost template instantiation
Sep 30, 2021
Hipreme
Sep 30, 2021
jfondren
Sep 30, 2021
jfondren
Sep 30, 2021
Hipreme
Sep 30, 2021
jfondren
Sep 30, 2021
jfondren
Sep 30, 2021
Hipreme
Sep 30, 2021
Basile B.
Sep 30, 2021
Hipreme
Sep 30, 2021
Basile B.
Sep 30, 2021
Adam D Ruppe
Sep 30, 2021
Hipreme
Sep 30, 2021
Adam D Ruppe
September 30, 2021

I have a template function that all it does is given a symbol, it loads a dll for its type + its name:

void loadSymbol(alias s, string symName = "")()
{
	static if(symName == "")
		s = cast(typeof(s))_loadSymbol(_dll, (s.stringof~"\0").ptr);
	else
		s = cast(typeof(s))_loadSymbol(_dll, (symName~"\0").ptr);
}

The main problem is that this function is costing 2KB per instantiation, which is something pretty high. Specially if I inline, there is almost no cost when compared to its inline version. Trying to use pragma(inline, true) didn't do anything too.

If I understood correctly, mixin template would be some way to actually inline anything. The problem is that I can't have any kind of expression inside it, so, that's the only way I could think in how to do it.

Optimization seems to don't take care of that behaviour too.

I would like to know which approach could I take for making something like C's #define for that.

As this function could be really rewritten as a macro if I were coding in C, the cost is too high for a function that would be only a syntactic sugar

September 30, 2021

On Thursday, 30 September 2021 at 01:09:47 UTC, Hipreme wrote:

>

I have a template function that all it does is given a symbol, it loads a dll for its type + its name:

void loadSymbol(alias s, string symName = "")()
{
	static if(symName == "")
		s = cast(typeof(s))_loadSymbol(_dll, (s.stringof~"\0").ptr);
	else
		s = cast(typeof(s))_loadSymbol(_dll, (symName~"\0").ptr);
}

If symName is always a string literal, then you don't need to append a "\0"; string literals always have a trailing NUL after them, and they autoconvert to const(char)[] so you don't need the .ptr either.

September 30, 2021

On Thursday, 30 September 2021 at 01:09:47 UTC, Hipreme wrote:

>

I have a template function that all it does is given a symbol, it loads a dll for its type + its name:

void loadSymbol(alias s, string symName = "")()
{
	static if(symName == "")
		s = cast(typeof(s))_loadSymbol(_dll, (s.stringof~"\0").ptr);
	else
		s = cast(typeof(s))_loadSymbol(_dll, (symName~"\0").ptr);
}

The main problem is that this function is costing 2KB per instantiation, which is something pretty high. Specially if I inline, there is almost no cost when compared to its inline version. Trying to use pragma(inline, true) didn't do anything too.

cant you just use a regular functions ? loading happens at runtime right ?

September 30, 2021

On Thursday, 30 September 2021 at 01:56:42 UTC, Basile B. wrote:

>

On Thursday, 30 September 2021 at 01:09:47 UTC, Hipreme wrote:

>

I have a template function that all it does is given a symbol, it loads a dll for its type + its name:

void loadSymbol(alias s, string symName = "")()
{
	static if(symName == "")
		s = cast(typeof(s))_loadSymbol(_dll, (s.stringof~"\0").ptr);
	else
		s = cast(typeof(s))_loadSymbol(_dll, (symName~"\0").ptr);
}

The main problem is that this function is costing 2KB per instantiation, which is something pretty high. Specially if I inline, there is almost no cost when compared to its inline version. Trying to use pragma(inline, true) didn't do anything too.

cant you just use a regular functions ? loading happens at runtime right ?

The entire reason to having that function is having that syntax which would pretty much do the monkey's job for me:

Instead of writing

myFunction = cast(typeof(myFunction))_loadSymbol(_dll, "myFunction");

I could write

loadSymbol!myFunction;

But if no other way is found of doing that, I will do the massive rewriting.

Anyway, I don't think the problem is not in the way I'm doing, but the output, as that template could easily be inlined

September 30, 2021

On Thursday, 30 September 2021 at 02:02:19 UTC, Hipreme wrote:

>

On Thursday, 30 September 2021 at 01:56:42 UTC, Basile B. wrote:

>

On Thursday, 30 September 2021 at 01:09:47 UTC, Hipreme wrote:

>

I have a template function that all it does is given a symbol, it loads a dll for its type + its name:

void loadSymbol(alias s, string symName = "")()
{
	static if(symName == "")
		s = cast(typeof(s))_loadSymbol(_dll, (s.stringof~"\0").ptr);
	else
		s = cast(typeof(s))_loadSymbol(_dll, (symName~"\0").ptr);
}

The main problem is that this function is costing 2KB per instantiation, which is something pretty high. Specially if I inline, there is almost no cost when compared to its inline version. Trying to use pragma(inline, true) didn't do anything too.

cant you just use a regular functions ? loading happens at runtime right ?

The entire reason to having that function is having that syntax which would pretty much do the monkey's job for me:

Instead of writing

myFunction = cast(typeof(myFunction))_loadSymbol(_dll, "myFunction");

I could write

loadSymbol!myFunction;

But if no other way is found of doing that, I will do the massive rewriting.

Anyway, I don't think the problem is not in the way I'm doing, but the output, as that template could easily be inlined

well ok. Maybe try to see if overloads

void loadSymbol(alias s)()
void loadSymbol(alias s, string symName)()

helps. That save the static if. In addition there's the null sentinel that can save a bit of memory, as you've been suggested.

September 30, 2021

On Thursday, 30 September 2021 at 01:09:47 UTC, Hipreme wrote:

>

I have a template function that all it does is given a symbol, it loads a dll for its type + its name:

void loadSymbol(alias s, string symName = "")()
{
	static if(symName == "")
		s = cast(typeof(s))_loadSymbol(_dll, (s.stringof~"\0").ptr);
	else
		s = cast(typeof(s))_loadSymbol(_dll, (symName~"\0").ptr);
}

The main problem is that this function is costing 2KB per instantiation, which is something pretty high. Specially if I inline, there is almost no cost when compared to its inline version. Trying to use pragma(inline, true) didn't do anything too.

If I understood correctly, mixin template would be some way to actually inline anything. The problem is that I can't have any kind of expression inside it, so, that's the only way I could think in how to do it.

Optimization seems to don't take care of that behaviour too.

I would like to know which approach could I take for making something like C's #define for that.

As this function could be really rewritten as a macro if I were coding in C, the cost is too high for a function that would be only a syntactic sugar

I could reduce by almost 100kb of code by instead of using the former option, I use:

void loadSymbols(Ts...)()
{
	static foreach(s; Ts)
		s = cast(typeof(s))_loadSymbol(_dll, s.stringof);
}

Then instead of making a call for each one, I just pass a list of templates, this would only create one instantiation per need.

I do still don't think that this should be the answer, as the alias problem is not yet solved. It takes a lot of size for functions that could be only syntactic sugar, where C would be a lot better

September 30, 2021
On Thursday, 30 September 2021 at 02:02:19 UTC, Hipreme wrote:
> Instead of writing
>
> myFunction = cast(typeof(myFunction))_loadSymbol(_dll, "myFunction");
>
> I could write
>
> loadSymbol!myFunction;
>
> But if no other way is found of doing that, I will do the massive rewriting.

---

void function() x;

void load(void* thing, string name) {
       // this lhs cast is the magic, the rhs cast unneeded in reality cuz GetProcAddress returns void* naturally
        * cast(void**) thing = cast(void*) 0xdeadbeef;
}

void main() {
        // no cast here but yes repeated name
        load(&x, "foo");

         // casts needed here just to do the comparison
        assert(cast(void*) x == cast(void*) 0xdeadbeef);
}

---


A little void casting inside the function can do the trick. You will have to pass the name separately though.


im off to bed ttyl
September 30, 2021

On Thursday, 30 September 2021 at 02:09:50 UTC, Hipreme wrote:

>

I could reduce by almost 100kb of code by instead of using the former option, I use:

yes this is what id o you can list the Ts in an interface too. see my simpledisplay.d interface GL for example

>

I do still don't think that this should be the answer, as the alias problem is not yet solved. It takes a lot of size for functions that could be only syntactic sugar, where C would be a lot better

well it must generate functions for each thing there by definition and since you call them at runtime it can't cut them out either. so this is kinda meh

you MIGHT be able to do a tempalte that forwards to a single runtime function though. but i seriously need to g2g maybe can look tomorrow

September 30, 2021

On Thursday, 30 September 2021 at 01:44:57 UTC, jfondren wrote:

>

On Thursday, 30 September 2021 at 01:09:47 UTC, Hipreme wrote:

>

I have a template function that all it does is given a symbol, it loads a dll for its type + its name:

void loadSymbol(alias s, string symName = "")()
{
	static if(symName == "")
		s = cast(typeof(s))_loadSymbol(_dll, (s.stringof~"\0").ptr);
	else
		s = cast(typeof(s))_loadSymbol(_dll, (symName~"\0").ptr);
}

If symName is always a string literal, then you don't need to append a "\0"; string literals always have a trailing NUL after them, and they autoconvert to const(char)[] so you don't need the .ptr either.

Playing around with this, with a mock-up of your code, doesn't change object size at all.

Is the actual source available, that you're seeing this with?

September 30, 2021

On Thursday, 30 September 2021 at 02:22:35 UTC, jfondren wrote:

>

On Thursday, 30 September 2021 at 01:44:57 UTC, jfondren wrote:

>

On Thursday, 30 September 2021 at 01:09:47 UTC, Hipreme wrote:

>

I have a template function that all it does is given a symbol, it loads a dll for its type + its name:

void loadSymbol(alias s, string symName = "")()
{
	static if(symName == "")
		s = cast(typeof(s))_loadSymbol(_dll, (s.stringof~"\0").ptr);
	else
		s = cast(typeof(s))_loadSymbol(_dll, (symName~"\0").ptr);
}

If symName is always a string literal, then you don't need to append a "\0"; string literals always have a trailing NUL after them, and they autoconvert to const(char)[] so you don't need the .ptr either.

Playing around with this, with a mock-up of your code, doesn't change object size at all.

Is the actual source available, that you're seeing this with?

https://github.com/MrcSnm/HipremeEngine/tree/hotload/api

You may try messing at the module api.graphics.g2d.renderer2d

There is a function called initG2D

Try changing it from loadSymbols to each one loadSymbol.

You can just dub at the folder api.

« First   ‹ Prev
1 2