From 4886d18b98fbae9b96c30a148a1320158081b07c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carsten=20Gr=C3=A4ser?= <graeser@dune-project.org>
Date: Tue, 28 Mar 2023 21:02:34 +0200
Subject: [PATCH] Switch to std::uint_least32_t for stored indices in
 MatrixIndexSet

Using `std::uint_least32_t` instead of `std::size_t` halves the required memory
and thus also improves performance.
Notice that `std::uint32_t` is optional and `std::uint_fast32_t` is intended to be
fast in terms of computations and may have 64 bits. Here we're interested in reducing
memory and thus bandwith. Hence `std::uint_least32_t`, which is the smallest sufficient
type is most appropriate.
---
 dune/istl/matrixindexset.hh        | 2 +-
 dune/python/istl/matrixindexset.hh | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/dune/istl/matrixindexset.hh b/dune/istl/matrixindexset.hh
index acc4466b4..c6c60bd16 100644
--- a/dune/istl/matrixindexset.hh
+++ b/dune/istl/matrixindexset.hh
@@ -31,7 +31,7 @@ namespace Dune {
    */
   class MatrixIndexSet
   {
-    using Index = std::size_t;
+    using Index = std::uint_least32_t;
 
     // A vector that partly mimics a std::set by staying
     // sorted on insert() and having unique values.
diff --git a/dune/python/istl/matrixindexset.hh b/dune/python/istl/matrixindexset.hh
index 31c4cb8c9..ae2e26722 100644
--- a/dune/python/istl/matrixindexset.hh
+++ b/dune/python/istl/matrixindexset.hh
@@ -3,6 +3,7 @@
 #ifndef DUNE_PYTHON_ISTL_BCRSMATRIX_HH
 #define DUNE_PYTHON_ISTL_BCRSMATRIX_HH
 
+#include <cstdint>
 #include <memory>
 #include <stdexcept>
 #include <string>
@@ -29,7 +30,7 @@ namespace Dune
     void registerMatrixIndexSet(pybind11::handle scope,
                             pybind11::class_<MatrixIndexSet, options...> cls)
     {
-      typedef std::size_t size_type;
+      using size_type = Dune::MatrixIndexSet::size_type;
 
       // two different possible constructors
       cls.def( pybind11::init( [] () { return new MatrixIndexSet(); } ) );
-- 
GitLab