]> OCCT Git - occt.git/commitdiff
Foundation Classes - Optimize NCollection_Array1 with type specific (#608)
authorPasukhin Dmitry <dpasukhi@opencascade.com>
Sat, 12 Jul 2025 19:05:55 +0000 (20:05 +0100)
committerGitHub <noreply@github.com>
Sat, 12 Jul 2025 19:05:55 +0000 (20:05 +0100)
- Replaced parameterless `construct()` and `destroy()` methods with parameterized versions that specify ranges
- Updated type trait checks from `is_arithmetic` to `is_trivially_default_constructible` and `is_trivially_destructible`
- Removed redundant local variable `anOldSize` in the `Resize` method

src/FoundationClasses/TKernel/NCollection/NCollection_Array1.hxx

index a051bfd0b63b894d977a62ce5405e0d0be51c567..6659d8d13b6b88cb418ef2dae5dedd5aad86063c 100644 (file)
@@ -111,7 +111,7 @@ public:
     }
     myPointer = myAllocator.allocate(mySize);
     myIsOwner = true;
-    construct();
+    construct(0, mySize);
   }
 
   explicit NCollection_Array1(const allocator_type&  theAlloc,
@@ -129,7 +129,7 @@ public:
     }
     myPointer = myAllocator.allocate(mySize);
     myIsOwner = true;
-    construct();
+    construct(0, mySize);
   }
 
   explicit NCollection_Array1(const_reference        theBegin,
@@ -147,7 +147,7 @@ public:
     }
     myPointer = myAllocator.allocate(mySize);
     myIsOwner = true;
-    construct();
+    construct(0, mySize);
   }
 
   //! Copy constructor
@@ -180,7 +180,7 @@ public:
     {
       return;
     }
-    destroy();
+    destroy(myPointer, 0, mySize);
     myAllocator.deallocate(myPointer, mySize);
   }
 
@@ -238,7 +238,7 @@ public:
     }
     if (myIsOwner)
     {
-      destroy();
+      destroy(myPointer, 0, mySize);
       myAllocator.deallocate(myPointer, mySize);
     }
     myLowerBound       = theOther.myLowerBound;
@@ -337,9 +337,8 @@ public:
   {
     Standard_RangeError_Raise_if(theUpper < theLower, "NCollection_Array1::Resize");
     const size_t aNewSize     = static_cast<size_t>(theUpper - theLower + 1);
-    const size_t anOldSize    = mySize;
     pointer      aPrevContPnt = myPointer;
-    if (aNewSize == anOldSize)
+    if (aNewSize == mySize)
     {
       myLowerBound = theLower;
       return;
@@ -349,13 +348,12 @@ public:
       if (theToCopyData)
         destroy(myPointer, aNewSize, mySize);
       else
-        destroy();
+        destroy(myPointer, 0, mySize);
     }
     myLowerBound = theLower;
-    mySize       = aNewSize;
     if (theToCopyData)
     {
-      const size_t aMinSize = std::min<size_t>(aNewSize, anOldSize);
+      const size_t aMinSize = std::min<size_t>(aNewSize, mySize);
       if (myIsOwner)
       {
         myPointer = myAllocator.reallocate(myPointer, aNewSize);
@@ -365,15 +363,16 @@ public:
         myPointer = myAllocator.allocate(aNewSize);
         copyConstruct(aPrevContPnt, aMinSize);
       }
-      construct(anOldSize, aNewSize);
+      construct(mySize, aNewSize);
     }
     else
     {
       if (myIsOwner)
         myAllocator.deallocate(aPrevContPnt, mySize);
       myPointer = myAllocator.allocate(aNewSize);
-      construct();
+      construct(0, aNewSize);
     }
+    mySize    = aNewSize;
     myIsOwner = true;
   }
 
@@ -397,30 +396,17 @@ protected:
 
 protected:
   template <typename U = TheItemType>
-  typename std::enable_if<std::is_arithmetic<U>::value, void>::type construct()
+  typename std::enable_if<std::is_trivially_default_constructible<U>::value, void>::type construct(
+    const size_t,
+    const size_t)
   {
     // Do nothing
   }
 
   template <typename U = TheItemType>
-  typename std::enable_if<!std::is_arithmetic<U>::value, void>::type construct()
-  {
-    for (size_t anInd = 0; anInd < mySize; anInd++)
-    {
-      myAllocator.construct(myPointer + anInd);
-    }
-  }
-
-  template <typename U = TheItemType>
-  typename std::enable_if<std::is_arithmetic<U>::value, void>::type construct(const size_t,
-                                                                              const size_t)
-  {
-    // Do nothing
-  }
-
-  template <typename U = TheItemType>
-  typename std::enable_if<!std::is_arithmetic<U>::value, void>::type construct(const size_t theFrom,
-                                                                               const size_t theTo)
+  typename std::enable_if<!std::is_trivially_default_constructible<U>::value, void>::type construct(
+    const size_t theFrom,
+    const size_t theTo)
   {
     for (size_t anInd = theFrom; anInd < theTo; anInd++)
     {
@@ -429,32 +415,19 @@ protected:
   }
 
   template <typename U = TheItemType>
-  typename std::enable_if<std::is_arithmetic<U>::value, void>::type destroy()
-  {
-    // Do nothing
-  }
-
-  template <typename U = TheItemType>
-  typename std::enable_if<!std::is_arithmetic<U>::value, void>::type destroy()
-  {
-    for (size_t anInd = 0; anInd < mySize; anInd++)
-    {
-      myAllocator.destroy(myPointer + anInd);
-    }
-  }
-
-  template <typename U = TheItemType>
-  typename std::enable_if<std::is_arithmetic<U>::value, void>::type destroy(pointer,
-                                                                            const size_t,
-                                                                            const size_t)
+  typename std::enable_if<std::is_trivially_destructible<U>::value, void>::type destroy(
+    pointer,
+    const size_t,
+    const size_t)
   {
     // Do nothing
   }
 
   template <typename U = TheItemType>
-  typename std::enable_if<!std::is_arithmetic<U>::value, void>::type destroy(pointer      theWhat,
-                                                                             const size_t theFrom,
-                                                                             const size_t theTo)
+  typename std::enable_if<!std::is_trivially_destructible<U>::value, void>::type destroy(
+    pointer      theWhat,
+    const size_t theFrom,
+    const size_t theTo)
   {
     for (size_t anInd = theFrom; anInd < theTo; anInd++)
     {