-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Use Set hash in Distinct (2nd attempt) #7175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
6f8149a
tyler's PoC for attribute hashing
dashpole f36871d
lint
dashpole 8da27b2
iterate on runes, not bytes
dashpole 1ca3971
simplify benchmark loops
dashpole 6d02670
declare benchmark var globally
dashpole 34995bf
add fuzz test
dashpole 8a13753
remove changelog entry
dashpole 1ca4a33
lint
dashpole fffec87
fix set equality
dashpole 7ac0013
document that Distinct now has theoretically possible collisions
dashpole 04702b7
move changelog to unreleased
dashpole 103045b
Update attribute/hash_test.go
dashpole 3add646
Apply suggestions from code review
dashpole 5acd267
fixes after rebase
dashpole 4ed4e0d
lint
dashpole 41b0468
Merge branch 'main' into hash_distinct
dashpole File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package attribute // import "go.opentelemetry.io/otel/attribute" | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
|
||
"go.opentelemetry.io/otel/attribute/internal/fnv" | ||
) | ||
|
||
// Type identifiers. These identifiers are hashed before the value of the | ||
// corresponding type. This is done to distinguish values that are hashed with | ||
// the same value representation (e.g. `int64(1)` and `true`, []int64{0} and | ||
// int64(0)). | ||
// | ||
// These are all 8 byte length strings converted to a uint64 representation. A | ||
// uint64 is used instead of the string directly as an optimization, it avoids | ||
// the for loop in [fnv] which adds minor overhead. | ||
const ( | ||
boolID uint64 = 7953749933313450591 // "_boolean" (little endian) | ||
int64ID uint64 = 7592915492740740150 // "64_bit_i" (little endian) | ||
float64ID uint64 = 7376742710626956342 // "64_bit_f" (little endian) | ||
stringID uint64 = 6874584755375207263 // "_string_" (little endian) | ||
boolSliceID uint64 = 6875993255270243167 // "_[]bool_" (little endian) | ||
int64SliceID uint64 = 3762322556277578591 // "_[]int64" (little endian) | ||
float64SliceID uint64 = 7308324551835016539 // "[]double" (little endian) | ||
stringSliceID uint64 = 7453010373645655387 // "[]string" (little endian) | ||
) | ||
|
||
// hashKVs returns a new FNV-1a hash of kvs. | ||
func hashKVs(kvs []KeyValue) fnv.Hash { | ||
h := fnv.New() | ||
for _, kv := range kvs { | ||
h = hashKV(h, kv) | ||
} | ||
return h | ||
} | ||
|
||
// hashKV returns the FNV-1a hash of kv with h as the base. | ||
func hashKV(h fnv.Hash, kv KeyValue) fnv.Hash { | ||
h = h.String(string(kv.Key)) | ||
|
||
switch kv.Value.Type() { | ||
case BOOL: | ||
h = h.Uint64(boolID) | ||
h = h.Uint64(kv.Value.numeric) | ||
case INT64: | ||
h = h.Uint64(int64ID) | ||
h = h.Uint64(kv.Value.numeric) | ||
case FLOAT64: | ||
h = h.Uint64(float64ID) | ||
// Assumes numeric stored with math.Float64bits. | ||
h = h.Uint64(kv.Value.numeric) | ||
case STRING: | ||
h = h.Uint64(stringID) | ||
h = h.String(kv.Value.stringly) | ||
case BOOLSLICE: | ||
h = h.Uint64(boolSliceID) | ||
rv := reflect.ValueOf(kv.Value.slice) | ||
for i := 0; i < rv.Len(); i++ { | ||
h = h.Bool(rv.Index(i).Bool()) | ||
} | ||
case INT64SLICE: | ||
h = h.Uint64(int64SliceID) | ||
rv := reflect.ValueOf(kv.Value.slice) | ||
for i := 0; i < rv.Len(); i++ { | ||
h = h.Int64(rv.Index(i).Int()) | ||
} | ||
case FLOAT64SLICE: | ||
h = h.Uint64(float64SliceID) | ||
rv := reflect.ValueOf(kv.Value.slice) | ||
for i := 0; i < rv.Len(); i++ { | ||
h = h.Float64(rv.Index(i).Float()) | ||
} | ||
case STRINGSLICE: | ||
h = h.Uint64(stringSliceID) | ||
rv := reflect.ValueOf(kv.Value.slice) | ||
for i := 0; i < rv.Len(); i++ { | ||
h = h.String(rv.Index(i).String()) | ||
} | ||
case INVALID: | ||
default: | ||
// Logging is an alternative, but using the internal logger here | ||
// causes an import cycle so it is not done. | ||
v := kv.Value.AsInterface() | ||
msg := fmt.Sprintf("unknown value type: %[1]v (%[1]T)", v) | ||
panic(msg) | ||
} | ||
return h | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.