refactor: use thread_local in OldChunkStorage, FireworksRecipe, Level, Entity

This commit is contained in:
Tropical
2026-03-25 14:13:00 -05:00
parent 03f62dcb9e
commit 625ce97385
9 changed files with 44 additions and 150 deletions

View File

@@ -41,38 +41,6 @@
#include "../../Minecraft.Client/MinecraftServer.h"
#include <cstdint>
namespace {
#if defined(_WIN32)
inline void* LevelTlsGetValue(Level::TlsKey key) { return TlsGetValue(key); }
inline void LevelTlsSetValue(Level::TlsKey key, void* value) {
TlsSetValue(key, value);
}
#else
pthread_key_t CreateLevelTlsKey() {
pthread_key_t key;
pthread_key_create(&key, NULL);
return key;
}
inline void* LevelTlsGetValue(pthread_key_t key) {
return pthread_getspecific(key);
}
inline void LevelTlsSetValue(pthread_key_t key, void* value) {
pthread_setspecific(key, value);
}
#endif
} // namespace
#if defined(_WIN32)
Level::TlsKey Level::tlsIdx = TlsAlloc();
Level::TlsKey Level::tlsIdxLightCache = TlsAlloc();
#else
Level::TlsKey Level::tlsIdx = CreateLevelTlsKey();
Level::TlsKey Level::tlsIdxLightCache = CreateLevelTlsKey();
#endif
// 4J : WESTY : Added for time played stats.
#include "../Headers/net.minecraft.stats.h"
@@ -122,20 +90,20 @@ Level::TlsKey Level::tlsIdxLightCache = CreateLevelTlsKey();
// W - lighting value requires write
#endif
thread_local bool Level::m_threadInstaTick = false;
thread_local Level::lightCache_t* Level::m_threadLightCache = nullptr;
void Level::enableLightingCache() {
// Allocate 16K (needs 32K for large worlds) for a 16x16x16x4 byte cache of
// results, plus 128K required for toCheck array. Rounding up to 256 to keep
// as multiple of alignement - aligning to 128K boundary for possible cache
// locking.
void* cache = (unsigned char*)XPhysicalAlloc(
m_threadLightCache = (lightCache_t*)XPhysicalAlloc(
256 * 1024, MAXULONG_PTR, 128 * 1024, PAGE_READWRITE | MEM_LARGE_PAGES);
LevelTlsSetValue(tlsIdxLightCache, cache);
}
void Level::destroyLightingCache() {
lightCache_t* cache =
static_cast<lightCache_t*>(LevelTlsGetValue(tlsIdxLightCache));
XPhysicalFree(cache);
delete m_threadLightCache;
}
inline int GetIndex(int x, int y, int z) {
@@ -524,13 +492,11 @@ void Level::flushCache(lightCache_t* cache, uint64_t cacheUse,
// 4J - added following 2 functions to move instaBuild flag from being a class
// member, to TLS
bool Level::getInstaTick() {
return reinterpret_cast<std::intptr_t>(LevelTlsGetValue(tlsIdx)) != 0;
return m_threadInstaTick;
}
void Level::setInstaTick(bool enable) {
void* value = 0;
if (enable) value = (void*)1;
LevelTlsSetValue(tlsIdx, value);
m_threadInstaTick = enable;
}
// 4J - added
@@ -3177,8 +3143,7 @@ int Level::getExpectedLight(lightCache_t* cache, int x, int y, int z,
// this thread
void Level::checkLight(LightLayer::variety layer, int xc, int yc, int zc,
bool force, bool rootOnlyEmissive) {
lightCache_t* cache =
static_cast<lightCache_t*>(LevelTlsGetValue(tlsIdxLightCache));
lightCache_t* cache = m_threadLightCache;
uint64_t cacheUse = 0;
if (force) {