• Urist@lemmy.ml
    link
    fedilink
    English
    arrow-up
    40
    ·
    2 months ago

    Many of these have C-bindings for their libraries, which means that slowness is caused by bad code (such as making a for loop with a C-call for each iteration instead of once for the whole loop).

    I am no coder, but it is my experience that bad code can be slow regardless of language used.

    • Ephera@lemmy.ml
      link
      fedilink
      arrow-up
      30
      ·
      edit-2
      2 months ago

      Bad code can certainly be part of it. The average skill level of those coding C/C++/Rust tends to be higher. And modern programs typically use hundreds of libraries, so even if your own code is immaculate, not all of your dependencies will be.

      But there’s other reasons, too:

      • Python, Java etc. execute their compiler/interpreter while the program is running.
      • CLIs are magnitudes slower, because these languages require a runtime to be launched before executing the CLI logic.
      • GUIs and simulations stutter around, because these languages use garbage collection for memory management.
      • And then just death by a thousand paper cuts. For example, when iterating over text, you can’t tell it to just give you a view/pointer into the existing memory of the text. Instead, it copies each snippet of text you want to process into new memory.
        And when working with multiple threads in Java, it is considered best practice to always clone memory of basically anything you touch. Like, that’s good code and its performance will be mediocre. Also, you better don’t think about using multiple threads in Python+JS. For those two, even parallelism was an afterthought.

      Well, and then all of the above feeds back into all the libraries not being performant. There’s no chance to use the languages for performance-critical stuff, so no one bothers optimizing the libraries.

      • TimeSquirrel@kbin.melroy.org
        link
        fedilink
        arrow-up
        11
        ·
        2 months ago

        For example, when iterating over text, you can’t tell it to just give you a view/pointer into the existing memory of the text. Instead, it copies each snippet of text you want to process into new memory.

        As someone used to embedded programming, this sounds horrific.

        • Ephera@lemmy.ml
          link
          fedilink
          arrow-up
          7
          ·
          2 months ago

          Yep. I used to code a lot in JVM languages, then started learning Rust. My initial reaction was “Why the hell does Rust have two string types?”.
          Then I learned that it’s for representing actual memory vs. view and what that meant. Since then I’m thinking “Why the hell do JVM languages not have two string types?”.

          • calcopiritus@lemmy.world
            link
            fedilink
            arrow-up
            2
            ·
            2 months ago

            I’m not a java programmer, but I think the equivalent to str would be char[]. However the ergonomics of rust for str isn’t there for char[], so java devs probably use String everywhere.

            • Ephera@lemmy.ml
              link
              fedilink
              arrow-up
              1
              ·
              2 months ago

              Nope, crucial difference between Java’s char[] and Rust’s &str is that the latter is always a pointer to an existing section of memory. When you create a char[], it allocates a new section of memory (and then you get a pointer to that).

              One thing that they might be able to do, is to optimize it in the JVM, akin to Rust’s Cow.
              Basically, you could share the same section of memory between multiple String instances and only if someone writes to their instance of that String, then you copy it into new memory and do the modification there.
              Java doesn’t have mutability semantics, which Rust uses for this, but I guess, with object encapsulation, they could manually implement it whenever a potentially modifying method is called…?

    • aluminium@lemmy.world
      link
      fedilink
      arrow-up
      7
      ·
      2 months ago

      At least with Java, its the over(ab)use of Reflections and stuff like dependency injection that slows things down to a crawl.