Slashdot Mirror


Firefox Javascript Engine Becomes Single Threaded

An anonymous reader writes with news about work on Mozilla's Javascript engine. Quoting Mozilla engineer Luke Wagner's blog: "With web workers in separate runtimes, there were no significant multi-threaded runtime uses remaining. Furthermore, to achieve single-threaded compartments, the platform features that allowed JS to easily ship a closure off to another thread had been removed since closures fundamentally carry with them a reference to their original enclosing scope. Even non-Mozilla SpiderMonkey embeddings had reportedly experienced problems that pushed them toward a similar shared-nothing design. Thus, there was little reason to maintain the non-trivial complexity caused by multi-threading support. There are a lot of things that 'would be nice' but what pushed us over the edge is that a single-threaded runtime allows us to hoist a lot data currently stored per-compartment into the runtime. This provides immediate memory savings."

36 of 346 comments (clear)

  1. You had me at.. by Sez+Zero · · Score: 5, Funny

    "memory savings".

    1. Re:You had me at.. by Anonymous Coward · · Score: 5, Funny

      There's a reason they're the most efficient browser when it comes down to memory usage. They actively work at it.

    2. Re:You had me at.. by hedwards · · Score: 5, Informative

      Which is why they always cream the competition in the benchmarks? Seriously, the only time I've ever seen it waste memory was during a session where Silverlight crashed. In general it tends to use very little in the way of memory.

      OTOH, given your post, I can only assume that you're using lynx, tons of extensions or are some sort of troll.

    3. Re:You had me at.. by jellomizer · · Score: 4, Informative

      However memory usage is one of those moot points.
      Often (Not all the time) you come to a trade off of performance vs memory usage.
      Sometimes it is better to store a bunch of data in memory just for quick reference then having to calculate it over and over again or worse need to get it from slower storage such as a disk.
      Now there are things you can do to optimize the memory usage so it isn't as wasteful. However if you goal on low memory foot print chances are you are going to be sacrificing performance to use less memory.

      --
      If something is so important that you feel the need to post it on the internet... It probably isn't that important.
    4. Re:You had me at.. by sirsnork · · Score: 4, Informative

      Have you told firefox not to remember all your downloads indefinately? It gets a little slow when it's remembered a couple of hundred downloads, and that was the default setting a long time ago, if you've been upgrading and never reinstalled your OS you've probably still got that default setting.

      In saying that, I use chrome now, once they decided to start bumping major versions every month or so, and upgrading broke at least one extension for a week, it was time to move to chrome. Oddly when I did I still preferred the FF UI, course now they have changed that to be more like chrome anyway.

      --

      Normal people worry me!
    5. Re:You had me at.. by Anonymous Coward · · Score: 5, Informative

      Sorry, "leaks memory like a BP pipeline" sounds like the best description for a browser which seems to absolutely refuse to free up RAM used by old images loaded by Javascript that have since been kicked off the page. I can set up a timer to reload an image every, say, half hour* (think "weather report precipitation map" or "webcam image") on a machine that should be up 24/7, come in the next day, and have Firefox's "This page has a script that is not responding" popup because the OS was too busy thrashing swap after physical RAM filled up and Firefox thought it was the script's fault. It's not often I see the Mem and Swap meters in GKrellm2 solidly maxed out. For debug purposes, I can have it reload that image every five seconds and watch the memory steadily creep up every five seconds without ever doing anything resembling GC. Of course, if I close that tab, the memory returns instantly.

      Now, you might say that for a kiosk that should remain up 24/7 like that, I should consider a different means of presenting the data. And ultimately, I did consider a different means. Because neither Chrome/Chromium nor Opera have this problem. Using the exact same script on both browsers, once it reloaded the image, the old one was booted out of memory immediately, or at least quickly enough that any extra memory use was marginal and incidental, and certainly not to the point where it would suck down all of swap like Firefox did. In fact, this script is still running on a kiosk here, it has been for a couple weeks straight now, and there's no memory wasting in sight. Firefox wouldn't have lasted the first night without manually reloading the entire page.

      So yes. It's Firefox. Firefox leaks memory. A lot. It does this due to very poor cache decisions and inferior GC techniques. Period. This has been a known problem for some time; a cursory glance through Stack Overflow will find numerous questions regarding this exact situation and Firefox, none of which have conclusive answers besides "stop using Firefox". And the only common thread in all of them is Firefox. The problem is Firefox. Firefox is the problem. It leaks memory.

      *: Note that this is using the trick of appending the image's URL with a dummy timestamp variable to trick Firefox into not just loading the old image from cache despite pragmas and meta tags telling it not to. Point still stands, though: Chrome/Chromium and Opera understand enough to unload the previous image from RAM with the exact same script and usage.

    6. Re:You had me at.. by kbg · · Score: 5, Informative

      What, the hell are you talking about?

      Didn't you read this slashdot article: http://developers.slashdot.org/story/12/01/17/1338225/notes-on-reducing-firefoxs-memory-consumption

      Here is one of the relevant parts from the Firefox developer:

      "Finally, Firefox 4 had a new HTML5 parser. It had a bug which meant that every time you set an innerHTML property on an element, some memory would leak. And that’s a pretty common operation on many websites. "

      Please think before you post again.

    7. Re:You had me at.. by MightyYar · · Score: 5, Insightful

      Me to friend: Use Firefox! It has these awesome extensions! Definitely install AdBlock Plus, NoScript, Tree-Style Tabs, Auto Pager, IE Tab2, GreaseMonkey, Firebug, and the Developer Toolbar!

      Friend: Does your Firefox use every bit of your system's memory?

      Me: Pffft, well, only if I use the extensions!

      In all seriousness, they need to do something about the extensions. Refuse to host leaky ones or something. Extensions can't be Firefox's killer feature if they make it eat all of your RAM.

      --
      W..w..W - Willy Waterloo washes Warren Wiggins who is washing Waldo Woo.
    8. Re:You had me at.. by KiloByte · · Score: 5, Interesting

      Where do you pull that "3X faster" from? Without proper AdBlock, Chrome seems to be only about as fast as Firefox. With AdBlock installed and properly configured (ie, not just the defaults, including "non" (hah) intrusive ads), Firefox runs circles around Chrome. If you can't bother setting it up, try NoScript.

      That's just speed. Now try to factor in privacy, features or configurability. For example, in their default setting, Chrome crashes to desktop if you close the only tab. Someone on the Firefox team had the "brilliant" idea to ape that, and sadly, Firefox does that by default as well now. But fear not: "Close Last Tab" restores sanity. Also, tabs on top: I find myself using keyboard to access tabs only ~50% of the time -- the tab bar is accessed at least an order of magnitude more often than the URL bar, thus it should be more accessible. Again, the Firefox team aped Chrome's misfeature, but you can restore sanity easily. Get rid of the useless search bar? Here you go. Get rid of Google's typo-jacking? browser.fixup.alternate.enabled=false and keyword.enabled=false (you can type "google goat porn" if you want to search; rename the default keyword to "g" for convenience). Fix the http:/// being hidden for 1/3 sites that still didn't go SSL? browser.urlbar.trimURLs=false. And so on, so on.

      --
      The creatures outside looked from Alt-Right to Antifa; but already it was impossible to say which was which.
    9. Re:You had me at.. by squiggleslash · · Score: 4, Informative

      Benchmarks are one thing, real life is another. I'm having to restart Firefox on a regular basis on my BRAND NEW EIGHT GIGABYTE LAPTOP. That's right:

      • In an age in which 1G netbooks are extremely common, I have eight times as much memory
      • It's a new laptop = fresh install of everything. The only thing carried over from my old machine was via Firefox Sync

      So if you're about to whine "But Squiggy! It must be your profile! And you need to get with the program and have 4G of RAM", there's your answer. I don't want to hear it.

      I really don't understand people like you who insist there isn't a problem when:

      • There's a massive groundswell of frustration and anger about the issue
      • Slashdot posts another "Mozilla reports - we found the problem! Next release will not have those memory problems!" article EVERY MONTH
      • Usage of CHROME, which in every other respect is an inferior browser to Firefox, is going through the roof

      You think we're making it up? You think everyone's just switching to Chrome for the hell of it? Clue: they're not switching because it's more compatible, or more user friendly, or has more features. Because nobody outside of a Google diehard would ever argue such a thing.

      We use Firefox for a bit. Some time goes by. Maybe we launch another application. Perhaps we view a PDF. And then it starts. It takes a second or more for Firefox to notice we clicked on something. The scrollbar is no longer real time. Switching a tab causes nothing to happen for ten seconds. We try closing tabs. We go to "about:memory" and hit every button. It seems... slightly faster. Or was that our imagination? Hmmm, it's gone slow again.

      I'm giving up. I downloaded Firefox 3.6 from Mozilla's website last night. I'm going to make it my default browser.

      --
      You are not alone. This is not normal. None of this is normal.
    10. Re:You had me at.. by Anonymous Coward · · Score: 5, Informative

      Of the 38 bugs listed there, about 5 were not memory leaks according to the subject. How many of those were because of addons?

      Then clicking on the first few bugs:
      - ONE GUY had addons and the dev requested a no-addon test. No reply.
      - This one look more promising but when the dev said "To find out it's a real memory leak or not, run 'prstat' command in a terminal.
      If the vaule of 'RSS' field of firefox row keeps increasing, it's a memory leak."... No reply.
      - ONE GUY had problem spamming the same URL into the URL bar. "Maybe have to close it" sounds like he wasn't even sure. Not a typical use case. (don't spam the URL bar with the same URL.
      - Agan one guy.
      - "The numbers you quote are not exceptional at all. They're probably caused by the malware-database that is updated in the background"
      - "The fix in bug 426236 fixes all the leaks, but it doesn't touch controllers..." fixed in 2008, but left open "just-in-case"

      Maybe you should actually look at the things before linking them?

    11. Re:You had me at.. by jlebar · · Score: 5, Informative

      In all seriousness, they need to do something about the extensions. Refuse to host leaky ones or something. Extensions can't be Firefox's killer feature if they make it eat all of your RAM.

      We are so on this. In fact, add-ons are the majority of what we talk about at MemShrink these days.

      In theory, leak checking is now part of the addons.mozilla.org review process, so new add-ons will all undergo a (very basic) leak check before they're approved. I'm sure we'll have to tweak as time goes on, but it's a start.

      http://jlebar.com/2011/11/13/The_carrot%2C_the_stick%2C_and_the_wrench%3A_Add-on_leaks_are_everyone's_problem..html

    12. Re:You had me at.. by epine · · Score: 4, Interesting

      We live in a world where 8GB of RAM costs $50. I'm not sure how much I actually care whether Firefox uses 500MB vs. 2GB anymore.

      When my FF 3.6.3 gets above 1GB of virtual memory, it becomes a sluggish pig on my 8GB system. Frequent half second pauses. Characters blurt out ten at a time when I'm typing into a simple web form. I've always assumed this was a GC gag of some kind with worse than linear scaling as memory fragments.

      If it was using 2GB and never slowing down, I'd write it off as the cost of having a plug-in architecture. I have a lot of plug-ins. That's the whole point.

    13. Re:You had me at.. by UnknownSoldier · · Score: 3, Insightful

      > We live in a world where 8GB of RAM costs $50.

      Ah sweet, so you're offering to pay for upgrading/replacing netbooks and embedded devices with more RAM !

      Stop being a myopic PC developer with RAM means nothing -- there are other platforms where we actually care about RAM usage.

    14. Re:You had me at.. by MightyYar · · Score: 3, Insightful

      That is awesome - thanks. And I just read your team's memory presentation by Nicholas Nethercote :)

      --
      W..w..W - Willy Waterloo washes Warren Wiggins who is washing Waldo Woo.
    15. Re:You had me at.. by hazah · · Score: 3, Funny

      It parses the HTML as a bonus!

    16. Re:You had me at.. by Anonymous Coward · · Score: 3, Informative

      If you want to do something constructive about it rather than just ranting, file a bug (preferably with a self-contained testcase attached) and CC :njn and :jlebar to help triage.

    17. Re:You had me at.. by eexaa · · Score: 3, Insightful

      "Chrome is 3X faster"

      Dear sir, how did you obtain such an accurate measurement?

    18. Re:You had me at.. by bursch-X · · Score: 5, Funny

      You've got an 8GB system and complaining about FF 3.6 memory leakage?

      I'm not sure whether you are aware of it, but there's this cool way of replacing your application with a newer version (new: less than two years old). For free! It's called updating. I'm aware it's a radical concept, but try it out, it's pretty cool.

      --
      There are two rules for success:
      1. Never tell everything you know.
  2. SetTimeout by Anonymous Coward · · Score: 5, Interesting

    Maybe I'm missing something, but then how will "threading" (i use that term extremely loosely) methods like SetInterval and SetTimeout be implemented?

    1. Re:SetTimeout by The+MAZZTer · · Score: 5, Informative

      I am going to assume you are aware of how those functions work (specifically that they cannot interrupt the current thread if it is busy; they are only handled if the thread is idle) and that they themselves are not multi-threaded or have anything to do with multi-threading. It's not 100% clear from your post, though.

      Multi-threading in JS is handled by web workers.

    2. Re:SetTimeout by Anonymous Coward · · Score: 5, Informative

      The same way it's always been implemented. setTimeout is event driven; it adds an event to the event queue to be executed at a later time. Once your code returns, the browser can spin the event loop again. The timer event will come up in due course and the browser will reenter the js engine to call your function.

    3. Re:SetTimeout by The+MAZZTer · · Score: 5, Interesting

      Oh I read your post wrong; anyways I did answer your question in the first bit.

      Code sample:

      setTimeout("alert('This alert fires second.');", 0);
      for (var i = 0; i < 10000000; i++) { // Maybe do something in here that takes awhile. }
      alert("This alert fires before the other one.");

      Either your browser will display the alerts in the proper order despite the 0ms timeout (because timers are only handled when idle because they are NOT threaded) or your browser will get angry at the 10,000,000 iteration-long loop.

  3. us to hoist a lot data currently by ShakaUVM · · Score: 4, Insightful

    "us to hoist a lot data currently stored"

    I really wish I knew what this phrase meant. It sounds fascinating.

    But seriously, if there's no performance gain from multithreading, it can be a really good idea to move away from the complexity of it. There's a lot of traps people can fall into with concurrent code if they don't know what they're doing.

    1. Re:us to hoist a lot data currently by PhrostyMcByte · · Score: 3, Interesting

      They mean to replace multiple per-thread copies of data with one single copy of it, accessible by all threads. No doubt part of Mozilla's latest push to reduce memory consumption.

      One feature of x86 is that, save for a specialized SSE streaming store instruction, any store made by one core is immediately visible to all the other cores—even when the old value was already in a core's cache.

      Maintaining such cache coherency involves a lot of overhead, so to get better scalability multi-threaded apps will sometimes adopt a "share-nothing" model: all threads get their own copy of the data, and no other threads will ever get to touch it. You trade memory and complexity for speed.

      It sounds like Mozilla has decided this trade off is no longer worth it, and so has done away with multi-threading all together. Perhaps they will use green threads instead of native threads, though that brings along its own bag of complexities.

  4. Re:But... by allcoolnameswheretak · · Score: 3, Informative

    Single threaded is the safest way to program. Creating a multithreaded application without a strong multithreaded architecture is asking for trouble.

    The only problem with this is the limited performance and the fact that modern computers are packing more and more CPU's whereas individual CPU performance has been stagnating. Sooner or later people will have to work out the tools to create safe multithreaded applications without requiring a special degree in parallellization.

  5. Re:But... by msclrhd · · Score: 4, Informative

    What the article is saying is that each instance of the JavaScript runtime runs in one thread. If you want multi-threaded JavaScript, you need multiple runtime instances (one per thread). This is how WebWorkers work.

  6. Re:But... by 0xABADC0DA · · Score: 4, Informative

    Basically in a dynamically typed language like JavaScript every property access, function call, or any other thing that can be changed dynamically could be changed at runtime by another thread. So you need locking for every method call, property access, etc to make sure it isn't changed by another thread while it's accessed in another.

    There are some generally fast locking algorithms for when locks are used mostly by the same thread... for instance in Java locks can be owned by a thread and that thread never has to lock or unlock at all, but instead it periodically checks if another thread has written a flag saying it wants to become the owner, then there is synchronization to pass off ownership. This works ok for Java, where there are fewer things that can change at runtime and they are explicitly listed out (using 'synchronized'), but in a dynamic language it's usually just too much overhead.

    Just for comparison V8 is even more extremely single-threaded, with execution that can only be interrupted at some certain points in the JS code.

  7. A mistake by claytongulick · · Score: 3, Informative

    My initial sense of this, is that they are making a huge mistake here. I'll have to do more research, but my feeling is that they are moving in the wrong direction with this decision.

    One of the really cool "baked in" things with functional style language is their fundamental support for horizontal scaling across CPUs. My hope has been that javascript evolves towards this, so that the generic suite of functional methods become massively performant on a larger scale with map/reduce/fold/each calls.

    Closures present a bottleneck here, but it seems like a reasonable runtime could make some intelligent prediction about whether the isolated function is a closure or not, and ship it off to a different CPU/thread depending on optimization strategies, or even estimated closure size. Even better, this could be done at runtime with some runtime optimization based on execution metrics of an anonymous/declared function in-context.

    At the point of calling the map/reduce/fold/each function, the runtime should be able to decide whether to parallelize out the call, or even use some language extensions to let the developer specify the threading.

    The point is, now that they're making this decision, all of those options are gone from FF. And at a terrible time too. As we move toward CPU architectures that encourage parallelism, Mozilla is taking js off the table as a first-class language able to easily exploit those new architectures. That strikes me as a huge mistake, and I'm struggling to understand the rationale.

    --
    Drinking habits can be dangerous. You can choke on the cloth and the nuns will wonder where their clothes are.
    1. Re:A mistake by msclrhd · · Score: 3, Informative

      The old JavaScript runtime supported multi-threading in the runtime itself. This resulted in the need for complex threading/locking code to make sure that data was being accessed correctly. This is hard to maintain, easy to get wrong, consumes more memory and slows down things like garbage collection.

      The new JavaScript runtime is single-threaded. WebWorkers each have their own instance of a (single-threaded) JavaScript runtime that may be running on different threads.

      You can still have your map/reduce/fold/... functions running in parallel; they will be implemented on top of WebWorkers. As the representation of each runtime is simpler, the engine as a whole can optimise the work between threads better and perform better code generation.

      It also means that garbage collection is faster as it (a) does not navigate all memory allocated across all threads and (b) only blocks the thread the garbage collector is currently running on.

  8. *More* outsourcing?! Bloody "globalization"! by zooblethorpe · · Score: 3, Funny

    Multi-threading in JS is handled by web workers.

    Oh, great! So now we're outsourcing even more textile jobs to anyone willing to work online, is that it? Sheesh!

    (On a more serious note, thank you for that informative link.)

    --
    "What in the name of Fats Waller is that?"
    "A four-foot prune."
  9. The boy who cried "Leak!" by jlebar · · Score: 4, Insightful

    So while they may have fixed most of the memory leaks (it still runs like shit on a Mac), let us not allow them to get complacent again. [snip] By not frequently reminding them about memory leaks, you are opening the door to yet more bloat going forward.

    I believe there's a parallel with a common fable:

    You saw a wolf. I said I shot many, that the wolf situation should be improved, but you kept seeing wolves. This repeated many times and made you angry.

    Now lots of others are saying that there really are fewer wolves these days. Indeed, you have no reason to believe that they're wrong.

    But because of the offense done to you some time ago, you're going to continue crying wolf, even given no evidence at all?

    I don't understand. I guess this is an attempt to punish us? Is it that you feel like we harmed you with our lies, so you should try to harm us with yours? I'm afraid that by crying wolf, and encouraging others to do the same, may just cause us to ignore you all, which is exactly the outcome you don't want.

    I'm very sorry you feel like Mozilla deceived and harmed you. But the malicious attitude here and elsewhere in this thread is getting old. Use Chrome, if you like! But don't encourage people to waste developers' time with false claims.

    1. Re:The boy who cried "Leak!" by jlebar · · Score: 4, Informative

      Firefox has 400 million users. (That's 1/20th the world's population, for those following along at home.) Any time we make a UI change, some of these 400 million people will love it, and some of those 400 million people won't. I'm sorry you didn't like this change.

      There's an add-on to get the status bar back. Can we move on, please?

      https://addons.mozilla.org/en-US/firefox/addon/status-4-evar/

  10. Re:OM NOM NOM! by Zan+Lynx · · Score: 3, Informative

    A dozen tabs huh? Okay, I get a dozen tabs open, including Facebook, G+, a few Amazon pages and Slashdot. I've got NoScript and Firebug installed.

    My Firefox 9.0.1 is using 1.3 GB of virtual, but only 514 MB of real, actual memory.

    Double check what the title of the column is where you're reading the memory usage from. Or try looking at about:memory in Firefox.

  11. Firefox 9 unusably slow on system with 128M of RAM by bzipitidoo · · Score: 4, Informative

    As an exercise in insanity, I tried to install a modern Linux distro on an old Pentium II system with only 128M of RAM. Used btrfs for the heck of it. It was all going well until I tried to run Firefox 9.0.1. Thrashed that system mercilessly. Never mind actually showing a web page, just starting up blank was extremely slow. Sometimes I saw the "unresponsive script" popups on Firefox's own Javascript. I hacked out everything that used memory, dumping the LXDE desktop environment for a plain old window manager (jwm), and this helped, but it's not enough. Turned off images and disabled Javascript. It still thrashes swap.

    Chrome didn't do any better. Firefox 3.5 worked okay on an even smaller system (96M of RAM). Version 3.6.8 + LXDE works fine on a system with 192M of RAM. Here's hoping their MemShrink effort scores more big wins.

    --
    Intellectual Property is a monopolistic, selfish, and defective concept. It is "tyranny over the mind of man"