135 points · by atilaneves · 7y ago · 116 comments
I understand that the point of the code is to enumerate them without knowing that they can be parametrised, but the parametrisation in this case is not so scary, so it's a fun bit of trivia for programmers (for mathematicians, it's more fundamental, as the Pythagorean parametrisation is an important prototype for counting rational points on elliptic curves, a fundamental problem in number theory).
https://en.wikipedia.org/wiki/Pythagorean_triple#Generating_...
Slightly less easy nor well-known amongst mathemticians (or at least, this is new to me), you can enumerate all primitive Pythagorean triples in a tree!
https://en.wikipedia.org/wiki/Tree_of_primitive_Pythagorean_...
This tree is really interesting! I wonder where those matrices come from. The proof that it works is a boring mechanical check, but I'm now interested in how they were derived in the first place. This is a 20th century discovery, unlike the parametrisation which comes from antiquity. I think I'll be reading some papers about this.
When you work out this math, you recover the classical parametrisation, where the slope m/n of the line corresponds to the parameters, and this line-intersection method is a good inspiration for how to do the same with some higher-order curves such as elliptic curves, where it leads to the group law on them.
https://en.wikipedia.org/wiki/Fibonacci_number#Binet's_formu...
The only math stuff I kept updated was denotional semantics and linear algebra, due to compiler and 3D papers that I regularly read, which is anyway not something any of my peers bothers to do.
I suspect the experience here will differ quite a bit depending on what you're working on and what your preferred workflow looks like.
> Do you just give up and resort to printf debugging
FWIW, I've been printf debugging since I started programming, regardless of the language I use. To be fair, if I spent more time in a predominantly memory unsafe language, then I'd probably use a debugger more.
The reason is because I write a custom printf to print exactly what I need to know. Debuggers just bury you in irrelevant output.
A fair bit of the D programming language design is there to head off the detail mistakes people make. A more subtle thing in the language design is to make the designs less prone to error.
It's a problem with any kind of compiled language that runs through an optimiser, whether C, C++, Rust, or D. Maybe that's why so many people just don't use debuggers, although my philosophy is that everyone should use a debugger, but maybe they just haven't found the right one yet.
A variable doesn't have to be in a single register throughout an entire function, or even present in registers / memory at all! DWARF is capable of representing storage locations that change over the course of a subroutine, as well as synthesizing values from other registers/memory, simple operations, and constants. Compilers can do a lot better (although certainly not 100%) than they do today without sacrificing any of the optimizations they make, and especially Clang/LLVM have a lot of room for improvement.
I highly recommend this blog post on the subject: https://backtrace.io/blog/engineering/compile-once-debug-twi...
I find that I tend to write more and smaller unit tests than people who reach for debuggers. I debug logic errors by using println or writing more tests.
And yes, Rust is still very susceptible to logic errors.
I only step into a debugger when I'm really wtf'd, and I don't think that's happened to me even once with Rust (it used to happen a lot with C++, since printf debugging mixes so poorly with segfaults etc). No question that long compile times are a pain though. I generally use 'cargo check' 99.99% of the time.
Thankfully there's work going into making debug builds faster by, if I recall, replacing the llvm backend with some other thing I don't recall the name of.
In the optimised (O2/O3) builds the compiler effectively did that optimisation anyway, but not at O0.
It meant loop code in the source was longer, but made measurably (~10% if I remember correctly) faster debug builds on code which did a lot of vector iterating.
I have also noticed that often some of debug builds' slowness is actually due to asserts being on in debug builds as opposed to optimised builds, at least in some of the code I work on (which in some cases makes very heavy use of them) - removing asserts can in some cases bring things back to debug builds being ~5x slower than optimised compared to ~30x which is more acceptable.
Despite the changes people are submitting, it's good to know about these different gotchas. And there may be one or two actual compiler enhancements that could be made here also.
This pull request has timings with this changed: https://github.com/atilaneves/pythagoras/pull/1#issuecomment...
Not everyone is micro-optimizing their code just to save a couple of needless extra ms.
I never, ever since 1993, had to work on a C++ project where stdio vs iostream performance made any worthwhile difference, besides stdio being less secure to use.
CT = compile time
RT = run time1. https://raw.githubusercontent.com/atilaneves/pythagoras/mast...
2. https://raw.githubusercontent.com/atilaneves/pythagoras/mast...
“I believe the problem with ..= is that x..(y + 1) is not identical with x..=y. They are nearly equivalent on most values for x and y, except when y == i32::max(). At that point, x..=y should still be able to function, while x..(y + 1) is allowed to do nothing.”
As a C programmer, that sort of feels like cheating ;)
I have no idea why LLVM misses optimizing this in Rust's case. Likely it is fed much trickier IR. It also misses the optimization that it does in the C case of lifting the xx and zz multiplications to the appropriate loop level.
But I also note that the original version of your benchmark was calling `println!`, which means that you were essentially benchmarking high-level, thread-safe I/O. EDIT: Not in this case, see the thread below.
Some Rust benchmarking tips:
1. Always run `cargo build --release`. Debug builds are slow.
2. Be sure to use `stdin.lock()` or `stdout.lock()` when reading or writing standard I/O. If you don't, Rust has to lock standard I/O for you on each call. This means using `writeln!` instead of `println!`.
I don't think so. The program is only doing a thousand prints, which is far less than the total number of loop iterations. Try changing to locked stdout and writeln!---I doubt you'll notice a difference.
--------------------------
> Rust println!() flushes after each call,
To be clear, println! does not flush after each call.
Many terminals are line-buffered, and so when println! emits a \n, the terminal will flush. So, println! will probably cause a flush, but does not flush itself. println! is the exact same as `print!`, except for the additional printing of \n https://doc.rust-lang.org/stable/src/std/macros.rs.html#157-...
For these simple examples an strace may be enlightening in this regard.
Edit: Indeed this seems to be the case from the library source code (println! -> _eprint -> stdout -> LineWriter).
Edit: I've tried (println! vs a single io::stdout::lock() and writeln!), and it doesn't seem to make any difference, at least for the range.rs example. Hmm.
I remember this was simple to implement in Julia [2] where you can iterate over the full range `for i = typemin(Int):typemax(Int) ... end` without overflow and without overhead. Same should be possible in Rust I think.
[1] https://doc.rust-lang.org/src/core/iter/range.rs.html#340-35...
[2] https://github.com/JuliaLang/julia/blob/master/base/range.jl...
Honest question: why should I use ranges after all?
As I understand, ranges are the core of idiomatic D and they are not worth it (as per OP)?
AFAIK, ranges (at least in D) are not thread-safe [3]. So what real benefits does it bring?
[1] https://accu.org/content/conf2009/AndreiAlexandrescu_iterato...
[2] http://www.informit.com/articles/printerfriendly/1407357
[3] https://github.com/carun/parallel-read-tester/commit/3e69da4...
No it wasn't. Boost.Range library predates Andrei's Range talk in 2009. Boost.Range was introduced in boost 1.32, which was released in 2004:
https://www.boost.org/users/history/version_1_32_0.html
And from Boost.Range's "History and Acknowledgement" it explains where the term came from:
> The term Range was adopted because of paragraph 24.1/7 from the C++ standard
https://www.boost.org/doc/libs/1_32_0/libs/range/doc/history...
Furthermore, what is being standardized in C++ is an expansion of what is in Boost.Range, which uses iterators underneath.
Andrei's term for ranges(and what is in D) are actually quite different as it removes the basis for iterators completely.