News for hackers. Stuff that matters.
Re:Making the obvious code fast (2016) by barrkel on Wednesday April 17, 2019 @09:15AM (#19682224)
> What I would like to see is more of an attitude change among high level language designers and their communities.

The reason a language like C makes obvious code fast is because C has very little expressive power. The lack of power strongly encourages the programmer to think imperatively and code at a very low level. Apply this to an extremely trivial problem (mere addition - does it get any simpler?), and of course you end up with fast code.

The lesson doesn't generalize. The lack of expressive power means higher level abstractions are clumsy to write, refactoring is more expensive, and recomposing your program with e.g. a caching layer is much harder.

See https://blog.codinghorror.com/on-managed-code-performance-ag... for example (alas, it appears some of the original source blogs are gone) - Raymond Chen wrote a dictionary in C++, Rico Mariani implemented it in C# and and got great performance with the obvious solution.

Re:Making the obvious code fast (2016) by gameswithgo on Wednesday April 17, 2019 @02:16PM (#19685052)
Rust managed to make some of these high level abstractions compile to fast machine code almost every time. Its quite refreshing to go from Linq/Streams etc to Rust iterators.

So does C++ iterators for that matter but they are not so pleasant to actually write.

Re:Making the obvious code fast (2016) by barrkel on Wednesday April 17, 2019 @03:03PM (#19685443)
It's true. Rust's approach to passing lambdas to iterators usually gives the compiler good info to inline. Some of my exercise programs when learning Rust exceeded fairly straightforward C implementation too, possibly due to stronger guarantees around aliasing.
Re:Making the obvious code fast (2016) by kingaillas on Wednesday April 17, 2019 @11:28AM (#19683412)
>See https://blog.codinghorror.com/on-managed-code-performance-ag.... for example (alas, it appears some of the original source blogs are gone)

They are there... it's just the recent migration at MSDN blogs broke a lot of links. Basically it looks like Rico doesn't blog anymore (no longer at Microsoft) so his blog is archived and links to it work, but Raymond Chen's blog is active and was moved en masse to new links.

The links from Rico Mariani's blog (https://blogs.msdn.microsoft.com/ricom/2005/05/10/performanc...) are here:

Raymond Chen's posts:

https://devblogs.microsoft.com/oldnewthing/20050509-30/?p=35...

https://devblogs.microsoft.com/oldnewthing/20050510-55/?p=35...

https://devblogs.microsoft.com/oldnewthing/20050511-46/?p=35...

https://devblogs.microsoft.com/oldnewthing/20050513-26/?p=35...

https://devblogs.microsoft.com/oldnewthing/20050516-30/?p=35...

https://devblogs.microsoft.com/oldnewthing/20050518-42/?p=35...

https://devblogs.microsoft.com/oldnewthing/20050519-00/?p=35...

Re:Making the obvious code fast (2016) by tedunangst on Wednesday April 17, 2019 @10:23AM (#19682812)
But isn't the point that sufficiently smart compilers are supposed to compile to faster than C?
Re:Making the obvious code fast (2016) by barrkel on Wednesday April 17, 2019 @10:58AM (#19683117)
Are you being facetious? :) "Sufficiently smart compiler" - http://wiki.c2.com/?SufficientlySmartCompiler

Don't get me wrong, I like C - especially on small problems. I don't get the feeling that I'm creating something flexible with C, though, it's very much bespoke and tailored. I get involved in micromanagement of fine details. This is OK for a small problem, but extremely aggravating for a big problem. As soon as I want higher level abstractions, and start building structs of function pointers and whatnot, I just shake my head - I'd rather write a parser and corresponding text generator than manually compile that into C.

C++ has been hobbled by the concept of a sufficiently smart compiler. It's so afraid of introducing abstractions that aren't cost-free, it tries to make them all optional, and the resulting explosion in feature combination means some combinations don't work well together, and then every big C++ project has to decide which set of features they'll adopt, and which ones they won't.

Give me the freedom to think closer to the problem, and even if the abstractions have some costs, and even if the compilers aren't quite sufficiently smart, the chances are I'll come up with algorithm improvements that meet speed and space requirements and let me move on to the next problem much sooner. Agility trumps hyper-optimization.

Re:Making the obvious code fast (2016) by jstimpfle on Wednesday April 17, 2019 @05:55PM (#19686817)
According to some people, the problem is the data. C is a nice language for dealing with plain data.

Just today I rewrote my own streams abstraction, because libc sucks and I want to support my own printf style functions and my own custom streams (for example dynamically memory allocating streams. fopencookie() is not portable). I think that streams are one of the most abstract and most useful abstractions. It went pretty good. Making a few vtables explicitly in a few places is not that terrible.

Re:Making the obvious code fast (2016) by maxxxxx on Wednesday April 17, 2019 @12:52PM (#19684245)
That has been the dream for a long time. When Java introduced JIT (Hotspot?) a lot of people claimed that Java was faster than C. Lately people are telling me that JavaScript "can" run faster than C. Usually they present some well written code in their favorite language and then compare it to horribly written C. Usually I tell them that most likely the runtime of their language is written in C so you can always do as well in C.
Re:Making the obvious code fast (2016) by saagarjha on Wednesday April 17, 2019 @02:34PM (#19685207)
> Usually I tell them that most likely the runtime of their language is written in C so you can always do as well in C.

The point is that with a JIT the runtime is executing as little as possible, with the rest being dynamically optimized machine code. So this isn’t really a fair thing to say…

Re:Making the obvious code fast (2016) by gameswithgo on Wednesday April 17, 2019 @03:02PM (#19685439)
I've never seen in practice any jit do better than hand tuned AOT compiled code.
Re:Making the obvious code fast (2016) by barrkel on Wednesday April 17, 2019 @05:19PM (#19686576)
I've seen lambdas and virtuals inlined dynamically. AOT would need to be statically typed and instantiated all the way to do the same thing.

I could create a sample program that would be hard to make as fast in C or C++, with the constraint that the code needs to be dynamically loaded at runtime.

In practice, it's easier to modify a more dynamic program and be sure you're not breaking much. Highly tuned C tends to bake in assumptions about memory lifetime, layouts, call graph, etc. The flexibility to modify is what gives you the ability to target higher level improvements in a shorter amount of time.

Given any concrete implementation, after it's been designed in a more dynamic language, you can convert it to something fast in something like C. But starting from C is probably not a great idea. And not everything can be written multiple times economically.

E.g. the .net GC was prototyped in Lisp, and initially converted to C++ with a translator.

Re:Making the obvious code fast (2016) by pjmlp on Wednesday April 17, 2019 @05:37PM (#19686699)
I have, try to make an AOT compiler remove dynamic dispatch across shared libraries and convert it into either direct calls or even inline them.
Re:Making the obvious code fast (2016) by saagarjha on Wednesday April 17, 2019 @03:42PM (#19685787)
It's rare, but it can happen occasionally. A just-in-time compiler has access to tracing and profiling that a ahead-of-time compiler may not, which means that it can do things like inline a virtual method call that is actually monomorphic at runtime.
Re:Making the obvious code fast (2016) by maxxxxx on Wednesday April 17, 2019 @04:02PM (#19685964)
“occasionally”

That’s the keyword here.

Re:Making the obvious code fast (2016) by saagarjha on Wednesday April 17, 2019 @09:11PM (#19687878)
I was responding to a comment that had the word "never" in it.