This email is mostly intended to Eigen's developers, but any users concerned about Eigen's dynamic aligned memory allocation mechanism is welcome to share its thoughts and expertise.
As discussed in bugs 761 and 779, our dynamic memory allocation mechanism would deserve some cleaning.
As you can see there , the current logic is rather complicated and thus prone to bugs and it is difficult to predict what will be used under the hood.
I did some testing on both Linux and OSX, and I found that both posix_memalign and _mm_malloc are slightly slower than our handmade_aligned_malloc which is as fast as the non-aligned std::malloc (probably thanks to inlining). I observed almost a factor 2 for 32 bytes alignment.
Regarding memory overhead, there is no winners for 32 bytes alignment, but for 16 byes alignment, std::malloc is clearly better as it is already aligned on most systems.
With that in mind, we can remove both the posix_memalign and _mm_malloc paths without any regrets.This means that on Unix, std::malloc will never be bypassed anymore.
It remains the question of windows and its _aligned_malloc routine that I did not benched. For consistency with unix systems, I would propose to remove that path too.
Then, I also propose to keep EIGEN_MALLOC_ALREADY_ALIGNED as it is clearly better regarding memory consumption. In case of trouble, one can still compile with -DEIGEN_MALLOC_ALREADY_ALIGNED=0 to bypass the default assumptions (e.g. if using a custom non-aligned malloc). Nonetheless, to be future proof, I propose to extend this notion of EIGEN_MALLOC_ALREADY_ALIGNED to something more general as EIGEN_MALLOC_DEFAULT_ALIGNMENT whose value would be the default alignment in bytes (e.g., 8, 16, 32?).
Finally, to reduce the overhead of handmade_aligned_malloc for tiny buffers, I propose to disable the alignment of buffers smaller than the requested alignment.. This can easily be done by passing the buffer size to aligned_free()..
Those changes should be the latest major ones before Eigen 3.3 beta2 and the RCs...