From d6606124468b7a512c9ef6a476578a4aac179ebd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20N=C3=B6mmer?= Date: Mon, 22 Jul 2024 15:46:28 +0200 Subject: [PATCH] TS#38029 Rework functionId_set --- .../utils/functionID_set/functionId_set.h | 46 +++++++++---------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/Profiler/utils/functionID_set/functionId_set.h b/Profiler/utils/functionID_set/functionId_set.h index 3b6cf6a8..9a7973ae 100644 --- a/Profiler/utils/functionID_set/functionId_set.h +++ b/Profiler/utils/functionID_set/functionId_set.h @@ -6,33 +6,31 @@ /// Set that can only contain functionIDs, which are just unsigned ints. /// This is based on an array and is a lot faster than the default set implementation of the standard library for this use case. /// -class functionId_set +class functionId_set final { private: - const unsigned int default_size = 524'288; + const static unsigned int DEFAULT_SIZE = 131'072; + const static unsigned int NUM_XOR_VALUES = 10U; + static const FunctionID rotation_mask = (-1) & (CHAR_BIT * sizeof(FunctionID) - 1); - unsigned int current_size = default_size; + unsigned int current_size = DEFAULT_SIZE; unsigned int num_elements = 0; - unsigned int max_elements = default_size / 2; + unsigned int max_elements = DEFAULT_SIZE / 2; unsigned int modulo_mask = current_size - 1; - FunctionID* set = new FunctionID[default_size]{ 0 }; - - bool resizing = false; - + FunctionID* set = new FunctionID[DEFAULT_SIZE]{ 0 }; // Predetermined values for xor operation to place in different spots #ifdef _WIN64 - FunctionID xor_values[10] = { 14653048717414601650, 10827059576848633699, 18275351206681430557, 15388157215360276699, 5625538261940547542, 17184451615065543950, 12483508915876842786, 7009288139810362683, 8675975670288616007, 11886397353506492918 }; + FunctionID xor_values[NUM_XOR_VALUES] = { 14653048717414601650ULL, 10827059576848633699ULL, 18275351206681430557ULL, 15388157215360276699ULL, 5625538261940547542ULL, 17184451615065543950ULL, 12483508915876842786ULL, 7009288139810362683ULL, 8675975670288616007ULL, 11886397353506492918ULL }; #else - FunctionID xor_values[10] = { 987205454, 278240680, 4130902882, 445831414, 3235577889, 1789497761, 205336377, 2382455698, 1977849072, 966072234 }; + FunctionID xor_values[NUM_XOR_VALUES] = { 987205454U, 278240680U, 4130902882U, 445831414U, 3235577889U, 1789497761U, 205336377U, 2382455698U, 1977849072U, 966072234U }; #endif - const unsigned int num_xor_values = sizeof(xor_values) / sizeof(FunctionID); /// /// Updates the size of the array and reinserts the element into the new array. /// - void adjust_size() { + void increase_size() { max_elements = current_size; current_size *= 2; modulo_mask = current_size - 1; @@ -54,14 +52,16 @@ class functionId_set return (f >> 1) | (f << rotation_mask); } - inline bool tryInsert(unsigned int position, const FunctionID f) { + inline bool try_insert(unsigned int position, const FunctionID f) { if (set[position] == f) { return true; } if (set[position] == 0) { num_elements++; if (num_elements > max_elements) { - adjust_size(); + increase_size(); + insert(f); + return true; } set[position] = f; @@ -81,10 +81,10 @@ class functionId_set /// void clear() { delete set; - current_size = default_size; - max_elements = default_size / 2; + current_size = DEFAULT_SIZE; + max_elements = DEFAULT_SIZE / 2; num_elements = 0; - set = new FunctionID[default_size]{ 0 }; + set = new FunctionID[DEFAULT_SIZE]{ 0 }; } /// @@ -115,7 +115,7 @@ class functionId_set } // Then rotate bits and xor to try and find a new position - for (unsigned int i = 0; i < num_xor_values; i++) { + for (unsigned int i = 0; i < NUM_XOR_VALUES; i++) { position = (rotr(f) ^ xor_values[i]) & modulo_mask; if (set[position] == 0) { return false; @@ -136,23 +136,21 @@ class functionId_set return set[position]; } - - /// /// Inserts FunctionID f into the set. /// void insert(FunctionID f) { // Try insertion at the number modulo the size of the set first unsigned int position = f & modulo_mask; - if (tryInsert(position, f)) + if (try_insert(position, f)) { return; } // Then rotate bits and xor to try and find a new position - for (unsigned int i = 0; i < num_xor_values; i++) { + for (unsigned int i = 0; i < NUM_XOR_VALUES; i++) { position = (rotr(f) ^ xor_values[i]) & modulo_mask; - if (tryInsert(position, f)) + if (try_insert(position, f)) { return; } @@ -160,7 +158,7 @@ class functionId_set // If no position was found, just go to the next position with +1 position = (f + 1) & modulo_mask; - while (!tryInsert(position, f)) { + while (!try_insert(position, f)) { position = (position + 1) & modulo_mask; } }