Skip to content

Commit

Permalink
Merge pull request #146 from mkouba/string-switch-optimize-hash-colli…
Browse files Browse the repository at this point in the history
…sion

StringSwitch - fix hashCode collisons
  • Loading branch information
Ladicek committed Jan 4, 2023
2 parents 1185f56 + f51bebd commit ab1c927
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/quarkus/gizmo/EnumSwitchImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void writeBytecode(MethodVisitor methodVisitor) {
if (caseBlock != null && !fallThrough) {
caseBlock.breakScope(EnumSwitchImpl.this);
} else if (caseBlock == null) {
// Add empty fall through block
// Add an empty fall through block
caseBlock = new BytecodeCreatorImpl(EnumSwitchImpl.this);
caseEntry.setValue(caseBlock);
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/io/quarkus/gizmo/StringSwitchImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,23 @@ void writeBytecode(MethodVisitor methodVisitor) {
// Initialize the case blocks and lookup blocks
for (Entry<Integer, List<Entry<String, BytecodeCreatorImpl>>> hashEntry : hashToCaseBlocks.entrySet()) {
BytecodeCreatorImpl lookupBlock = new BytecodeCreatorImpl(StringSwitchImpl.this);
for (Entry<String, BytecodeCreatorImpl> caseEntry : hashEntry.getValue()) {
for (Iterator<Entry<String, BytecodeCreatorImpl>> it = hashEntry.getValue().iterator(); it.hasNext();) {
Entry<String, BytecodeCreatorImpl> caseEntry = it.next();
BytecodeCreatorImpl caseBlock = caseEntry.getValue();
if (caseBlock != null && !fallThrough) {
caseBlock.breakScope(StringSwitchImpl.this);
} else if (caseBlock == null) {
// TODO empty block
// Add an empty fall through block
caseBlock = new BytecodeCreatorImpl(StringSwitchImpl.this);
caseEntry.setValue(caseBlock);
}
// caseBlock.findActiveResultHandles(inputHandles);
BytecodeCreatorImpl isEqual = (BytecodeCreatorImpl) lookupBlock
.ifTrue(Gizmo.equals(lookupBlock, lookupBlock.load(caseEntry.getKey()), value)).trueBranch();
isEqual.jumpTo(caseBlock);
if (!it.hasNext()) {
// None of the labels is equal to the tested value - skip to the default block
lookupBlock.jumpTo(defaultBlock);
}
}
hashToLabel.put(hashEntry.getKey(), lookupBlock.getTop());
lookupBlock.findActiveResultHandles(inputHandles);
Expand Down

0 comments on commit ab1c927

Please sign in to comment.