From cb4158902a680c58c5834da8577cd95ff753fd7c Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 17 Nov 2021 14:45:15 +0200 Subject: [PATCH] Update usage of hash functions Calculating hashes directly from the binary values is faster than first creating a string. --- source/core/reflectdata.cpp | 18 ++++++++++++------ source/core/tag.cpp | 4 ++-- source/glsl/finalize.cpp | 2 +- source/glsl/generate.cpp | 2 +- source/materials/material.cpp | 9 ++++++--- 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/source/core/reflectdata.cpp b/source/core/reflectdata.cpp index db679873..8ecb9abe 100644 --- a/source/core/reflectdata.cpp +++ b/source/core/reflectdata.cpp @@ -10,10 +10,12 @@ namespace GL { void ReflectData::update_layout_hash() { - string layout_descriptor; + layout_hash = hash<32>(uniform_blocks.size()); for(const UniformBlockInfo &b: uniform_blocks) - layout_descriptor += format("%d:%x\n", b.bind_point, b.layout_hash); - layout_hash = hash32(layout_descriptor); + { + layout_hash = hash_update<32>(layout_hash, b.bind_point); + layout_hash = hash_update<32>(layout_hash, b.layout_hash); + } } @@ -24,10 +26,14 @@ void ReflectData::UniformBlockInfo::sort_uniforms() void ReflectData::UniformBlockInfo::update_layout_hash() { - string layout_descriptor; + layout_hash = hash<32>(uniforms.size()); for(const UniformInfo *u: uniforms) - layout_descriptor += format("%d:%s:%x:%d\n", u->location, u->name, u->type, u->array_size); - layout_hash = hash32(layout_descriptor); + { + layout_hash = hash_update<32>(layout_hash, u->location); + layout_hash = hash_update<32>(layout_hash, u->name); + layout_hash = hash_update<32>(layout_hash, u->type); + layout_hash = hash_update<32>(layout_hash, u->array_size); + } } } // namespace GL diff --git a/source/core/tag.cpp b/source/core/tag.cpp index 56236c19..84eb074a 100644 --- a/source/core/tag.cpp +++ b/source/core/tag.cpp @@ -22,7 +22,7 @@ namespace Msp { namespace GL { Tag::Tag(const char *s): - id((s && *s) ? hash32(s, strlen(s)) : 0) + id((s && *s) ? hash<32>(s, strlen(s)) : 0) { #ifdef DEBUG if(s) @@ -31,7 +31,7 @@ Tag::Tag(const char *s): } Tag::Tag(const string &s): - id(s.empty() ? 0 : hash32(s)) + id(s.empty() ? 0 : hash<32>(s)) { #ifdef DEBUG if(!s.empty()) diff --git a/source/glsl/finalize.cpp b/source/glsl/finalize.cpp index 60312be8..52cf9580 100644 --- a/source/glsl/finalize.cpp +++ b/source/glsl/finalize.cpp @@ -156,7 +156,7 @@ void LocationAllocator::bind_uniform(RefPtr &layout, const string &name, { set &used = used_bindings[desc_set]; - unsigned bind_point = fold32(hash64(name))%range; + unsigned bind_point = hash_fold<32>(hash<64>(name))%range; while(used.count(bind_point)) bind_point = (bind_point+1)%range; diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index c82d1c6c..85c3009b 100644 --- a/source/glsl/generate.cpp +++ b/source/glsl/generate.cpp @@ -22,7 +22,7 @@ void ConstantIdAssigner::apply(Module &module, const Features &features) id = j->second; else { - id = hash32(v->name)%features.constant_id_range; + id = hash<32>(v->name)%features.constant_id_range; while(used_ids.count(id)) id = (id+1)%features.constant_id_range; } diff --git a/source/materials/material.cpp b/source/materials/material.cpp index 6d9c0571..a34c0457 100644 --- a/source/materials/material.cpp +++ b/source/materials/material.cpp @@ -20,12 +20,15 @@ const Program *Material::create_compatible_shader(const map &extra_ for(const auto &kvp: extra_spec) spec_values[kvp.first] = kvp.second; - string info = module_name; + uint64_t info_hash = hash<64>(module_name); for(const auto &kvp: spec_values) - info += format(",%s:%d", kvp.first, kvp.second); + { + info_hash = hash_update<64>(info_hash, kvp.first); + info_hash = hash_update<64>(info_hash, kvp.second); + } Resources &res = Resources::get_global(); - string name = format("_material_%016x.shader", hash64(info)); + string name = format("_material_%016x.shader", info_hash); Program *shprog = res.find(name); if(shprog) return shprog; -- 2.45.2