]> git.ipfire.org Git - thirdparty/vectorscan.git/commitdiff
raw pointers replaced with smart pointers
authorapostolos <apostolos.tapsas@vectorcamp.gr>
Wed, 15 Sep 2021 10:03:25 +0000 (13:03 +0300)
committerKonstantinos Margaritis <konstantinos@vectorcamp.gr>
Tue, 12 Oct 2021 08:51:34 +0000 (11:51 +0300)
benchmarks/benchmarks.cpp
benchmarks/noodle.cpp
benchmarks/shufti.cpp
benchmarks/truffle.cpp

index 8143589176d1120d889e0cd579182fb9eaf082a2..be224ba9297bdafb9562f29d70094ca0f9b34f6b 100644 (file)
@@ -5,6 +5,7 @@
 #include <time.h>
 #include <functional>
 #include <vector>
+#include <memory>
 
 #define MAX_LOOPS    500000000
 #define MAX_MATCHES  10
@@ -14,7 +15,7 @@ int main(){
     int sizes[] =  {  16000,  32000,  64000, 120000, 1600000, 2000000, 2500000, 3500000, 150000000, 250000000, 350000000, 500000000 };
     const char charset[] = "aAaAaAaAAAaaaaAAAAaaaaAAAAAAaaaAAaaa"; 
     for (size_t i = 0; i < std::size(sizes); i++) {
-        for(int j = 0; j < 4; j++) {
+        for(size_t j = 0; j < std::size(functions); j++) {
             functions[j](sizes[i], MAX_LOOPS / sizes[i], MAX_MATCHES, false);
             functions[j](sizes[i], MAX_LOOPS / sizes[i], MAX_MATCHES, true);  
         } 
@@ -22,15 +23,14 @@ int main(){
     for(size_t i=0; i < std::size(sizes); i++){
         //we imitate the noodle unit tests
         for (int char_len = 1; char_len < 9; char_len++) {
-            char *str = new char[char_len];
+            std::unique_ptr<char []> str ( new char[char_len] );
             for (int j=0; j<char_len; j++) {
                 srand (time(NULL));
                 int key = rand() % + 36 ;
                 str[char_len] = charset[key];
                 str[char_len + 1] = '\0';
             }
-            noodle_benchmarks(sizes[i],  MAX_LOOPS / sizes[i], str,char_len, 0);
-            delete [] str;    
+            noodle_benchmarks(sizes[i],  MAX_LOOPS / sizes[i], str.get(), char_len, 0);  
         }
     }
     return 0;
index db1b6b9f5993b584ccdab9b82a32b9ff7e339725..e5e56b528816a0d423a1e3dd64bc04f13c40fd7a 100644 (file)
@@ -8,6 +8,7 @@
 #include "scratch.h"
 #include <vector>
 #include <chrono>
+#include <memory>
 
 
 struct hlmMatchEntry {
@@ -31,8 +32,8 @@ hwlmcb_rv_t hlmSimpleCallback(size_t to, u32 id,
 
 void noodle_benchmarks(int size, int loops, const char *lit_str, int lit_len, char nocase){
     ctxt.clear();
-    u8 *data = new u8[size];
-    memset(data, 'a', size);
+    std::unique_ptr<u8 []> data ( new u8[size] );
+    memset(data.get(), 'a', size);
     double total_sec = 0.0;
     u64a transferred_size = 0;
     double avg_time = 0.0;
@@ -45,7 +46,7 @@ void noodle_benchmarks(int size, int loops, const char *lit_str, int lit_len, ch
     struct hs_scratch scratch;
     auto start = std::chrono::steady_clock::now(); 
     for (int i = 0; i < loops; i++){ 
-        noodExec(n.get(), data, size, 0, hlmSimpleCallback, &scratch); 
+        noodExec(n.get(), data.get(), size, 0, hlmSimpleCallback, &scratch); 
     }
     auto end = std::chrono::steady_clock::now();
     total_sec += std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
@@ -62,7 +63,6 @@ void noodle_benchmarks(int size, int loops, const char *lit_str, int lit_len, ch
     /*calculate average bandwidth*/
     bandwitdh = max_bw / loops;
     printf(KMAG "Case with %u matches in random pos with %u * %u iterations," KBLU " total elapsed time =" RST " %.3f s, " 
-               KBLU "average time per call =" RST " %.3f μs," KBLU " bandwidth = " RST " %.3f MB/s," KBLU " average bandwidth =" RST " %.3f MB/s \n",
-               lit_len, size ,loops, total_sec, avg_time, max_bw, bandwitdh);    
-    delete [] data;
+           KBLU "average time per call =" RST " %.3f μs," KBLU " bandwidth = " RST " %.3f MB/s," KBLU " average bandwidth =" RST " %.3f MB/s \n",
+           lit_len, size ,loops, total_sec, avg_time, max_bw, bandwitdh);    
 }
\ No newline at end of file
index d170bfcf24b33ecbe40142f84e1770709e5698e4..f6c2be5cd2457db751418670a198265a60023cd0 100644 (file)
@@ -6,14 +6,15 @@
 #include <cstring>
 #include <ctime>
 #include <cstdlib>
+#include <memory>
 
 void shufti_benchmarks(int size, int loops, int M, bool has_match) { 
     m128 lo, hi;
     ue2::CharReach chars;
     chars.set('a');
     int ret = shuftiBuildMasks(chars, (u8 *)&lo, (u8 *)&hi);
-    u8 *kt1 = new u8[size];
-    memset(kt1,'b',size);
+    std::unique_ptr<u8 []> kt1 ( new u8[size] );
+    memset(kt1.get(),'b',size);
     double total_sec = 0.0;            
     u64a transferred_size = 0;
     double bandwitdh = 0.0;
@@ -28,9 +29,9 @@ void shufti_benchmarks(int size, int loops, int M, bool has_match) {
             kt1[pos] = 'a';
             unsigned long act_size = 0;
             auto start = std::chrono::steady_clock::now();
-            for(int i = 0; i < loops; i++) {
-                const u8 *res = shuftiExec(lo, hi, kt1, kt1 + size);
-                act_size += res - kt1;
+            for(int i = 0; i < loops; i++) { 
+                const u8 *res = shuftiExec(lo, hi, kt1.get(), kt1.get() + size);
+                act_size += res - kt1.get();
             }
             auto end = std::chrono::steady_clock::now();
             double dt = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
@@ -55,7 +56,7 @@ void shufti_benchmarks(int size, int loops, int M, bool has_match) {
     } else {
         auto start = std::chrono::steady_clock::now();
         for (int i = 0; i < loops; i++) {
-            shuftiExec(lo, hi, kt1, kt1 + size);
+            shuftiExec(lo, hi, kt1.get(), kt1.get() + size);
         }
         auto end = std::chrono::steady_clock::now();
         total_sec += std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
@@ -75,7 +76,6 @@ void shufti_benchmarks(int size, int loops, int M, bool has_match) {
                KBLU "average time per call =" RST " %.3f μs ," KBLU " bandwidth = " RST " %.3f MB/s," KBLU " average bandwidth =" RST " %.3f MB/s \n",
                size ,loops, total_sec, avg_time, max_bw, bandwitdh);
     }
-    delete [] kt1;
 }
 
 void rshufti_benchmarks(int size, int loops, int M, bool has_match) {
@@ -83,8 +83,8 @@ void rshufti_benchmarks(int size, int loops, int M, bool has_match) {
     ue2::CharReach chars;
     chars.set('a');
     int ret = shuftiBuildMasks(chars, (u8 *)&lo, (u8 *)&hi);
-    u8 *kt1 = new u8[size];
-    memset(kt1,'b',size);
+    std::unique_ptr<u8 []> kt1 ( new u8[size] );
+    memset(kt1.get(),'b',size);
     double total_sec = 0.0;            
     u64a transferred_size = 0;
     double bandwitdh = 0.0;
@@ -100,8 +100,8 @@ void rshufti_benchmarks(int size, int loops, int M, bool has_match) {
             unsigned long act_size = 0;
             auto start = std::chrono::steady_clock::now();
             for(int i = 0; i < loops; i++) {
-                const u8 *res = rshuftiExec(lo, hi, kt1, kt1 + size);
-                act_size += res - kt1;
+                const u8 *res = rshuftiExec(lo, hi, kt1.get(), kt1.get() + size);
+                act_size += res - kt1.get();
             }
             auto end = std::chrono::steady_clock::now();
             double dt = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
@@ -126,7 +126,7 @@ void rshufti_benchmarks(int size, int loops, int M, bool has_match) {
     } else {
         auto start = std::chrono::steady_clock::now();
         for (int i = 0; i < loops; i++) {
-            rshuftiExec(lo, hi, kt1, kt1 + size);
+            rshuftiExec(lo, hi, kt1.get(), kt1.get() + size);
         }
         auto end = std::chrono::steady_clock::now();
         total_sec += std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
@@ -146,5 +146,4 @@ void rshufti_benchmarks(int size, int loops, int M, bool has_match) {
                KBLU "average time per call =" RST " %.3f μs ," KBLU " bandwidth = " RST " %.3f MB/s," KBLU " average bandwidth =" RST " %.3f MB/s \n",
                size ,loops, total_sec, avg_time, max_bw, bandwitdh);
     }
-    delete [] kt1;
 }
index 077cadba3df36199df266542532266a85c128602..d521c2b112423be0c495434396dfc6260adc063c 100644 (file)
@@ -5,14 +5,15 @@
 #include <chrono>
 #include <cstring>
 #include <ctime>
+#include <memory>
 
 void truffle_benchmarks(int size, int loops, int M, bool has_match) {
     m128 lo, hi;
     ue2::CharReach chars;
     chars.set('a');
     truffleBuildMasks(chars, (u8 *)&lo, (u8 *)&hi);
-    u8 *kt1 = new u8[size];
-    memset(kt1,'b',size);
+    std::unique_ptr<u8 []> kt1 ( new u8[size] );
+    memset(kt1.get(),'b',size);
     double total_sec = 0.0;            
     u64a transferred_size = 0;
     double bandwitdh = 0.0;
@@ -28,8 +29,8 @@ void truffle_benchmarks(int size, int loops, int M, bool has_match) {
             unsigned long act_size = 0;
             auto start = std::chrono::steady_clock::now();
             for(int i = 0; i < loops; i++) {
-                const u8 *res = truffleExec(lo, hi, kt1, kt1 + size);
-                act_size += res - kt1;
+                const u8 *res = truffleExec(lo, hi, kt1.get(), kt1.get() + size);
+                act_size += res - kt1.get();
             }
             auto end = std::chrono::steady_clock::now();
             double dt = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
@@ -54,7 +55,7 @@ void truffle_benchmarks(int size, int loops, int M, bool has_match) {
     } else {
         auto start = std::chrono::steady_clock::now();
         for (int i = 0; i < loops; i++) {
-            truffleExec(lo, hi, kt1, kt1 + size);
+            truffleExec(lo, hi, kt1.get(), kt1.get() + size);
         }
         auto end = std::chrono::steady_clock::now();
         total_sec += std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
@@ -74,7 +75,6 @@ void truffle_benchmarks(int size, int loops, int M, bool has_match) {
                KBLU "average time per call =" RST " %.3f μs ," KBLU " bandwidth = " RST " %.3f MB/s," KBLU " average bandwidth =" RST " %.3f MB/s \n",
                size ,loops, total_sec, avg_time, max_bw, bandwitdh);
     }
-    delete [] kt1;
 }
 
 
@@ -83,8 +83,8 @@ void rtruffle_benchmarks(int size, int loops, int M, bool has_match) {
     ue2::CharReach chars;
     chars.set('a');
     truffleBuildMasks(chars, (u8 *)&lo, (u8 *)&hi);
-    u8 *kt1 = new u8[size];
-    memset(kt1,'b',size);
+    std::unique_ptr<u8 []> kt1 ( new u8[size] );
+    memset(kt1.get(),'b',size);
     double total_sec = 0.0;            
     u64a transferred_size = 0;
     double bandwitdh = 0.0;
@@ -100,8 +100,8 @@ void rtruffle_benchmarks(int size, int loops, int M, bool has_match) {
             unsigned long act_size = 0;
             auto start = std::chrono::steady_clock::now();
             for(int i = 0; i < loops; i++) {
-                const u8 *res = rtruffleExec(lo, hi, kt1, kt1 + size);
-                act_size += res - kt1;
+                const u8 *res = rtruffleExec(lo, hi, kt1.get(), kt1.get() + size);
+                act_size += res - kt1.get();
             }
             auto end = std::chrono::steady_clock::now();
             double dt = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
@@ -126,7 +126,7 @@ void rtruffle_benchmarks(int size, int loops, int M, bool has_match) {
     } else {
         auto start = std::chrono::steady_clock::now();
         for (int i = 0; i < loops; i++) {
-            rtruffleExec(lo, hi, kt1, kt1 + size);
+            rtruffleExec(lo, hi, kt1.get(), kt1.get() + size);
         }
         auto end = std::chrono::steady_clock::now();
         total_sec += std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
@@ -146,5 +146,4 @@ void rtruffle_benchmarks(int size, int loops, int M, bool has_match) {
                KBLU "average time per call =" RST " %.3f μs ," KBLU " bandwidth = " RST " %.3f MB/s," KBLU " average bandwidth =" RST " %.3f MB/s \n",
                size ,loops, total_sec, avg_time, max_bw, bandwitdh);
     }
-    delete [] kt1;
 }