Re: 0 cost template instantiation

2021-09-29 Thread Hipreme via Digitalmars-d-learn

On Thursday, 30 September 2021 at 03:21:51 UTC, jfondren wrote:

On Thursday, 30 September 2021 at 03:13:28 UTC, jfondren wrote:

As provided, loadSymbol without the "static if":

965K libhipengine_api.a

with `mixin(loadSymbol("name"))` instead of a template:

749K libhipengine_api.a


The difference goes down to 66K vs. 60K with `dub -brelease 
--compiler=gdc`. ldc's not as slim but still much better than 
dmd, and -brelease is actually much less significant. So dmd's 
the main culprit for the bloat here. huh.


But since you have importPaths-ldc perhaps you weren't using 
dmd to begin with.



Yup. The mixin(loadSymbol("")) seems the best way to do.

I believe that should be the option for loading separated symbols 
while loadSymbols version is the best for loading a lot of 
symbols(IDK how expensive is mixing a lot of functions) as it 
costs 1KB per instantiation.


I'm actually using DMD and LDC, dmd compiles a bit faster, but I 
use ldc for deploying it. Never used GDC.






Re: 0 cost template instantiation

2021-09-29 Thread jfondren via Digitalmars-d-learn

On Thursday, 30 September 2021 at 03:13:28 UTC, jfondren wrote:

As provided, loadSymbol without the "static if":

965K libhipengine_api.a

with `mixin(loadSymbol("name"))` instead of a template:

749K libhipengine_api.a


The difference goes down to 66K vs. 60K with `dub -brelease 
--compiler=gdc`. ldc's not as slim but still much better than 
dmd, and -brelease is actually much less significant. So dmd's 
the main culprit for the bloat here. huh.


But since you have importPaths-ldc perhaps you weren't using dmd 
to begin with.


Re: 0 cost template instantiation

2021-09-29 Thread jfondren via Digitalmars-d-learn

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

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.


The version I checked out, 2874073b54, doesn't have a loadSymbols.

As provided, loadSymbol without the "static if":

965K libhipengine_api.a

with `mixin(loadSymbol("name"))` instead of a template:

749K libhipengine_api.a

https://gist.github.com/jrfondren/d776ccffb105f464b53c53712656a1d3

That's probably disappointing, since it's a much less pleasant 
interface, but what you could do is take the both entire 
`extern(C) void function() beginSprite; ... 
loadSymbol!beginSprite;` blocks and put them in one string 
literal that you process with a function and mixin once. Now 
instead of an annoying macro alternative you have a DSL for 
DLL-extern functions.


Re: 0 cost template instantiation

2021-09-29 Thread Hipreme via Digitalmars-d-learn

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.





Re: 0 cost template instantiation

2021-09-29 Thread jfondren via Digitalmars-d-learn

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?


Re: 0 cost template instantiation

2021-09-29 Thread Adam D Ruppe via Digitalmars-d-learn

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


Re: 0 cost template instantiation

2021-09-29 Thread Adam D Ruppe via Digitalmars-d-learn

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(, "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


Re: 0 cost template instantiation

2021-09-29 Thread Hipreme via Digitalmars-d-learn

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





Re: 0 cost template instantiation

2021-09-29 Thread Basile B. via Digitalmars-d-learn

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.


Re: 0 cost template instantiation

2021-09-29 Thread Hipreme via Digitalmars-d-learn

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


Re: 0 cost template instantiation

2021-09-29 Thread Basile B. via Digitalmars-d-learn

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 ?


Re: 0 cost template instantiation

2021-09-29 Thread jfondren via Digitalmars-d-learn

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.


0 cost template instantiation

2021-09-29 Thread Hipreme via Digitalmars-d-learn
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