Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions framework/src/main/java/org/tron/core/net/peer/Item.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
package org.tron.core.net.peer;

import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import java.security.SecureRandom;
import lombok.AccessLevel;
import lombok.Getter;
import org.tron.common.utils.Sha256Hash;
import org.tron.protos.Protocol.Inventory.InventoryType;

@Getter
public class Item {

private static final HashFunction ID_HASH = newIdHash();

@Getter
private Sha256Hash hash;
private final Sha256Hash hash;
@Getter
private InventoryType type;
private final InventoryType type;
@Getter
private long time;
private final long time;
@Getter(AccessLevel.NONE)
private final int idHash;


public Item(Sha256Hash hash, InventoryType type) {
this.hash = hash;
this.type = type;
this.time = System.currentTimeMillis();
this.idHash = ID_HASH.hashBytes(hash.getBytes()).asInt();
}

private static HashFunction newIdHash() {
SecureRandom random = new SecureRandom();
return Hashing.sipHash24(random.nextLong(), random.nextLong());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you share the benchmark code and clarify whether the timings include Item construction? SipHash is computed in the constructor, so cache operations on prebuilt items would miss that added cost.

Please also compare construction plus cache lookup/insertion, including allocation and GC overhead, using the same workload and JVM warmup before and after the change. Results on both JDK 8/x86_64 and JDK 17/aarch64 would help assess the performance impact.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The timings in the description include construction: new Item(...) is inside the timed loop, and only id generation is outside. They were single cold runs, though, so I reran a warmed before/after comparison (code below).

Base 37ea60a8bc vs this PR, each in its own JVM; 5 × 1 s warmup and 5 × 1 s measurement; JDK 17.0.18 / aarch64, -XX:+UseZGC -Xms2g -Xmx2g. Allocation is the bytes allocated by the measuring thread per item.

Per item Before This PR
new Item(...) 22.6–23.1 ns, 40 B 46.4–48.2 ns, 136 B
Construction + cache lookups/insertions (INVENTORY path) 671–1494 ns, 447 B 774–1272 ns, 543 B¹

¹ 695 B in one of the five JVMs.

Construction costs about 24 ns and 96 B more per item, because Guava's hashBytes allocates a hasher, a buffer and the HashCode. With the cache operations included, allocation is 21% higher and the time difference is within run-to-run variation. On that path ZGC ran 8 times per 5 s measurement before and 4–8 times with this PR.

I don't have a JDK 8 / x86_64 machine at hand, so there are no numbers for it yet; the harness runs there unchanged.

}

@Override
Expand All @@ -36,7 +50,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return hash.hashCode();
return idHash;
}

@Override
Expand Down
54 changes: 54 additions & 0 deletions framework/src/test/java/org/tron/core/net/peer/ItemTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.tron.core.net.peer;

import java.util.HashSet;
import java.util.Set;
import org.junit.Assert;
import org.junit.Test;
import org.tron.common.utils.Sha256Hash;
import org.tron.core.capsule.BlockCapsule.BlockId;
import org.tron.protos.Protocol.Inventory.InventoryType;

public class ItemTest {

private static Sha256Hash idEndingInZeros(int i) {
byte[] bytes = new byte[Sha256Hash.LENGTH];
bytes[0] = (byte) (i >>> 24);
bytes[1] = (byte) (i >>> 16);
bytes[2] = (byte) (i >>> 8);
bytes[3] = (byte) i;
return Sha256Hash.wrap(bytes);
}

@Test
public void testEqualItemsShareHashCode() {
Sha256Hash id = idEndingInZeros(7);
Item item = new Item(id, InventoryType.TRX);
Item copy = new Item(Sha256Hash.wrap(id.getBytes().clone()), InventoryType.TRX);
Assert.assertEquals(item, copy);
Assert.assertEquals(item.hashCode(), copy.hashCode());
Assert.assertEquals(item.hashCode(), item.hashCode());
}

@Test
public void testBlockIdAndPlainHashWithSameBytesShareHashCode() {
BlockId blockId = new BlockId(Sha256Hash.of(new byte[] {1}), 100L);
Item fromBlockId = new Item(blockId, InventoryType.BLOCK);
Item fromPlainHash = new Item(Sha256Hash.wrap(blockId.getBytes()), InventoryType.BLOCK);
Assert.assertEquals(fromBlockId, fromPlainHash);
Assert.assertEquals(fromPlainHash, fromBlockId);
Assert.assertEquals(fromBlockId.hashCode(), fromPlainHash.hashCode());
}

@Test
public void testIdsWithEqualLastFourBytesGetDistinctHashCodes() {
int count = 10_000;
Set<Integer> hashCodes = new HashSet<>();
for (int i = 0; i < count; i++) {
Sha256Hash id = idEndingInZeros(i);
Assert.assertEquals(0, id.hashCode());
hashCodes.add(new Item(id, InventoryType.TRX).hashCode());
}
// A keyed 32-bit hash gives about 0.01 expected collisions among 10,000 ids.
Assert.assertTrue("distinct hash codes: " + hashCodes.size(), hashCodes.size() >= count - 10);
}
}
Loading