|
3 | 3 | import static datadog.trace.bootstrap.instrumentation.api.UTF8BytesString.EMPTY; |
4 | 4 |
|
5 | 5 | import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString; |
| 6 | +import datadog.trace.util.HashingUtils; |
6 | 7 | import java.util.Collections; |
7 | 8 | import java.util.List; |
8 | 9 | import java.util.Objects; |
@@ -49,42 +50,19 @@ public MetricKey( |
49 | 50 | this.httpMethod = httpMethod == null ? null : UTF8BytesString.create(httpMethod); |
50 | 51 | this.httpEndpoint = httpEndpoint == null ? null : UTF8BytesString.create(httpEndpoint); |
51 | 52 |
|
52 | | - // Unrolled polynomial hashcode to avoid varargs allocation |
53 | | - // and eliminate data dependency between iterations as in Arrays.hashCode. |
54 | | - // Coefficient constants are powers of 31, with integer overflow (hence negative numbers). |
55 | | - // See |
56 | | - // https://richardstartin.github.io/posts/collecting-rocks-and-benchmarks |
57 | | - // https://richardstartin.github.io/posts/still-true-in-java-9-handwritten-hash-codes-are-faster |
58 | | - |
59 | | - // Only include httpMethod and httpEndpoint in hash if they are not null |
60 | | - // This ensures backward compatibility when the feature is disabled |
61 | | - |
62 | | - // Note: all the multiplication got constant folded at compile time (see javap -verbose ...) |
63 | | - int tmpHash = |
64 | | - (int) (31L * 31 * 31 * 31 * 31 * 31 * 31 * 31) * Boolean.hashCode(this.isTraceRoot) // 8 |
65 | | - + (int) (31L * 31 * 31 * 31 * 31 * 31 * 31) * this.spanKind.hashCode() // 7 |
66 | | - + 31 * 31 * 31 * 31 * 31 * 31 * this.peerTags.hashCode() // 6 |
67 | | - + 31 * 31 * 31 * 31 * 31 * this.resource.hashCode() // 5 |
68 | | - + 31 * 31 * 31 * 31 * this.service.hashCode() // 4 |
69 | | - + 31 * 31 * 31 * this.operationName.hashCode() // 3 |
70 | | - + 31 * 31 * this.type.hashCode() // 2 |
71 | | - + 31 * this.httpStatusCode // 1 |
72 | | - + (this.synthetics ? 1 : 0); // 0 |
73 | | - // optional fields |
74 | | - if (this.serviceSource != null) { |
75 | | - tmpHash += |
76 | | - (int) (31L * 31 * 31 * 31 * 31 * 31 * 31 * 31 * 31) * this.serviceSource.hashCode(); // 9 |
77 | | - } |
78 | | - if (this.httpEndpoint != null) { |
79 | | - tmpHash += |
80 | | - (int) (31L * 31 * 31 * 31 * 31 * 31 * 31 * 31 * 31 * 31) |
81 | | - * this.httpEndpoint.hashCode(); // 10 |
82 | | - } |
83 | | - if (this.httpMethod != null) { |
84 | | - tmpHash += |
85 | | - (int) (31L * 31 * 31 * 31 * 31 * 31 * 31 * 31 * 31 * 31 * 31) |
86 | | - * this.httpMethod.hashCode(); // 11 |
87 | | - } |
| 53 | + int tmpHash = 0; |
| 54 | + tmpHash = HashingUtils.addToHash(tmpHash, this.isTraceRoot); |
| 55 | + tmpHash = HashingUtils.addToHash(tmpHash, this.spanKind); |
| 56 | + tmpHash = HashingUtils.addToHash(tmpHash, this.peerTags); |
| 57 | + tmpHash = HashingUtils.addToHash(tmpHash, this.resource); |
| 58 | + tmpHash = HashingUtils.addToHash(tmpHash, this.service); |
| 59 | + tmpHash = HashingUtils.addToHash(tmpHash, this.operationName); |
| 60 | + tmpHash = HashingUtils.addToHash(tmpHash, this.type); |
| 61 | + tmpHash = HashingUtils.addToHash(tmpHash, this.httpStatusCode); |
| 62 | + tmpHash = HashingUtils.addToHash(tmpHash, this.synthetics); |
| 63 | + tmpHash = HashingUtils.addToHash(tmpHash, this.serviceSource); |
| 64 | + tmpHash = HashingUtils.addToHash(tmpHash, this.httpEndpoint); |
| 65 | + tmpHash = HashingUtils.addToHash(tmpHash, this.httpMethod); |
88 | 66 | this.hash = tmpHash; |
89 | 67 | } |
90 | 68 |
|
|
0 commit comments