Question about passing parameters

29 views
Skip to first unread message

Ruud Reus

unread,
Apr 8, 2020, 1:22:07 PM4/8/20
to ClojureScript
I am working on a game in Clojurescript. Inside the game loop all the game's state is being tracked in a hashmap.
The gameloop is somewhat inspired by this article: https://swannodette.github.io/2013/08/02/100000-processes

(go (loop [refresh (timeout rate) s state]
     
(let [[v c] (alts! [refresh ui-channel])]
       
(condp = c
          refresh
(let [new-state (update-state s)]
                   
(render! new-state)
                   
(recur (timeout rate) new-state))
          ui
-channel (let [new-state (process-ui s v)]
                       
(recur refresh new-state))))))


I'm not at all sure if this strategy is sensible performance-wise but I'm trying to make a clear distinction between a functional part in which the game's state is being update by the update-state function, and a user interface part where a lot of side effects occur, brought together by channels, as the above snippet hopefully makes clear.

I have noticed that by doing this, I tend to make many function calls from the update-state function with the complete game state as a parameter, whether it is to update the location of a single game entity, or to decide whether a mouse click hit a player, etc.
Now my question is: is it considered bad (memory-wise and performance-wise) if you pass around the complete game state all the time? It is not a huge object (location, speed, orientation, color of about 25 entities). Or is it better to select just these pieces of data that are needed, pass them around, and then update the game state? I guess in other words, does it matter where and when the destructuring is being done?
Hope this question makes any sense, let me know if more specific examples are needed.

Phill Wolf

unread,
Apr 8, 2020, 7:02:18 PM4/8/20
to ClojureScript
I think it won't matter.  Passing a big collection to a function takes no more memory or time than passing a small collection.  If you use get-in or update-in, shallow structures will be a tiny, tiny bit faster to work with than deeper structures; but I wouldn't "fix" that "problem" if it would bend the program out of shape. 

Ruud Reus

unread,
Apr 9, 2020, 4:44:56 AM4/9/20
to ClojureScript
That's good to know, thank you. 
Reply all
Reply to author
Forward
0 new messages