News for hackers. Stuff that matters.
Re:Random Acts of Optimization by saosebastiao on Thursday October 29, 2015 @05:06PM (#10474285)
Actually, they can, and quite well actually. The dotnet native compiler actually does quite a few analyses for "hotness", mostly centered around maintaining cache locality. However if there is a lot of entropy in the possible code paths, it might not be computationally feasible to analyze the hotness of all of the paths. In those cases, we accept heuristic substitutes, of which both JITs and PGO have shown good results.
Re:Random Acts of Optimization by haberman on Thursday October 29, 2015 @05:51PM (#10474619)
> it might not be computationally feasible to analyze the hotness of all of the paths

It has nothing to do with computational feasibility. It has to do with the fact that it depends on the input. In many cases there simply is not enough information at compile time to determine which paths are hotter than others.

For example, error paths are generally very cold, but how is a compiler supposed to know that a path is an error path? They just look like regular conditionals.

> In those cases, we accept heuristic substitutes, of which both JITs and PGO have shown good results.

JIT and PGO aren't heuristic, they are based on measurement. By your logic, if I look at the speedometer in my car that is just a "heuristic" of my speed. It's not a heuristic, it's an empirical measurement.

Re:Random Acts of Optimization by saosebastiao on Thursday October 29, 2015 @07:22PM (#10475165)
> It has nothing to do with computational feasibility. It has to do with the fact that it depends on the input. In many cases there simply is not enough information at compile time to determine which paths are hotter than others.

That's a fair point, but still quite a bit overstated. There are a variety of analyses that compilers can and do perform to understand possible input before it ever receives the input. I'm familiar with a handful of probabilistic and set-cardinality estimation techniques used in math optimization pre-solvers (a form of compiler) that do exactly this, and I'm pretty sure both GCC and LLVM already do several similar analyses.

Furthermore, a very large subset of optimizations won't have any path dependence in the optimization that it choses, and a significant subset after that has choice thresholds that are trivially computable given an accurate cost model. When the former happens, profiling gives no advantage at all, and when the latter happens, it is typically more efficient to branch on that threshold than it is to try to guess at which choice is more efficient by measuring sample input.

> For example, error paths are generally very cold, but how is a compiler supposed to know that a path is an error path? They just look like regular conditionals.

That might be true of some languages, but not so with others. Exceptions are obviously error paths, and several strongly typed languages have error paths encoded in the type system.

> JIT and PGO aren't heuristic, they are based on measurement. By your logic, if I look at the speedometer in my car that is just a "heuristic" of my speed. It's not a heuristic, it's an empirical measurement.

But that's the thing, measurement of your input is a heuristic. If your program execution is heavily influenced by its input, and you optimize using profiling, you are assuming that past input is predictive of future input. That assumption is a heuristic. It might be a really good heuristic, but it still is a heuristic.

Re:Random Acts of Optimization by haberman on Thursday October 29, 2015 @07:57PM (#10475363)
> Furthermore, a very large subset of optimizations won't have any path dependence in the optimization that it choses

Hot code should be: optimized for speed, inlined, unrolled (where it helps), kept local with other hot code, and register-allocated with other hot code.

Cold code should be: optimized for size, not inlined, not unrolled, kept away from hot code, and should never be accommodated in register allocation for any reason (ie. should never reduce availability of registers needed by hot paths).

So I don't agree that optimizations should be performed in a path-independent way.

> But that's the thing, measurement of your input is a heuristic.

By that standard, everything is heuristic. The assumption that your cost model matches the actual CPU the code will run on is a heuristic.

Re:Random Acts of Optimization by barrkel on Friday October 30, 2015 @10:07PM (#10481527)
If we followed your advice, the differential between worst case and best case performance would be large.

There are good reasons for preferring a less extreme difference in potential performance for general computing with AOT compiled languages. In particular, we should ensure the worst case performance isn't awful. We wouldn't want e.g. an occasional exception getting thrown to act like a DoS attack on a web server.

Re:Random Acts of Optimization by haberman on Saturday October 31, 2015 @02:21PM (#10483556)
Nothing I was suggesting would create "awful" performance. Inlining and unrolling are not necessary to get reasonable performance -- just try gcc or Clang with -Os.

For an occasional exception to "act like a DoS attack", the cold path would have to be thousands or millions of times slower than the hot paths. But compiler optimizations don't create anywhere near these kinds of constant factors. Even from -O0 to -O3 is more like a 5-10x difference, maybe 100x in extreme cases. The difference between -Os and -O3 is much more modest, more like 0-50%. Nothing that is going to cause anything remotely resembling a DoS.

Re:Random Acts of Optimization by vitalyd on Thursday October 29, 2015 @06:34PM (#10474871)
>For example, error paths are generally very cold, but how is a compiler supposed to know that a path is an error path? They just look like regular conditionals.

At least for .NET that path is likely throwing an exception. If you don't have a profile, statically assuming those paths are cold is likely to be correct.

Re:Random Acts of Optimization by haberman on Thursday October 29, 2015 @07:01PM (#10475035)
That's an interesting point -- my background is more in C and C++-without-exceptions. But in exception-based languages, I can see how the presence of "throw" would be a very useful signal for static analysis.
Re:Random Acts of Optimization by vitalyd on Thursday October 29, 2015 @07:19PM (#10475145)
Yes, this is a side benefit to exceptions (or some other language's way to highlight errors and thus likely cold paths).
Re:Random Acts of Optimization by darkmighty on Thursday October 29, 2015 @07:59PM (#10475375)
> It has to do with the fact that it depends on the input.

A special case of: https://en.wikipedia.org/wiki/No_free_lunch_in_search_and_op...

This does not hold, of course, if you could automatically simulate the users themselves...