Skip to content

Commit

Permalink
Do not hash stack past length
Browse files Browse the repository at this point in the history
As an optimization, we don't zero the scratch stack every single time
and simply set its length to zero.

This commit ensures that we don't try to read these garbage values while
hashing because then the hash for two stacktraces that are equivalent
will be different reducing the space in the hashmap where they are
inserted.
  • Loading branch information
javierhonduco committed Mar 22, 2024
1 parent 82f8496 commit fa98cc7
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/bpf/profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,14 @@ unsigned long long hash_stack(native_stack_t *stack) {
unsigned long long hash = seed ^ (stack->len * m);

for (int i = 0; i < MAX_STACK_DEPTH; i++) {
unsigned long long k = stack->addresses[i];
// The stack is not zeroed when we initialise it, we simply
// set the length to zero. This is a workaround to produce
// the same hash for two stacks that might have garbage values
// after their length.
unsigned long long k = 0;
if (i < stack->len) {
k = stack->addresses[i];
}

k *= m;
k ^= k >> r;
Expand Down

0 comments on commit fa98cc7

Please sign in to comment.