3 ms·
I thought this was about adopting a programming style where each variable is assigned to only once. But it was something else entirely. I was interested becaus
by WalterBright 7y ago
I thought this was about adopting a programming style where each variable is assigned to only once. But it was something else entirely.
I was interested because I've started adopting the single assignment style in my own code, and like the results (the code is easier to comprehend).
- glouwbug 7y agoAgreed, I'm currently taking a similar approach, but forgoing pointers as much as possible as well. This leads to structs being passed by value, and const abuse. Ray h_cast(const Hero hero, const Hit hit, const Sheer sheer, const int yres, const int xres) { const Point end = p_sub(hit.where, hero.where); const Point corrected = p_turn(end, -hero.yaw); const Line trace = { hero.where, hit.where }; const Projection projection = p_project(yres, xres, hero.fov.a.x, hero.pitch, corrected, hero.height); const Ray ray = { trace, corrected, p_sheer(projection, sheer), hit.surface, hit.offset, hero.torch }; return ray; } It leads to some nice somewhat functional styles. I estimate its 80% pure, given its C99 alone. I've adopted this style in my Age of Empires 2 engine rewrite if anyone is curious: https://github.com/glouw/openempires https://github.com/glouw/openempires As for performance I am heavily relying on LTO. I seem to get a 50% performance boost with pass by values on structs - as I've sedded openempires to replace occurrences of _Type name_ with C++'s _Type& name_ for all functions arguments - and the added pointer aliasing and de-references bogged the engine down in heavily forested areas. Compilers love singly assigned const values, and seem to suffer under heavy pointer aliasing.
- saagarjha 7y ago> I've sedded openempires to replace occurrences of _Type name_ with C++'s _Type& name_ for all functions arguments This isn’t a valid transformation :/ > Compilers love singly assigned const values I mean, they’re going to convert to SSA for you anyways. > and seem to suffer under heavy pointer aliasing Have you tried restrict?
- voldacar 7y ago> This leads to structs being passed by value do compilers generally optimize this away on -O3, or do you just accept the performance hit in exchange for prettier code?
- asymptotically2 7y agoIf it's a static method (or in an anonymous namespace in C++), it should get fixed at any optimisation level. However if it's a global method, you'll need to pass -flto to defer optimisations to link time to make it work.
- ternaryoperator 7y ago>I was interested because I've started adopting the single assignment style in my own code What benefit have you seen from doing this? Genuinely curious.
- guerrilla 7y agoIt's easier for compilers to optimize. https://en.wikipedia.org/wiki/Static_single_assignment_form https://en.wikipedia.org/wiki/Static_single_assignment_form
- nn3 7y agoIt actually is not. The compiler has no problems turning your imperative code into SSA on its own, and PHI functions are no problem for optimizers. I guess it's just for readability and easier reasoning about the code.
- ternaryoperator 7y agoYou're not the OP of the comment, who is a compiler writer (C, C++, D). I hope he'll weigh in with his rationale.
- masklinn 7y agoAny MFA code is trivially converted to SSA and that's pretty much the first thing your compiler will do. So doing SSA by hand makes no real difference to the compiler unless your compiler is so trivial it doesn't start by converting its input to SSA.
- Animats 7y agoThat's what I thought, too. There's a lot to be said for real single-assignment programming. It provides many of the advantages of functional programming. The code is more readable, because the intermediate variables have names. When you need to do something imperative, you don't have to jump through hoops to do it. Go and Rust both encourage that style - the default is to create a new variable, not assign to an old one. (The semantics of multiple := assignment in Go, where some variables are assigned and others are created, though...) One of the historical headaches of C is that function parameters are non-const by-value by default. That's backwards. The default should be const ref. If you need a local mutable copy, or the parameter is an "in-out", as Ada calls it, you should have to specify that. Those are the less likely cases and the ones that cause trouble if missed. Passing small const ref values by value is a compiler optimization; the programmer shouldn't have to worry about it, especially since whether it's a win varies with the target machine. The language in the article seems more like a variant of Matlab, not C.
- johnthescott 7y agoi recall a micro language built around the infamous brainfuck that has a non destructive read for function arguments. seems like a bigger concept exists. let the caller determine what the called can modify.
- wirrbel 7y agoSingle assignment style has structural similarities to continuation-passing style in FP iirc.