Efficiency with the functional paradigm
.net, f#, functional-programming
Solution
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?
Is it really the case that you are copying "large chunks of data"?
Normally, the advantage of immutable data structures is that you don't need to copy them. For example, if you have a functional Map, you may "update" some data, but most data remain untouched and are shared between the old and thze new map, as well as the bigger part of the map itself.
Problem
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: 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. Is F# efficient at copying records with all but a few fields updated? If it's not, how can I improve efficiency myself?