Defer to std::fill_n when filling a dense object with a constant value.

Benchmark numbers are in: https://gitlab.com/libeigen/eigen/-/snippets/2147292

These were produced using the following benchmark (roughly similar to the benchmark reported in #2272 (closed) ) written using the Google benchmark library https://github.com/google/benchmark

The benchmark code looks as follows:

#include <benchmark/benchmark.h>                                                                                                                                                                                                                           
#include <Eigen/Core>                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                           
void BMSetZeroDynamic(benchmark::State& state) {                                                                                                                                                                                                           
  int n = state.range(0);                                                                                                                                                                                                                                  
  Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> x;                                                                                                                                                                                                 
  x.setRandom(n, n);                                                                                                                                                                                                                                       
  for (auto s : state) {                                                                                                                                                                                                                                   
    benchmark::DoNotOptimize(x.setZero(n, n));                                                                                                                                                                                                             
  }                                                                                                                                                                                                                                                        
}                                                                                                                                                                                                                                                          
BENCHMARK(BMSetZeroDynamic)->Range(1, 512);                                                                                                                                                                                                                
                                                                                                                                                                                                                                                           
template<int N>                                                                                                                                                                                                                                            
void BMSetZeroStatic(benchmark::State& state) {                                                                                                                                                                                                            
  Eigen::Matrix<double, N, N> x;                                                                                                                                                                                                                           
  x.setRandom();                                                                                                                                                                                                                                           
  for (auto s : state) {                                                                                                                                                                                                                                   
    benchmark::DoNotOptimize(x.setZero());                                                                                                                                                                                                                 
  }                                                                                                                                                                                                                                                        
}                                                                                                                                                                                                                                                          
BENCHMARK_TEMPLATE(BMSetZeroStatic, 1);                                                                                                                                                                                                                    
BENCHMARK_TEMPLATE(BMSetZeroStatic, 8);                                                                                                                                                                                                                    
BENCHMARK_TEMPLATE(BMSetZeroStatic, 64);                                                                                                                                                                                                                   
BENCHMARK_TEMPLATE(BMSetZeroStatic, 100);                                                                                                                                                                                                                  
BENCHMARK_TEMPLATE(BMSetZeroStatic, 512);                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                           
BENCHMARK_MAIN();   

This closes #2272 (closed)

Edited by Rasmus Munk Larsen

Merge request reports

Loading