4 ms·
If a compiler could proof that creating the channels/go routines/what-have-you does not influence the arithmetics (e.g. because only associative operations are
by killercup 11y ago
If a compiler could proof that creating the channels/go routines/what-have-you does not influence the arithmetics (e.g. because only associative operations are used and the order doesn't matter), it could constant-fold the loops and just output 499999500000 directly.
I don't know any compiler (and language implementation) that can do that, though.
- wizzard0 11y agoHaskell maybe?
- danellis 11y agoDoesn't seem out of the question, but it would require a very good type system. I think out of these languages, only Haskell is at that level.
- sirclueless 11y agoAny of the VM languages could probably do this heuristically as well: the "if size == 1" base case depends only on one of the parameters, you could hoist that out of the concurrent thread/coroutine and into the caller. Then the code path that hits only ever assigns to a future/promise/array/whatever and immediately returns so it won't block and has no other side effects so it can run synchronously for less than the cost of a context switch. In general programmers are pretty good about not kicking off expensive computations they don't need to, so the easy pickings here are probably pretty small. And as soon as there are any side effects at all (which includes any writes to shared memory) it becomes very hard to reason about moving computation from one thread to another. So it's easy to see why compiler writers are not super excited to start optimizing across concurrent threads.