Cool VL Viewer forum

Why use MD5 when SHA256 is at least twice as fast?
Page 1 of 1

Author:  kathrine [ 2022-04-06 00:00:37 ]
Post subject:  Why use MD5 when SHA256 is at least twice as fast?

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

Author:  Henri Beauchamp [ 2022-04-06 00:13:39 ]
Post subject:  Re: Why use MD5 when SHA256 is at least twice as fast?

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.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group