Rush - memoize issues

Viewed 52

I have a game I'm trying to code and I want to memoize a next_guess function, as it's costly. I know there are some memoization crates, but I have some weird requirements and the whole project is an exercise in learning Rust, so I wanted to know how a true Rustacean would think about it. The next_guess function is in the impl of a Node struct. The game tree branches very fast, so each level has dozens of possible next step nodes to analyze. If I add a reference to the memoize HashMap to the Node struct for next_guess to use, I can't make it mutable, as there can't be multiple mutable references to it. But I needed it to be mutable so I can add new values. I thought using globals was a no-no, but is setting the HashMap as a lazy_static the right approach or should I use an unsafe block to access it (can I do that?). Thanks

1 Answers

Just my two cents:

  • Having a HashMap within each Node will consume lots of space.
  • If you already have a specific Node at hand, do you really want to look it up in an HashMap? Couldn't the Node itself store the cached value (possibly in an Option<BestNextGuess>?
Related