5 ms·
Sleep and functions like it should be [almost] banned... They choose a time that computation happens. But computation should instead be done as soon as possi
by londons_explore 4y ago
Sleep and functions like it should be [almost] banned...
They choose a time that computation happens. But computation should instead be done as soon as possible, and the output delayed to the necessary time.
For example, in an animation, you don't need to wake up the CPU every 16ms to compute the next frame. You should instead just compute all the frames and send it off to the GPU to display in order and with the right timings.
- nemothekid 4y agoAnd then when every time the user interacts with the system you would have to trash the GPU queue and recompute everything. If you could compute everything before hand, it would be far simpler just encode the entire scene as a video. For example, consider a game server that must handle inputs from multiple clients. It can't just compute everything and queue it off to the network because a user input may change the entire game world. A "computation" loop provides a consistent model of which all the game interaction happens.
- deleted 4y ago[deleted]
- kevingadd 4y ago> They choose a time that computation happens. But computation should instead be done as soon as possible, and the output delayed to the necessary time. This is fundamentally not a solution for any problem where you're reading input from outside. If that animation is reacting to user input, rendering the frames 100ms ahead will create 100ms or more of "input lag" that the user can see and feel and they hate it.
- londons_explore 4y agoWell you only do the computation when you have the necessary inputs available, and in the case of something interactive that means waiting till you have the necessary keypresses (or lack of keypresses). You can still compute ahead if you have a good guess what inputs you'll receive (eg. You predict that no keys will be pressed), but you have to be happy to throw away the results if your prediction was wrong.
- ec109685 4y agoHmm, what’s wrong with: sleep until reddy to blit next frame blit frame render next frame repeat
- sgtnoodle 4y agoSleeping a thread doesn't "choose" when something happens, the programmer does. Sleep functions are just abstractions over hardware timer interrupts. There's some inherent complexity there, and abstracting it away is naturally going to impose some limits or compromises. Windows' "sleep" function seems to be especially limited, but being able to precisely schedule a CPU thread to a hardware timer is important for solving a lot of interesting problems. As you push a program to its limits, of course you'll need to carefully choose the abstractions that most optimally achieve your requirements given the available hardware. I agree that bare sleep style functions rarely make sense. Blocking select-style calls with a timeout often do, though, which are largely equivalent to sleep. The whole point of a CPU is to be general purpose. A CPU isn't optimal for anything. The magic of a computer is that it makes "anything" possible. Why would you want to ban sleep? If somebody makes a crappy arcade game with terrible latency and jitter, good for them. Someone not yet familiar with the nuances of "sleep" will learn a lot faster building some crappy but functional programs than they will trying to learn a modern graphics API. A seasoned expert will readily barf out a crappy program with some sleep calls in it when they know it's good enough to solve the problem at hand.