digitalmars.D.learn - 0 cost template instantiation
- Hipreme (26/26) Sep 29 2021 I have a template function that all it does is given a symbol, it
- jfondren (5/16) Sep 29 2021 If symName is always a string literal, then you don't need to
- jfondren (4/21) Sep 29 2021 Playing around with this, with a mock-up of your code, doesn't
- Hipreme (6/28) Sep 29 2021 https://github.com/MrcSnm/HipremeEngine/tree/hotload/api
- Basile B. (3/19) Sep 29 2021 cant you just use a regular functions ? loading happens at
- Hipreme (12/34) Sep 29 2021 The entire reason to having that function is having that syntax
- Basile B. (6/41) Sep 29 2021 well ok. Maybe try to see if overloads
- Adam D Ruppe (18/25) Sep 29 2021 ---
- Hipreme (16/42) Sep 29 2021 I could reduce by almost 100kb of code by instead of using the
- Adam D Ruppe (9/15) Sep 29 2021 yes this is what id o you can list the Ts in an interface too.
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
Sep 29 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.
Sep 29 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: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?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.
Sep 29 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: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.On Thursday, 30 September 2021 at 01:09:47 UTC, Hipreme wrote: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?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.
Sep 29 2021
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.
Sep 29 2021
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.aThe 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.
Sep 29 2021
On Thursday, 30 September 2021 at 03:21:51 UTC, jfondren wrote:On Thursday, 30 September 2021 at 03:13:28 UTC, jfondren wrote: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.As provided, loadSymbol without the "static if": 965K libhipengine_api.a with `mixin(loadSymbol("name"))` instead of a template: 749K libhipengine_api.aThe 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.
Sep 29 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 ?
Sep 29 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: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 inlinedI 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 ?
Sep 29 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: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.On Thursday, 30 September 2021 at 01:09:47 UTC, Hipreme wrote: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 inlinedI 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 ?
Sep 29 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
Sep 29 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 sugarI 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
Sep 29 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 exampleI 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 betterwell 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
Sep 29 2021