-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: remove cached hash code for ItemId (#1696)
The id is generated from a hash function, typically SHA1 so we can just use a part of that for the hash code of the id object. Since data is sometimes partitioned and routed based on the most and least significant bits, we pick one of the middle bytes to reduce the liklihood of collisions. Benchmarks show it is about 25% slower to access the hash code this way, but it does provide some memory savings and compute savings when creating an id instance since the murmur hash doesn't need to be computed.
- Loading branch information
1 parent
8c8edfc
commit 7474caa
Showing
3 changed files
with
110 additions
and
55 deletions.
There are no files selected for viewing
This file contains 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 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
60 changes: 60 additions & 0 deletions
60
atlas-jmh/src/main/scala/com/netflix/atlas/core/model/ItemIdHashCode.scala
This file contains 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,60 @@ | ||
/* | ||
* Copyright 2014-2024 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.atlas.core.model | ||
|
||
import org.openjdk.jmh.annotations.Benchmark | ||
import org.openjdk.jmh.annotations.Scope | ||
import org.openjdk.jmh.annotations.State | ||
import org.openjdk.jmh.infra.Blackhole | ||
|
||
import scala.util.Random | ||
|
||
/** | ||
* ``` | ||
* > jmh:run -prof gc -prof stack -wi 5 -i 10 -f1 -t1 .*ItemIdHashCode.* | ||
* ``` | ||
* | ||
* ItemId.hashCode: | ||
* | ||
* ``` | ||
* Benchmark Mode Cnt Score Error Units | ||
* before thrpt 10 1547433250.866 ± 52995197.574 ops/s | ||
* after thrpt 10 1162791300.824 ± 36880550.752 ops/s | ||
* ``` | ||
* | ||
* | ||
* ItemId.intValue: | ||
* | ||
* ``` | ||
* before thrpt 10 292282926.019 ± 2643333.767 ops/s | ||
* after thrpt 10 1101391907.134 ± 14583585.092 ops/s | ||
* ``` | ||
*/ | ||
@State(Scope.Thread) | ||
class ItemIdHashCode { | ||
|
||
private val id = ItemId(Random.nextBytes(20)) | ||
|
||
@Benchmark | ||
def intValue(bh: Blackhole): Unit = { | ||
bh.consume(id.intValue) | ||
} | ||
|
||
@Benchmark | ||
def hashCode(bh: Blackhole): Unit = { | ||
bh.consume(id.hashCode()) | ||
} | ||
} |