Skip to content

Commit

Permalink
Optimise shader cache string generation
Browse files Browse the repository at this point in the history
  • Loading branch information
stuarthayhurst committed Jul 4, 2024
1 parent 842ee46 commit c6c0106
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/ammonite/core/fileManager.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <cstddef>
#include <cstdint>
#include <chrono>
#include <filesystem>
#include <string>
Expand All @@ -24,15 +25,30 @@ namespace ammonite {
}

namespace {
//Hash input filenames together to create a unique cache string
//Don't use this for security, you'll lose your job
static std::string generateCacheString(std::string* filePaths,
unsigned int fileCount) {
std::string inputString = std::string("");
uint8_t output[8] = {0};
uint8_t prev = 0;

/*
- XOR the first byte of the hash with each character of each path
- After each character, sequentially XOR every byte of the hash with the result of
the previous XOR
*/
for (unsigned int i = 0; i < fileCount; i++) {
inputString += filePaths[i] + std::string(";");
uint8_t* filePath = (uint8_t*)filePaths[i].c_str();
int pathLength = filePaths[i].length();
for (int i = 0; i < pathLength; i++) {
output[0] ^= filePath[i];
for (int j = 0; j < 8; j++) {
output[j] ^= prev;
prev = output[j];
}
}
}

return std::to_string(std::hash<std::string>{}(inputString));
return std::to_string(*(uint64_t*)output);
}
}

Expand Down

0 comments on commit c6c0106

Please sign in to comment.