Skip to content

Commit

Permalink
better initial allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
Baunsgaard committed Oct 23, 2024
1 parent 3447463 commit 6e9d587
Showing 1 changed file with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ private Map<T, Integer> parallelCreateRecodeMap(int estimate, ExecutorService po
tasks.add(pool.submit(() -> createRecodeMap(estimate, start, end)));
}
// make the initial map thread local allocation.
final Map<T, Integer> map = createRecodeMap(estimate, 0, blk);
final Map<T, Integer> map = new HashMap<>((int)(estimate * 1.3));
createRecodeMap(map, 0, blk);
for(int i = 0; i < tasks.size(); i++) { // merge with other threads work.
final Map<T, Integer> map2 = tasks.get(i).get();
mergeRecodeMaps(map, map2);
Expand Down Expand Up @@ -181,6 +182,10 @@ private void mergeRecodeMaps(Map<T, Integer> target, Map<T, Integer> from) {
private Map<T, Integer> createRecodeMap(final int estimate, final int s, final int e) {
// * 1.3 because we hashMap has a load factor of 1.75
final Map<T, Integer> map = new HashMap<>((int) (Math.min((long) estimate, (e - s)) * 1.3));
return createRecodeMap(map, s, e);
}

private Map<T, Integer> createRecodeMap( Map<T, Integer> map, final int s, final int e) {
int id = 1;
for(int i = s; i < e; i++)
id = addValRecodeMap(map, id, i);
Expand Down

0 comments on commit 6e9d587

Please sign in to comment.