Cool VL Viewer forum

View unanswered posts | View active topics It is currently 2022-05-18 12:01:47

Reply to topic  [ 2 posts ] 
Why use MD5 when SHA256 is at least twice as fast? 
Author Message

Joined: 2011-10-07 10:39:20
Posts: 148
Reply with quote
I noticed this new snippet in the 1.29.0 patches:

+void LLMeshSkinInfo::updateHash()
+   // When the mesh UUID is known (which is always the case for LLMeshSkinInfo
+   // instances created by the mesh repository), use its 64 bits digest; there
+   // is no need to hash anything else, since a skin with the same UUID always
+   // got the same joints, inverse bind matrix, etc. HB
+   if (mMeshID.notNull())
+   {
+      mHash = mMeshID.getDigest64();
+      return;
+   }
+   LLMD5 hash;
+   // Hash joint names (like in LL's performance viewer). HB
+   for (U32 i = 0, count = mJointNames.size(); i < count; ++i)
+   {
+      hash.update(mJointNames[i]);
+   }
+   // Hash joint keys (LL's performance viewer uses joint numbers instead). HB
+   hash.update((U8*), sizeof(U32) * mJointKeys.size());
+   // Hash inverse bind matrix (like in LL's performance viewer).
+   // Note: there should be no padding/aligment issue between elements in the
+   // mInvBindMatrix LLMatrix4s vector, given that an LLMatrix4 is represented
+   // by 16 32 bits values (64 bytes). So we can save a loop here and hash the
+   // whole vector as one contiguous block of data. HB
+   hash.update((U8*),
+            sizeof(LLMatrix4) * mInvBindMatrix.size());
+   hash.finalize();
+   U64 digest[2];   // 128 bits (16 bytes) buffer for LLMD5::raw_digest()
+   hash.raw_digest((U8*)digest);
+   // Further digest to 64 bits only. LL's original code ignores digest[1],
+   // which gives a less good quality digest than XORing the two 64 bits words
+   // like I what am doing here. HB
+   mHash = digest[0] ^ digest[1];

If this is performance critical, i would suggest to kick out MD5 and use one of the hardware accelerated hashes in openssl (or a non cryptographic hash thats even faster).

e.g. SHA256 tends to be at least twice as fast on modern hardware supporting SHA-NI instructions (see ... 2-encoding )


openssl speed -evp md5
openssl speed -evp sha256

2022-04-06 00:00:37

Joined: 2009-03-17 18:42:51
Posts: 4906
Reply with quote
LL's code is indeed super-slow, but it is almost never executed because I added the mesh skin UUID digest (XORing of the two 64 bits long words making up an UUID): all the meshes downloaded via the mesh repository (i.e. from the grid) do have an UUID, and so only my super-fast "mHash = mMeshID.getDigest64();" line is ever executed.
The only exception would be for the mesh upload floater (i.e. meshes you load from a local file and that do not have an UUID), which is not time critical at all.

2022-04-06 00:13:39
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.