1. 24

  2. 16


    I changed this:

    Set<Thing> aSet = new HashSet<Thing>();

    To this:

    Set<Thing> aSet = new HashSet<Thing>(0);

    on Java 1.3

    1. 4

      And it is a real cost — I had to spend time justifying the presence of that ‘0’ many times over the years as other developers questioned its purpose.

      Were they good developers? I almost never touch Java, but the purpose of the 0 was immediately obvious to me.

      I get the point about premature optimization, but optimizing for developers who don’t understand that containers have default capacities seems equally ridiculous.

      1. 6

        I suspect it was more of a “Oh, what’s this?” than a “You should remove this from the code”, but I can’t speak for the author.

        That being said, it was also pretty obvious to me. C# has similar constructors for collections to allow for pre-allocating large space, and I could see a zero-allocating version being a thing there as well.

      2. 1

         I had to spend time justifying the presence of that ‘0’ many times over the years as other developers questioned its purpose.

        I don’t get why he wouldn’t just put a code comment, or a regression test that checks memory usage. Then he doesn’t need to say anything.

        1. 10

          Per the article, he did both of those.

          1. 3

            That’s what I get for skimming and commenting. I deserve to look dumb there.

        2. 1

          All I could think of while reading this was “wow, Java is or used to be really problematic.”

          1. 3

            Can you be more specific?

            At least the optimization itself would be applicable to many programming languages: Setting a good initial size for a container.

            1. 0

              Amen. Makes me happy millenials killed it (along with its bastard companion XML).

              1. 15

                Java is alive and well. I have no idea how you come to the conclusion it was killed.

                1. 0

                  Java is dead in the sense C++ is dead. Once dominant, now one of the languages used by increasingly old guard. Of course there are still projects in Java, and even likely some people coding applets for old times sake.

                  But you can ignore Java at this point without handicapping your career.

                  1. 6

                    I am working for start-ups in the Bay Area and I can tell you that java is very much alive and well and used for new things every day. Nobody writes GUI apps in it anymore, but in the back-end it is widely popular.

                    1. 3

                      People do tons of new projects in C++ too. Still nothing like its heyday mid-90s.

                    2. 3

                      But you can ignore Java at this point without handicapping your career.

                      I agree with you, but I can’t think of a language that’s not true of. There are a lot of language ecosystems that don’t overlap much if at all - Java, Ruby, Python, .NET, Rust, Erlang…

                      1. 3

                        I think if you don’t have some level of understanding the level of reasoning that C works at, that can be a bit of a handicap, at least from a performance standpoint. Though that’s less of a language thing than it is about being able to reason about bytes, pointers and allocations when needed.

                        1. 0

                          That wasn’t true say 15 years ago. Back then if you wanted to have professional mobility outside certain niches, you had to know Java.

                          1. 2

                            I’m going to respectfully disagree. 15 years ago, you had Java, and you had LAMP (where the “P” could be Perl, PHP, or Python), and you had the MS stack, and you still had a great deal of non-MS C. After all that, you had all the other stuff.

                            Yes, Java may have been the biggest of those, but relegating “the MS stack” to “certain niches” perhaps forgets how dominant Windows was at the time. Yes, OSX was present, but it had just come out, and hadn’t made any significant inroads with developers yet. Yes, Linux was present, but “this is the year of Linux on the desktop” has been a decades-long running gag for a reason.

                            1. 1

                              MS stack was in practice still C++/MFC at the time, and past its heyday. The dotcom boom dethroned desktop, Windows and C++ and brought Java to prominence. By 2000, everyone and their dog were counting enterprise beans: C++ was still massive on Monster, but Java had a huge lead.

                              Then Microsoft jumped ship to .NET and C++ has not recovered even since. In mid-90s you were so much more likely to land a job doing C++ vs plain C; now it’s the opposite.

                              My karma shows I hurt a lot of feelings with my point, but sorry guys Java is in visible decline.

                              1. 1

                                Oh, my feelings weren’t hurt, and I don’t disagree that Java is in decline. I merely disagree with the assertion that, 15 years ago, you had to know Java or relegate yourself to niche work. I was in the industry at the time. My recollection is that the dotcom boom brought perl and php to prominence, rather than java.

                                Remember that java’s big promise at the time was “run anywhere”. Yes, there were applets, and technically servlets, but the former were used mostly for toys, and the latter were barely used at all for a few years. Java was used to write desktop applications as much as anywhere else. And, you probably recall, it wasn’t very good at desktop applications.

                                I worked in a “dotcom boom” company that used both perl and java (for different projects). It was part of a larger company that used primarily C++ (to write a custom webserver), and ColdFusion. The java work was almost universally considered a failed project due to performance and maintenance problems (it eventually recovered, but it took a long time). The perl side ended up getting more and more of the projects moving forward, particularly the ones with aggressive deadlines.

                                Now, it may be that, by 15 years ago, perl was already in decline. And, yes, java took some of that market share. But python and ruby took more of it. A couple years later, Django and Rails both appeared, and new adoption of perl dropped drastically.

                                Meanwhile, java soldiered along and became more and more prominent. But it was never able to shake those dynamic languages out of the web, and it was never able to make real inroads onto the desktop. It never became the lingua franca that it wanted to be.

                                And now it’s in decline. A decline that’s been slowed by the appearance of other JVM languages, notably scala (and, to a lesser degree, clojure).

                    3. 6

                      Incidents of Java in my life have only increased as my career has, I’m quite certain Java is far from dead and we’re all the worse for it. I’ve even worked for “hip” millennial companies that have decided they needed to switch to Java.

                      1. 5

                        Java is still alive and kicking, having a language that has proven itself to be good enough with a rich ecosystem with different vendors having implemented their own JVM, we’re all the worse for that because?

                  2. 1

                    Does anyone else have stories or practices in other languages of things that are both likely optimized and faster?