Skip to content

Commit

Permalink
Optimize toImmutable on primitive sets
Browse files Browse the repository at this point in the history
Signed-off-by: Mohammad Rezaei <mohdev@rezaei.cc>
  • Loading branch information
mohrezaei committed Oct 23, 2020
1 parent d1ab331 commit 2806fd6
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,17 @@ public class <name>HashSet extends Abstract<name>Set implements Mutable<name>Set
{
return <name>Sets.immutable.with(this.<type>Iterator().next());
}
<name>HashSet mutableSet = <name>HashSet.newSetWith(this.toArray());
<name>HashSet mutableSet;
if (this.table.length == smallestPowerOfTwoGreaterThan(this.size() \<\< 1)) // table is not too big
{
mutableSet = this;
this.copyOnWrite = true;
}
else
{
mutableSet = new <name>HashSet(this.size());
mutableSet.addAll(this);
}
return new Immutable<name>HashSet(mutableSet.table, mutableSet.occupiedWithData, mutableSet.zeroToThirtyOne, mutableSet.zeroToThirtyOneOccupied);
}

Expand Down

0 comments on commit 2806fd6

Please sign in to comment.