Earlier quoted context omitted.
Did you ask yourself this question before you wrote this comment?
To answer your question: no, I didn't. It doesn't make sense to—I'm not here saying anything like, "I believe it's because X" without knowing whether X is true or not. If you think otherwise, explain how. (I have now answered your question. If you respond, how about doing so in the form of an answer to mine—and not evading it with another question?)
Inside the JVM: Arrays and how they differ from other objects
111–120 of 133 posts
Re: Inside the JVM: Arrays and how they differ from other objects
#112Earlier quoted context omitted.
In Java, it's src array, src offset, dest array, dest offset, length. It's a natural order of from, to. It's C memcpy() that's the odd one out by putting the destination before the source.
British people generally don't, but Americans very often use "to...from".
Even though I find memcpy and friends to be perfectly logical using the assignment analogy suggested by thwarted, I often need to re-read english sentences written that way.
Re: Inside the JVM: Arrays and how they differ from other objects
#113Earlier quoted context omitted.
> TLAB TLB?
The TLAB is the Thread Local Allocation Buffer. In short and a bit simplified, normally when you allocate memory, the allocator needs to synchronize between threads because RAM is a shared resource. This means that a thread that allocates a lot can disrupt the performance of other threads, among other weird effects. But there's a small buffer called the TLAB owned by each thread where this isn't true: Allocation in t…
Re: Inside the JVM: Arrays and how they differ from other objects
#114Earlier quoted context omitted.
Yet, somehow I doubt you write perfect code in all those languages. Do you cringe at yourself and conclude you just don’t care also?
No I don't, and when someone cringes looking at my code, I shut up, apply the fix and get to improve my skills on the language, instead of excusing myself.
Re: Inside the JVM: Arrays and how they differ from other objects
#115Earlier quoted context omitted.
That would be much more convincing if you linked to a proper JDK distribution that's not based on the OpenJDK.
Like on street markets, we are having bonus today, and you get two for the price of one. https://www.ptc.com/en/products/developer-tools/perc https://www.aicas.com/wp/products-services/jamaicavm/
You implied before that there are major JDK distributions which are not based on OpenJDK, but these two examples do not show that: these are niche JDKs.
Re: Inside the JVM: Arrays and how they differ from other objects
#116> Another curiosity of Java arrays is that they can have a size of zero. > This code will not result in an error message. This surprising feature is used primarily by code generators, which might create an array and then discover there are no values to place in it. What? How can someone at Oracle have written this? Zero-length arrays are used all the time when you call a function asking for an array of "the latest st…
Author here. Zero-length arrays are used in the narrow domain you mention. but they are rare in bread-and-butter Java programming. Given some of the other comments on this page, you can see that this aspect is new to multiple readers. And in my experience speaking to Java devs, the reaction of surprise is far more common than "of course, I use them."
Re: Inside the JVM: Arrays and how they differ from other objects
#117In my experience, just a little bit of insider knowledge goes a long ways to making better code. Arrays are fun things, especially when you do a deep dive into the System.arraycopy() function. But the same goes for all Collections in Java. For instance, most of them have a default size (mostly 10), and growing them is a costly operation. So knowing beforehand how large your collection can or may be, can benefit code.…
> For instance, most of them have a default size (mostly 10), and growing them is a costly operation. So knowing beforehand how large your collection can or may be, can benefit code. It's really a tricky balance. Over-allocating collections "just in case" can quite often be very expensive as well, since large array allocations tend to be fairly slow (since e.g. they typically won't fit in the TLAB).
It is sometimes really tricky. When I worked with streaming XML documents that were gigabytes in size, there is a really fine margin you have to work with.
However some general knowledge can be pretty useful. I saw colleagues just do "= new ArrayList(1000);" without considering the collection type or possible size. And besides being a bit ignorant, it can also be really confusing for other developers that take first look at such code.
Re: Inside the JVM: Arrays and how they differ from other objects
#118In my experience, just a little bit of insider knowledge goes a long ways to making better code. Arrays are fun things, especially when you do a deep dive into the System.arraycopy() function. But the same goes for all Collections in Java. For instance, most of them have a default size (mostly 10), and growing them is a costly operation. So knowing beforehand how large your collection can or may be, can benefit code.…
I cringe every single time I see a for loop for what System.arraycopy () has been providing since early days. For better or worse, it shows me that the author isn't that into Java.
The worst thing is, that System.arraycopy() is an optimized JNI call which is much faster than copying it by hand [1].
> For better or worse, it shows me that the author isn't that into Java.
The thing is though, most of the time arrays in Java are used because of performance. Or maybe ignorance. Because why would anyone voluntarily give up all the comforts of a List? It's not that Collections are very hard to find in the documentation. And most of the IntelliJ suggest switching to a Collection anyway.
1. https://www.javaspecialists.eu/archive/Issue124-Copying-Arra...
Re: Inside the JVM: Arrays and how they differ from other objects
#119In my experience, just a little bit of insider knowledge goes a long ways to making better code. Arrays are fun things, especially when you do a deep dive into the System.arraycopy() function. But the same goes for all Collections in Java. For instance, most of them have a default size (mostly 10), and growing them is a costly operation. So knowing beforehand how large your collection can or may be, can benefit code.…
Does anyone have any good book recommendations or links for insider knowledge of the JVM/Java? If Clojure focused all the better :)
In addition I really enjoyed exploring the JDK documentation. Especially Java It's not exactly JVM, but just wanted to share anyway :).
Re: Inside the JVM: Arrays and how they differ from other objects
#120In my experience, just a little bit of insider knowledge goes a long ways to making better code. Arrays are fun things, especially when you do a deep dive into the System.arraycopy() function. But the same goes for all Collections in Java. For instance, most of them have a default size (mostly 10), and growing them is a costly operation. So knowing beforehand how large your collection can or may be, can benefit code.…
The default size of an ArrayList has been 0 for a while. On the first insertion, it is initialized to 10.