Re:When zero cost abstractions aren't zero cost
by erk__ on Monday August 09, 2021 @11:19PM
(#28124850)
Doing some further research you can make the optimized initialization of the WrappedByte vector by using it as a iterator.
vec![0u8; len]
.into_iter()
.map(|x| WrappedByte(x))
.collect::<Vec<_>>()
which will give you a list of WrappedBytes but initialized with calloc. I assume this is because llvm can see that the map is a identity function in this case and then can optimize it out.Godbolt: https://godbolt.org/z/hxnnPcjvG
Re:When zero cost abstractions aren't zero cost
by topspin on Tuesday August 10, 2021 @12:07AM
(#28125057)
That we might expect compilers to boil this statement down to calloc(n, s) is astonishing. The subtilty compilers is amazing.
Re:When zero cost abstractions aren't zero cost
by tialaramex on Tuesday August 10, 2021 @07:41AM
(#28127587)
If you write some of the "clever" pop count algorithms in C or C++, good compilers will go "Oh you're doing bit counting" and on a modern platform with a bit counting CPU instruction your whole algorithm turns into one CPU instruction.
[In Rust the standard library provides count_ones() on integer types, and that's actually safely wrapping an intrinsic pop count feature]
Re:When zero cost abstractions aren't zero cost
by madmoose on Tuesday August 10, 2021 @10:31AM
(#28129303)
C++ also has std::popcount since C++20. It should probably have had that from the beginning.
godbolt: https://godbolt.org/z/8bM7zz4E7
the specific specialization: https://github.com/rust-lang/rust/blob/master/library/alloc/...