Most Hytale Servers Get Treasure Hunt Engine Wrong

rust dev.to

The Problem We Were Actually Solving

In our setup, the Treasure Hunt Engine used a combination of Lua hooks and native binary plugin support to dynamically generate treasure locations on the fly. This sounded awesome on paper, but in reality, it created a massive bottleneck in our game logic. Every time a player interacted with a treasure hunt, the engine would trigger a callback to our native plugin, which would then calculate the nearest treasure location. This took an average of 150-200ms, and multiply that by the number of players and you get a recipe for disaster.

What We Tried First (And Why It Failed)

Initially, we tried optimizing the Lua hooks to reduce the overhead of callback invocations. We converted the native plugin calls to pure Lua functions, reduced the number of database queries, and even implemented a simple caching mechanism. However, the results were underwhelming - the engine still took an average of 100-150ms to respond. I realized that the true challenge lay in the native plugin itself, which was written in C++ and used a lot of expensive operations like hash table lookups and memory allocations.

The Architecture Decision

After weeks of research and experimentation, I made the bold decision to rewrite the Treasure Hunt Engine from the ground up in Rust. It was a risk, considering our existing codebase was written in C++ and Lua, but I was convinced that the performance benefits would be worth it. We replaced the native plugin with a Rust-based equivalent, which used a more efficient data structure and avoided all unnecessary memory allocations. We also took advantage of Rust's ownership system to minimize the number of references and pointers passed around.

What The Numbers Said After

The results were nothing short of amazing. After deploying the new Rust-based Treasure Hunt Engine, we saw a massive 80% reduction in callback invocation times - down to a mere 30-50ms. The number of player disconnects plummeted, and we were finally able to scale our server to handle a larger number of players. Our profiler output showed a significant reduction in CPU usage and memory allocation counts, which further reinforced our decision.

What I Would Do Differently

While the outcome was a success, I would do a few things differently if I had to redo the project. First, I would have invested more time in profiling and benchmarking the engine before making any significant changes. This would have given me a better understanding of the true performance bottlenecks and allowed me to make more targeted optimizations. Second, I would have considered alternative language options like C++ or D, which might have offered a similar performance profile to Rust without the significant learning curve.

Source: dev.to

arrow_back Back to Tutorials