diff --git a/src/finiteVolume/fvMesh/wallDist/nearWallDist/nearWallDist.C b/src/finiteVolume/fvMesh/wallDist/nearWallDist/nearWallDist.C
index e79ce51ac8b65461cb69faf5f2a5943b652a59b4..bf5462f2c6ecbd059c9996984f89aa7f70011141 100644
--- a/src/finiteVolume/fvMesh/wallDist/nearWallDist/nearWallDist.C
+++ b/src/finiteVolume/fvMesh/wallDist/nearWallDist/nearWallDist.C
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2016 OpenFOAM Foundation
-    Copyright (C) 2020 OpenCFD Ltd.
+    Copyright (C) 2020,2024 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -27,60 +27,83 @@ License
 \*---------------------------------------------------------------------------*/
 
 #include "nearWallDist.H"
-#include "fvMesh.H"
 #include "cellDistFuncs.H"
 #include "wallFvPatch.H"
-#include "surfaceFields.H"
 
 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
 
 void Foam::nearWallDist::calculate()
 {
-    cellDistFuncs wallUtils(mesh_);
+    const cellDistFuncs wallUtils(mesh_);
 
-    // Get patch ids of walls
-    labelHashSet wallPatchIDs(wallUtils.getPatchIDs<wallPolyPatch>());
+    // Collect indices of wall patches
 
-    // Size neighbours array for maximum possible
+    DynamicList<label> wallPatchIDs(mesh_.boundary().size());
+    label nWalls = 0;
 
-    DynamicList<label> neighbours(wallUtils.maxPatchSize(wallPatchIDs));
+    forAll(mesh_.boundary(), patchi)
+    {
+        if (isA<wallFvPatch>(mesh_.boundary()[patchi]))
+        {
+            wallPatchIDs.append(patchi);
+            nWalls += mesh_.boundary()[patchi].size();
+        }
+        else
+        {
+            // Set distance to 0
+            operator[](patchi) = 0.0;
+        }
+    }
 
 
-    // Correct all cells with face on wall
+    // Collect all mesh faces of wall patches
 
-    const volVectorField& cellCentres = mesh_.C();
+    DynamicList<label> faceLabels(nWalls);
 
-    forAll(mesh_.boundary(), patchi)
+    for (const label patchi : wallPatchIDs)
     {
-        fvPatchScalarField& ypatch = operator[](patchi);
-
         const fvPatch& patch = mesh_.boundary()[patchi];
+        const auto& pp = patch.patch();
 
-        if (isA<wallFvPatch>(patch))
+        forAll(patch, i)
         {
-            const polyPatch& pPatch = patch.patch();
+            faceLabels.append(pp.start()+i);
+        }
+    }
 
-            const labelUList& faceCells = patch.faceCells();
+    const uindirectPrimitivePatch wallPatch
+    (
+        UIndirectList<face>(mesh_.faces(), faceLabels),
+        mesh_.points()
+    );
 
-            // Check cells with face on wall
-            forAll(patch, patchFacei)
-            {
-                wallUtils.getPointNeighbours(pPatch, patchFacei, neighbours);
+    const volVectorField& cellCentres = mesh_.C();
 
-                label minFacei = -1;
+    DynamicList<label> neighbours;
 
-                ypatch[patchFacei] = wallUtils.smallestDist
-                (
-                    cellCentres[faceCells[patchFacei]],
-                    pPatch,
-                    neighbours,
-                    minFacei
-                );
-            }
-        }
-        else
+    nWalls = 0;
+    for (const label patchi : wallPatchIDs)
+    {
+        const fvPatch& patch = mesh_.boundary()[patchi];
+        const labelUList& faceCells = patch.patch().faceCells();
+
+        fvPatchScalarField& ypatch = operator[](patchi);
+
+        forAll(patch, patchFacei)
         {
-            ypatch = 0.0;
+            // Get point connected neighbours (in wallPatch indices!)
+            wallUtils.getPointNeighbours(wallPatch, nWalls, neighbours);
+
+            label minFacei = -1;
+            ypatch[patchFacei] = wallUtils.smallestDist
+            (
+                cellCentres[faceCells[patchFacei]],
+                wallPatch,
+                neighbours,
+                minFacei
+            );
+
+            nWalls++;
         }
     }
 }