Efficiency with the functional paradigm

Viewed 167

Since functional code by definition avoids mutability as best as it can, one could write a stateful program by unfolding state from the previous over time. So, I'm writing a game in F# purely in the functional style, and of course games tend to have lots and lots of state. I essentially use records for game objects (such as players), and I simply map over all these states to get the next state. This works very nicely. But, as my game gets more and more structurally complex, I'm worried that it will get sluggish because of all that copying each update. I'm wondering how I might try to avoid these pitfalls in the future (not right this moment since it's not much of an issue yet, though).

So are there any significant optimizations used in the functional style that F# doesn't do for me, specifically related to copying large chunks of data when only small portions might be changed? Also, is there anything that F# has that I can use to my advantage in this same way?

One more thing -- here are my two main concerns that might not even be true. I'd love to get these straightened out:

  1. Garbage collection. Since I end up copying thousands of records a second, there's got to be a massive amount of object generation, all of which is extremely short-lived since I just throw away old state. This seems to be a probable reason for the short but barely noticeable jumps in the game every second or so.

  2. Is F# efficient at copying records with all but a few fields updated? If it's not, how can I improve efficiency myself?

1 Answers
Related