Skip to content

Commit

Permalink
TS#38029 Rework functionId_set
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphael-N committed Jul 22, 2024
1 parent bd3289a commit d660612
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions Profiler/utils/functionID_set/functionId_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </summary>
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);

/// <summary>
/// Updates the size of the array and reinserts the element into the new array.
/// </summary>
void adjust_size() {
void increase_size() {
max_elements = current_size;
current_size *= 2;
modulo_mask = current_size - 1;
Expand All @@ -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;
Expand All @@ -81,10 +81,10 @@ class functionId_set
/// </summary>
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 };
}

/// <summary>
Expand Down Expand Up @@ -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;
Expand All @@ -136,31 +136,29 @@ class functionId_set
return set[position];
}



/// <summary>
/// Inserts FunctionID f into the set.
/// </summary>
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;
}
}

// 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;
}
}
Expand Down

0 comments on commit d660612

Please sign in to comment.