July 20, 2016 Dynamic code generation | ||||
---|---|---|---|---|
| ||||
Does D offer any solutions to generate code dynamically? I would like to order based on optimal strategies. This requires effectively hard coding the execution path. A simple example, if (x == true) foo(); else bar(); can be recoded to be foo() or bar() while x is fixed, in my case x is fixed for long periods and therefor the check is unnecessary. I also know when x changes in all cases. This is just a simple example, of course. I would not want to resort to assembly programming to accomplish this. |
July 20, 2016 Re: Dynamic code generation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rufus Smith | On 07/20/2016 06:36 AM, Rufus Smith wrote: > Does D offer any solutions to generate code dynamically? I don't think so. > I would like to > order based on optimal strategies. This requires effectively hard coding > the execution path. > > A simple example, > > if (x == true) > foo(); > else > bar(); > > can be recoded to be foo() or bar() while x is fixed, in my case x is > fixed for long periods and therefor the check is unnecessary. I also > know when x changes in all cases. This is just a simple example, of > course. I would not want to resort to assembly programming to accomplish > this. Just an idea: ---- void main() { setX(true); f(); /* calls foo */ setX(false); f(); /* calls bar */ } void function() f; void setX(bool x) { if (x) f = &impl!true; else f = &impl!false; } void impl(bool x)() { static if (x) foo(); else bar(); } void foo() { import std.stdio; writeln("foo"); } void bar() { import std.stdio; writeln("bar"); } ---- Of course, if this is for optimization purposes, measure first if hard-coded is actually faster than the branch. |
Copyright © 1999-2021 by the D Language Foundation