4 ms·
I appreciate that nothing happens at the same time on a single core but CPU time is shared between threads so it 'appears' as if more than one thing happens at
by andrew1 17y ago
I appreciate that nothing happens at the same time on a single core but CPU time is shared between threads so it 'appears' as if more than one thing happens at once. A good example of this is a web browser on a single core machine - the browser does not freeze up while it is downloading data. That is because the CPU time is shared between the UI thread and the other worker threads.
- axod 17y agoPossibly in some browsers. An alternate (better) model would be to simply have a single thread with a main loop, have async networking, and UI updates periodically in the same thread. while(true) { networking.check(); // Check if any sockets are ready for read/write/connect ui.update(); // Update the UI a bit if needed } The only case this would be a terrible idea is if you don't have control of all the code, or need to interface to things that may block/crash/etc.
- gaius 17y agoThis is how Tcl works.
- coliveira 17y agoThis only works if the UI library has good performance. During years this was a big issue in Java, and why it really needed multi-threading.
- scott_s 17y agoNo modern web browser controls all of the code, since it must execute arbitrary JavaScript - which can block and crash. Everyone doing something doesn't make them right. But when all major instances of an application are implemented differently than you think is best, perhaps you don't understand the problem as well as you think you do. Chrome, I think, is the best browser architecture, and it looks like IE and Firefox will adapt something similar. I think they use separate processes to manage tabs instead of threads, but it's still parallel.
- axod 17y ago>> "No modern web browser controls all of the code, since it must execute arbitrary JavaScript - which can block and crash." That's a silly argument. The following can't block or crash. while(true) { jsRuntime.executeInstruction(); // Other stuff. }
- scott_s 17y agoExecute a single JavaScript instruction at a time? I doubt the performance of that would be acceptable. But if you're aware of any browsers doing that, I'd like to know.
- axod 17y agoIf I were to write a browser right now, it's how I'd do it. You would more likely execute a few js instructions per loop, depending on what else you have to do in that loop also - network check, ui update, etc. Why would there be a performance hit in doing js instructions one by one though ;) A loop isn't expensive.
- scott_s 17y agoYou would have terrible cache performance, since you would constantly bounce back and forth between the JavaScript VM and other browser code.
- axod 17y agoCode is code. If you can explain that a bit more I'd be interested. Obviously you wouldn't update the UI every time you execute a js instruction. That would be insane. I just put 1 js instruction in the loop to have the minimum unit, in case anything else needs to be updated very quickly at the same time - eg some animation etc
- tsuraan 17y agoCPUs (or cores in a CPU) have a pretty small instruction cache. On a Core2, I believe it's 32K cache per core. It's somewhat expensive (slow) to fetch instructions from RAM to L1, but once code is in L1, reading from it is immediate, or close to it. If you have a two-core machine where one core can do most of your js dispatch logic, and the other core can do the inner loop of your rendering logic, then you can have much better performance than constantly swapping out your logic on the cache of a single core. I'm really not sure how useful it is to cache-optimise a browser. You need to be able to fit a significant amount of logic into 32K in order to take advantage of cache. When I used to do realtime image processing (AR), I could get an order of magnitude speedup by just getting my logic to fit into 32K; image processing code can literally go from 3fps to 30fps just by tightening code to the point where it can fit in L1 cache. I don't know if it's possible to fit significant amounts of rendering logic or js logic into 32K, but if it is, then dedicating a core to each of those functions could give a significant speedup.
- andrew1 17y agoI can't reply to your comment to this. Pretty much everything I do at work involves communicating with resources which may block/crash so it wouldn't really be practical to put everything in a single while loop. It sounds a little bit to me like you're re-inventing the wheel - multi-threading means you don't have to go to the pains of breaking your long-running tasks up into pieces, the infrastructure takes care of that for you. I'm not trying to convert you, it sounds like you're perfectly happy and successful working in a single threaded environment. It just sounds like you're doing quite a lot of work to avoid multiple threads, which really aren't that difficult to manage.
- axod 17y ago... and then you have to deal with all the concurrency and blocking issues that come with outsourcing your timeslicing. Timeslicing isn't hard. It's even simpler if you're the one writing the code. If you are in the horrible position of having to interface with blocking/crashing code though, great shame :(
- andrew1 17y agoOne of the big pains of my work day is our accounting system which, while being great at what it does well, can be abysmally slow to respond to queries (the simple question of how much of X do we own now, takes it around three minutes to answer...). We know from experience though that internally it can deal with up to three requests at a time without it slowing down. If I only had one thread then three requests (which block) would take me nine minutes to process, with three threads I can get all the results back in three minutes.
- axod 17y agoInterfacing with a broken bad interface is never fun. But perhaps fix the problem instead of working around it and letting it spread...
- andrew1 17y agoUnfortunately that's not practical, it's third party software and at least the last time we checked there wasn't a better alternative with the same functionality.
- dkersten 17y agonothing happens at the same time on a single core Not always true. There are multiple levels of parallelism in modern CPUs and cores is only one of these, eg instruction-level parallelism, hyperthreading etc Anyway, theres more to concurrent programming than doing multiple things at once in parallel - theres also the use of threads to turn a synchronous blocking call into an asynchronous one, like you said.