thenfour / digifujam

32 stars 1 forks source link

ping optimization: break out tasks #257

Closed thenfour closed 2 years ago

thenfour commented 2 years ago

there was an old bug about ping optimization and i want to bring it back, on the topic of scalability. problem is that pings are sent to all clients in a room, which is no biggie by itself, but pings don't scale. They contain the user list for the whole "world", so the cost increases exponentially.

pings currently do many things at once which is also a problem, because i have seen in profiling that pings are a big spike in CPU and allocations due to all this. It's where we manually clean abandoned users, graffiti, calculate ping, and gather up a world population.

the reason it's done all at the same time was considered an optimization. it's better to calculate world pop once and send to all users, rather than calculate it each time a client asks for it on demand. but there are other ways.

improvement proposal: separate these tasks into individual timers and just cache the results. let clients fetch on demand, probably on another timer. this will allow us to do things MUCH less frequently without loss of UX. and it also decouples tasks so there's no spike in CPU.

and for calculating ping MS, it can now be done on a per-user basis.

related : #14


Notes after implementation: Massive optimizations, even though it won't affect much without many users.

Overall the "ping" payload is now staggered, calculated at the global level, and went from about 14kb to 700 bytes.

I did no performance measurements other than some size profiling. Doing tests for scale is a bore.