performance - How/why is Integer caching faster in Java? -
according java language specification, section 5.1.7, java caches integer
s -128 127 performance optimization.
so when compare a == b
, a
& b
integers in caching range, returns true though they're different objects.
mechanically, performance benefits result caching? according this answer, "the purpose save memory, leads faster code due better cache efficiency." how lead faster code? how might use feature improve performance in real code write?
does have following method found in integercache
class?
public static integer valueof(int i) { assert integercache.high >= 127; if (i >= integercache.low && <= integercache.high) return integercache.cache[i + (-integercache.low)]; return new integer(i); }
the performance benefit nothing ==
. real reason cache allows valueof
avoid creating lots of objects when same "small" integers boxed repeatedly. integer
objects occupy space (at least 16 bytes). creating lots of them means garbage collector needs run more frequently.
my understanding java team did lot of analysis of real-world applications, , came conclusion integer
cache worthwhile optimization.
(the reason nothing ==
cannot rely on ==
working integer
objects. therefore, code ... without ensuring integers in specified range ... buggy. , if range check ensure numbers in-range, spend more on save using ==
.)
"... leads faster code due better cache efficiency." how lead faster code?
the cache efficiency talking hardware level memory cache modern processors use deal mismatch between cpu , memory speeds. if application has 1 integer
object representing number 1
, number appears frequently, chances memory addresses holding 1
object in (memory) caches increased. cache hits means faster code.
does have following method found in integercache class?
erm ... yes.
Comments
Post a Comment