www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Compiler-recognized template patterns

reply Quirin Schroll <qs.il.paperinik gmail.com> writes:
 From [D Language Foundation March 2024 Monthly Meeting 
Summary](https://forum.dlang.org/post/qwyhlvqintqsnzwnzuwv forum.dlang.org)
 Walter said the way to do that was to have the compiler 
 recognize the template and not instantiate it, but just return 
 the result. He'd done that for a couple of the templates. 
 `Unqual` sounded like a good candidate for that. Then it would 
 still look like a template, but it wouldn't have the cost of a 
 template anymore.
That’s not a good solution, it’s just whack-a-mole. Why not add a pragma so that any template can be like that at the programmer’s discretion? `pragma(nocache)` or something like that. We can add it to any Phobos template and D programmers can add it in their code bases where they see fit.
Jul 23
next sibling parent "H. S. Teoh" <hsteoh qfbox.info> writes:
On Tue, Jul 23, 2024 at 09:36:21AM +0000, Quirin Schroll via Digitalmars-d
wrote:
 From [D Language Foundation March 2024 Monthly Meeting
 Summary](https://forum.dlang.org/post/qwyhlvqintqsnzwnzuwv forum.dlang.org)
 Walter said the way to do that was to have the compiler recognize
 the template and not instantiate it, but just return the result.
 He'd done that for a couple of the templates. `Unqual` sounded like
 a good candidate for that. Then it would still look like a template,
 but it wouldn't have the cost of a template anymore.
That’s not a good solution, it’s just whack-a-mole. Why not add a pragma so that any template can be like that at the programmer’s discretion? `pragma(nocache)` or something like that. We can add it to any Phobos template and D programmers can add it in their code bases where they see fit.
Doubt that would make much of a difference. DMD uses the "allocate and forget" strategy of memory allocation; it doesn't even matter if something is not cached, if memory was allocated while computing it, that memory will stick around. Might as well cache it so that you don't spend even more memory afterwards to recompute it. T -- Once bitten, twice cry...
Jul 23
prev sibling parent reply "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
On 23/07/2024 9:36 PM, Quirin Schroll wrote:
  From [D Language Foundation March 2024 Monthly Meeting 
 Summary](https://forum.dlang.org/post/qwyhlvqintqsnzwnzuwv forum.dlang.org)
 Walter said the way to do that was to have the compiler recognize the 
 template and not instantiate it, but just return the result. He'd done 
 that for a couple of the templates. `Unqual` sounded like a good 
 candidate for that. Then it would still look like a template, but it 
 wouldn't have the cost of a template anymore.
That’s not a good solution, it’s just whack-a-mole. Why not add a pragma so that any template can be like that at the programmer’s discretion? `pragma(nocache)` or something like that. We can add it to any Phobos template and D programmers can add it in their code bases where they see fit.
Turning off caching requires template instantiation to occur. It does not change that. The point is to recognize a template instantiation request as a ``__trait`` which is very cheap to do on the compiler side. So no, turning off caching wouldn't be beneficial.
Jul 23
parent Stefan Koch <uplink.coder googlemail.com> writes:
On Tuesday, 23 July 2024 at 16:00:39 UTC, Richard (Rikki) Andrew 
Cattermole wrote:
 Turning off caching requires template instantiation to occur. 
 It does not change that.

 The point is to recognize a template instantiation request as a 
 ``__trait`` which is very cheap to do on the compiler side.

 So no, turning off caching wouldn't be beneficial.
+1 In general turning off template caching is never beneficial. I have experimented with that in my time.
Jul 24