Skip to content

Commit

Permalink
Avoid allocating long[] holder for size = 0
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Aug 22, 2023
1 parent e2c0c05 commit 423a64e
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions core/src/main/java/org/jruby/RubyHash.java
Original file line number Diff line number Diff line change
Expand Up @@ -1356,11 +1356,18 @@ public RubyFixnum hash() {
@JRubyMethod(name = "hash")
public RubyFixnum hash(ThreadContext context) {
final int size = size();
long[] hval = { Helpers.hashStart(context.runtime, size) };
if (size > 0) {

long hash = Helpers.hashStart(context.runtime, size);

if (size != 0) {
long[] hval = {hash};

iteratorVisitAll(context, CalculateHashVisitor, hval);

hash = hval[0];
}
return context.runtime.newFixnum(hval[0]);

return context.runtime.newFixnum(hash);
}

private static final ThreadLocal<ByteBuffer> HASH_16_BYTE = ThreadLocal.withInitial(() -> ByteBuffer.allocate(16));
Expand Down

0 comments on commit 423a64e

Please sign in to comment.