Re: Enums: Properties vs. Methods
Robert Klemme wrote:
Lew<l...@lewscanon.com> wrote:
Yes. So? that's what HotSpot does, except it's not one per instance,
it's N>= 1 per instance, potentially. HotSpot optimizes for
individual hot spots in the code, hence the name.
Hmm... Considering
public void hotSpotMethod(Valve v) {
// code
if (v.isOpen()) {
// ...
}
else {
// ...
}
// more code
}
Since this method can be invoked with different Valve instances the
only optimization I can see so far is
public void hotSpotMethod(Valve v) {
// code
if (v.open) {
// ...
}
else {
// ...
}
// more code
}
or are you saying that we can actually get this?
I make no claims whatsoever about individually crafted examples designed to
show where HotSpot might fail. I only repeat publicly available information
about what it does.
public void hotSpotMethod(boolean valve_open) {
// code
if (valve_open) {
// ...
}
else {
// ...
}
// more code
}
....
Yes, it wins, but that has nothing to do with memory or performance in
the JVM.
For the toy example given I would expect no difference, since the
final variables ocmpile to constants anyway.
But those final variables are only constant /per instance/. How would
hotspot be able to inline them?
Per instance, or perhaps per call. Of course. It might then un-inline them
for a later call, then re-inline them - even the exact same call if conditions
change that affect optimization.
The beauty of HotSpot is that it accounts for changing run-time circumstances,
so it can perform optimizations that elude static analysis.
You should read about it.
....
As for JVM effects, I would expect the two approaches to be
indistinguishable. Constant variables are compiled into constants in
the code, so they get treated identically with constants (being, after
all, constants in truth) at run time.
I don't think these final members (which are not static!) can be
compiled into the code because there is just one class (and hence one
instance of each method) but multiple instances with different sets of
values for their final members.
The JLS requires them to be compiled into the code. And regardless of what
you don't think, that's exactly what happens. You should read about it.
Start here: JLS, ??17.5.3, as I cited earlier.
--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg