Skip to content
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

Improve bad hash functions #91552

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions core/math/delaunay_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ class Delaunay3D {

struct TriangleHasher {
_FORCE_INLINE_ static uint32_t hash(const Triangle &p_triangle) {
uint32_t h = hash_djb2_one_32(p_triangle.triangle[0]);
h = hash_djb2_one_32(p_triangle.triangle[1], h);
return hash_fmix32(hash_djb2_one_32(p_triangle.triangle[2], h));
uint32_t h = hash_murmur3_one_32(p_triangle.triangle[0]);
h = hash_murmur3_one_32(p_triangle.triangle[1], h);
return hash_fmix32(hash_murmur3_one_32(p_triangle.triangle[2], h));
}
};

Expand Down
18 changes: 12 additions & 6 deletions core/templates/hashfuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,16 +343,22 @@ struct HashMapHasherDefault {
h = hash_murmur3_one_32(p_vec.w, h);
return hash_fmix32(h);
}
static _FORCE_INLINE_ uint32_t hash(const Vector2 &p_vec) {
uint32_t h = hash_murmur3_one_real(p_vec.x);
static _FORCE_INLINE_ uint32_t hash_seed(const Vector2 &p_vec, uint32_t p_seed = HASH_MURMUR3_SEED) {
uint32_t h = hash_murmur3_one_real(p_vec.x, p_seed);
h = hash_murmur3_one_real(p_vec.y, h);
return hash_fmix32(h);
return h;
}
static _FORCE_INLINE_ uint32_t hash(const Vector3 &p_vec) {
uint32_t h = hash_murmur3_one_real(p_vec.x);
static _FORCE_INLINE_ uint32_t hash(const Vector2 &p_vec) {
return hash_fmix32(hash_seed(p_vec));
}
static _FORCE_INLINE_ uint32_t hash_seed(const Vector3 &p_vec, uint32_t p_seed = HASH_MURMUR3_SEED) {
uint32_t h = hash_murmur3_one_real(p_vec.x, p_seed);
h = hash_murmur3_one_real(p_vec.y, h);
h = hash_murmur3_one_real(p_vec.z, h);
return hash_fmix32(h);
return h;
}
static _FORCE_INLINE_ uint32_t hash(const Vector3 &p_vec) {
return hash_fmix32(hash_seed(p_vec));
}
static _FORCE_INLINE_ uint32_t hash(const Vector4 &p_vec) {
uint32_t h = hash_murmur3_one_real(p_vec.x);
Expand Down
4 changes: 3 additions & 1 deletion editor/plugins/gizmos/navigation_region_3d_gizmo_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ class NavigationRegion3DGizmoPlugin : public EditorNode3DGizmoPlugin {
Vector3 to;

static uint32_t hash(const _EdgeKey &p_key) {
return HashMapHasherDefault::hash(p_key.from) ^ HashMapHasherDefault::hash(p_key.to);
uint32_t hash = HashMapHasherDefault::hash_seed(p_key.from);
hash = HashMapHasherDefault::hash_seed(p_key.to, hash);
return hash_fmix32(hash);
Nazarwadim marked this conversation as resolved.
Show resolved Hide resolved
}

bool operator==(const _EdgeKey &p_with) const {
Expand Down
4 changes: 3 additions & 1 deletion modules/navigation/nav_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ struct EdgeKey {
PointKey b;

static uint32_t hash(const EdgeKey &p_val) {
return hash_one_uint64(p_val.a.key) ^ hash_one_uint64(p_val.b.key);
uint32_t hash = hash_murmur3_one_64(p_val.a.key);
hash = hash_murmur3_one_64(p_val.b.key, hash);
return hash_fmix32(hash);
}

bool operator==(const EdgeKey &p_key) const {
Expand Down
5 changes: 3 additions & 2 deletions scene/resources/3d/concave_polygon_shape_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ class ConcavePolygonShape3D : public Shape3D {
Vector3 a;
Vector3 b;
static uint32_t hash(const DrawEdge &p_edge) {
uint32_t h = hash_murmur3_one_32(HashMapHasherDefault::hash(p_edge.a));
return hash_murmur3_one_32(HashMapHasherDefault::hash(p_edge.b), h);
uint32_t h = HashMapHasherDefault::hash_seed(p_edge.a);
h = HashMapHasherDefault::hash_seed(p_edge.b, h);
return hash_fmix32(h);
}
bool operator==(const DrawEdge &p_edge) const {
return (a == p_edge.a && b == p_edge.b);
Expand Down
2 changes: 1 addition & 1 deletion scene/resources/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ class BaseMaterial3D : public Material {
}

static uint32_t hash(const MaterialKey &p_key) {
return hash_djb2_buffer((const uint8_t *)&p_key, sizeof(MaterialKey));
return hash_murmur3_buffer((const uint8_t *)&p_key, sizeof(MaterialKey));
}
bool operator==(const MaterialKey &p_key) const {
return memcmp(this, &p_key, sizeof(MaterialKey)) == 0;
Expand Down
37 changes: 21 additions & 16 deletions scene/resources/surface_tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,24 @@ bool SurfaceTool::Vertex::operator==(const Vertex &p_vertex) const {
}

uint32_t SurfaceTool::VertexHasher::hash(const Vertex &p_vtx) {
uint32_t h = hash_djb2_buffer((const uint8_t *)&p_vtx.vertex, sizeof(real_t) * 3);
h = hash_djb2_buffer((const uint8_t *)&p_vtx.normal, sizeof(real_t) * 3, h);
h = hash_djb2_buffer((const uint8_t *)&p_vtx.binormal, sizeof(real_t) * 3, h);
h = hash_djb2_buffer((const uint8_t *)&p_vtx.tangent, sizeof(real_t) * 3, h);
h = hash_djb2_buffer((const uint8_t *)&p_vtx.uv, sizeof(real_t) * 2, h);
h = hash_djb2_buffer((const uint8_t *)&p_vtx.uv2, sizeof(real_t) * 2, h);
h = hash_djb2_buffer((const uint8_t *)&p_vtx.color, sizeof(real_t) * 4, h);
h = hash_djb2_buffer((const uint8_t *)p_vtx.bones.ptr(), p_vtx.bones.size() * sizeof(int), h);
h = hash_djb2_buffer((const uint8_t *)p_vtx.weights.ptr(), p_vtx.weights.size() * sizeof(float), h);
h = hash_djb2_buffer((const uint8_t *)&p_vtx.custom[0], sizeof(Color) * RS::ARRAY_CUSTOM_COUNT, h);
h = hash_murmur3_one_32(p_vtx.smooth_group, h);
h = hash_fmix32(h);
uint32_t h = hash_murmur3_one_32(p_vtx.smooth_group);

h = hash_murmur3_one_float(p_vtx.color.r, h);
h = hash_murmur3_one_float(p_vtx.color.g, h);
h = hash_murmur3_one_float(p_vtx.color.b, h);
h = hash_murmur3_one_float(p_vtx.color.a, h);

h = HashMapHasherDefault::hash_seed(p_vtx.normal, h);
h = HashMapHasherDefault::hash_seed(p_vtx.binormal, h);
h = HashMapHasherDefault::hash_seed(p_vtx.tangent, h);
h = HashMapHasherDefault::hash_seed(p_vtx.uv, h);
h = HashMapHasherDefault::hash_seed(p_vtx.uv2, h);
h = HashMapHasherDefault::hash_seed(p_vtx.vertex, h);

h = hash_murmur3_buffer((const uint8_t *)p_vtx.bones.ptr(), p_vtx.bones.size() * sizeof(int), h);
h = hash_murmur3_buffer((const uint8_t *)p_vtx.weights.ptr(), p_vtx.weights.size() * sizeof(float), h);
h = hash_murmur3_buffer((const uint8_t *)&p_vtx.custom[0], sizeof(Color) * RS::ARRAY_CUSTOM_COUNT, h);

return h;
}

Expand All @@ -163,10 +169,9 @@ bool SurfaceTool::SmoothGroupVertex::operator==(const SmoothGroupVertex &p_verte
}

uint32_t SurfaceTool::SmoothGroupVertexHasher::hash(const SmoothGroupVertex &p_vtx) {
uint32_t h = hash_djb2_buffer((const uint8_t *)&p_vtx.vertex, sizeof(real_t) * 3);
h = hash_murmur3_one_32(p_vtx.smooth_group, h);
h = hash_fmix32(h);
return h;
uint32_t h = hash_murmur3_one_32(p_vtx.smooth_group);
h = HashMapHasherDefault::hash_seed(p_vtx.vertex, h);
return hash_fmix32(h);
}

uint32_t SurfaceTool::TriangleHasher::hash(const int *p_triangle) {
Expand Down