Saturday, 15 August 2015

java - Unusual Performance Setting Integer Array Values -



java - Unusual Performance Setting Integer Array Values -

this unusual case came while profiling specialised collection i've been working on.

the collection pretty much 2 arrays, 1 int[] array of keys, , 1 object[] array of values, hash function providing rapid lookup. it's working nicely, i've come profiling code , getting weird results; profiling i've decided old fashioned way, grabbing system.currenttimemillis(), running test on , on , checking how much time has elapsed, so:

long stime = system.currenttimemillis(); (int index : indices) foo.remove(index); long took = system.currenttimemillis() - stime;

in test have foo prepared 200,000 entries, , pre-generated list of indices remove. reset , run test using loop one thousand repetitions , add together took running total.

now, commands extremely results compared other info types, except remove(int) method. however, i've been struggling figure out why, removal method identical get(int) method (other removal obviously), shown:

public object get(int key) { int = getindex(key); // hashes key , locates homecoming (i >= 0) ? this.values[i] : null; } public object remove(int key) { int = getindex(key); // same above if (i >= 0) { --this.size; ++this.modifications; // concurrent access behaviour this.keys[i] = 0; // 0 indicates null entry object old = this.values[i]; this.values[i] = null; homecoming old; } homecoming null; }

while expect removal take bit longer, they're taking more 5 times long execute get(int). however, if comment out line this.keys[i] = 0 performance becomes identical get(int).

am right in observing issue assigning value int[] array? i've tried commenting out this.values operations , experience same slow times, leaving this.values while commenting out this.keys[i] = 0 consistently solves problem; i'm @ total loss what's going on, there done it?

the performance still considering removals relatively rare, seems unusual setting value in int[] seemingly having such big impact, i'm curious know why.

the code written doesn't work concurrently. if there's other concurrency code not shown, source timing differences. other that, cause simply accessing keys[] array in add-on values[] array changes memory access patterns. instance, switching registers memory locations, l1 cache l2 cache, or l3 cache, or main memory. 'false sharing' illustration of degradation pattern. 'mechanical sympathy' name used tuning current hardware architectures.

java arrays performance

No comments:

Post a Comment