Lack of performance when using short or int on 32-bit platforms, in all cases, except for short [] compared to int [], and even then the cons usually outweigh the pros.
Assuming you are working on x64, x86 or ARM-32:
- When used, 16-bit SHORTs are stored in 32-bit or 64-bit integer registers, just like int. That is, when short is used, you get no memory or performance compared to int.
- When on the stack, 16-bit SHORTs are stored in 32-bit or 64-bit slots to support stack alignment (just like int). You do not get performance or memory benefits when using SHORT and INT for local variables.
- When passed as parameters, SHORTs automatically expand to 32-bit or 64-bit when they are pushed onto the stack (as opposed to just pressing int). Your code here is actually slightly less performance and has a slightly larger (code) amount of memory than if you were using ints.
- When storing global (static) variables, these variables automatically expand to occupy 32-bit or 64-bit intervals to ensure alignment of pointers (references). This means that you are not benefiting from performance or memory for using SHORT and INT for global (static) variables.
- When storing fields, they live in a structure in the heap memory, which is mapped to the class layout. In this class, fields are automatically padded to 32-bit or 64-bit in order to maintain field alignment on the heap. You do not benefit from performance or memory by using SHORT for fields and INT.
The only advantage you'll ever see when using SHORTs or INTs is when you allocate an array of them. In this case, an array of N shorts is approximately half the size of an array of N ints.
Besides the performance benefits of having variables in the hot loop together in the case of complex but localized math in a large array of shorts, you will never see the benefits of using SHORTS and INT.
In all other cases, such as shorts, used for fields, globals, parameters, and locales, except for the number of bits that it can store, there is no difference between SHORT and INT.
My advice, as always, is to recommend that before making your code more difficult to read and more artificially limited, try BENCHMARKING your code to find out where the bottlenecks are in memory and the processor, and then enable them.
I highly suspect that if you ever encounter the fact that your application suffers from using int rather than short, then you have long since dropped Java for fewer heads in memory / processor standby mode, so all this work is pre-directed For nothing.
source share