Skip to content

Commit

Permalink
Showing 11 changed files with 83 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@
import org.jruby.truffle.builtins.CoreMethod;
import org.jruby.truffle.builtins.CoreMethodArrayArgumentsNode;
import org.jruby.truffle.core.array.ArrayOperations;
import org.jruby.truffle.core.hash.KeyValue;
import org.jruby.truffle.core.hash.HashOperations;
import org.jruby.truffle.core.string.StringOperations;
import org.jruby.truffle.language.control.RaiseException;
@@ -26,7 +27,6 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map.Entry;

@CoreClass("Truffle::Process")
public abstract class TruffleProcessNodes {
@@ -83,7 +83,7 @@ private Collection<SpawnFileAction> parseOptions(DynamicObject options) {
}

Collection<SpawnFileAction> actions = new ArrayList<>();
for (Entry<Object, Object> keyValue : HashOperations.iterableKeyValues(options)) {
for (KeyValue keyValue : HashOperations.iterableKeyValues(options)) {
final Object key = keyValue.getKey();
final Object value = keyValue.getValue();

Original file line number Diff line number Diff line change
@@ -18,7 +18,6 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;

public abstract class BucketsStrategy {
@@ -34,7 +33,7 @@ public abstract class BucketsStrategy {
private static final int[] CAPACITIES = Arrays.copyOf(org.jruby.RubyHash.MRI_PRIMES, org.jruby.RubyHash.MRI_PRIMES.length - 1);

@TruffleBoundary
public static DynamicObject create(RubyContext context, Collection<Map.Entry<Object, Object>> entries, boolean byIdentity) {
public static DynamicObject create(RubyContext context, Collection<KeyValue> entries, boolean byIdentity) {
int actualSize = entries.size();

final int bucketsCount = capacityGreaterThan(entries.size()) * OVERALLOCATE_FACTOR;
@@ -43,7 +42,7 @@ public static DynamicObject create(RubyContext context, Collection<Map.Entry<Obj
Entry firstInSequence = null;
Entry lastInSequence = null;

for (Map.Entry<Object, Object> entry : entries) {
for (KeyValue entry : entries) {
Object key = entry.getKey();

if (!byIdentity && RubyGuards.isRubyString(key)) {
@@ -203,8 +202,8 @@ public static void resize(RubyContext context, DynamicObject hash) {
assert HashOperations.verifyStore(context, hash);
}

public static Iterator<Map.Entry<Object, Object>> iterateKeyValues(final Entry firstInSequence) {
return new Iterator<Map.Entry<Object, Object>>() {
public static Iterator<KeyValue> iterateKeyValues(final Entry firstInSequence) {
return new Iterator<KeyValue>() {

private Entry entry = firstInSequence;

@@ -214,31 +213,12 @@ public boolean hasNext() {
}

@Override
public Map.Entry<Object, Object> next() {
public KeyValue next() {
if (!hasNext()) {
throw new NoSuchElementException();
}

final Entry finalEntry = entry;

final Map.Entry<Object, Object> entryResult = new Map.Entry<Object, Object>() {

@Override
public Object getKey() {
return finalEntry.getKey();
}

@Override
public Object getValue() {
return finalEntry.getValue();
}

@Override
public Object setValue(Object value) {
throw new UnsupportedOperationException();
}

};
final KeyValue entryResult = new KeyValue(entry.getKey(), entry.getValue());

entry = entry.getNextInSequence();

@@ -253,11 +233,11 @@ public void remove() {
};
}

public static Iterable<Map.Entry<Object, Object>> iterableKeyValues(final Entry firstInSequence) {
return new Iterable<Map.Entry<Object, Object>>() {
public static Iterable<KeyValue> iterableKeyValues(final Entry firstInSequence) {
return new Iterable<KeyValue>() {

@Override
public Iterator<Map.Entry<Object, Object>> iterator() {
public Iterator<KeyValue> iterator() {
return iterateKeyValues(firstInSequence);
}

Original file line number Diff line number Diff line change
@@ -19,7 +19,6 @@
import org.jruby.truffle.language.RubyNode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ConcatHashLiteralNode extends RubyNode {

@@ -37,10 +36,10 @@ public Object execute(VirtualFrame frame) {

@TruffleBoundary
private Object buildHash(DynamicObject[] parts) {
final List<Map.Entry<Object, Object>> keyValues = new ArrayList<>();
final List<KeyValue> keyValues = new ArrayList<>();

for (int i = 0; i < parts.length; i++) {
for (Map.Entry<Object, Object> keyValue : HashOperations.iterableKeyValues(parts[i])) {
for (KeyValue keyValue : HashOperations.iterableKeyValues(parts[i])) {
keyValues.add(keyValue);
}
}
17 changes: 8 additions & 9 deletions truffle/src/main/java/org/jruby/truffle/core/hash/HashNodes.java
Original file line number Diff line number Diff line change
@@ -49,7 +49,6 @@
import org.jruby.truffle.language.objects.AllocateObjectNodeGen;
import org.jruby.truffle.language.yield.YieldNode;
import java.util.Arrays;
import java.util.Map;


@CoreClass("Hash")
@@ -630,7 +629,7 @@ public DynamicObject eachPackedArray(VirtualFrame frame, DynamicObject hash, Dyn
public DynamicObject eachBuckets(VirtualFrame frame, DynamicObject hash, DynamicObject block) {
assert HashOperations.verifyStore(getContext(), hash);

for (Map.Entry<Object, Object> keyValue : BucketsStrategy.iterableKeyValues(Layouts.HASH.getFirstInSequence(hash))) {
for (KeyValue keyValue : BucketsStrategy.iterableKeyValues(Layouts.HASH.getFirstInSequence(hash))) {
yieldPair(frame, block, keyValue.getKey(), keyValue.getValue());
}

@@ -845,7 +844,7 @@ public DynamicObject mapBuckets(VirtualFrame frame, DynamicObject hash, DynamicO
int index = 0;

try {
for (Map.Entry<Object, Object> keyValue : BucketsStrategy.iterableKeyValues(Layouts.HASH.getFirstInSequence(hash))) {
for (KeyValue keyValue : BucketsStrategy.iterableKeyValues(Layouts.HASH.getFirstInSequence(hash))) {
arrayBuilderNode.appendValue(store, index, yieldPair(frame, block, keyValue.getKey(), keyValue.getValue()));
index++;
}
@@ -1065,11 +1064,11 @@ public DynamicObject mergeBucketsBuckets(VirtualFrame frame, DynamicObject hash,

final DynamicObject merged = allocateObjectNode.allocateHash(Layouts.BASIC_OBJECT.getLogicalClass(hash), new Entry[BucketsStrategy.capacityGreaterThan(Layouts.HASH.getSize(hash) + Layouts.HASH.getSize(other))], 0, null, null, null, null, false);

for (Map.Entry<Object, Object> keyValue : BucketsStrategy.iterableKeyValues(Layouts.HASH.getFirstInSequence(hash))) {
for (KeyValue keyValue : BucketsStrategy.iterableKeyValues(Layouts.HASH.getFirstInSequence(hash))) {
setNode.executeSet(frame, merged, keyValue.getKey(), keyValue.getValue(), isCompareByIdentity);
}

for (Map.Entry<Object, Object> keyValue : BucketsStrategy.iterableKeyValues(Layouts.HASH.getFirstInSequence(other))) {
for (KeyValue keyValue : BucketsStrategy.iterableKeyValues(Layouts.HASH.getFirstInSequence(other))) {
setNode.executeSet(frame, merged, keyValue.getKey(), keyValue.getValue(), isCompareByIdentity);
}

@@ -1101,7 +1100,7 @@ public DynamicObject mergePackedBuckets(VirtualFrame frame, DynamicObject hash,
}
}

for (Map.Entry<Object, Object> keyValue : BucketsStrategy.iterableKeyValues(Layouts.HASH.getFirstInSequence(other))) {
for (KeyValue keyValue : BucketsStrategy.iterableKeyValues(Layouts.HASH.getFirstInSequence(other))) {
setNode.executeSet(frame, merged, keyValue.getKey(), keyValue.getValue(), isCompareByIdentity);
}

@@ -1122,7 +1121,7 @@ public DynamicObject mergeBucketsPacked(VirtualFrame frame, DynamicObject hash,

final DynamicObject merged = allocateObjectNode.allocateHash(Layouts.BASIC_OBJECT.getLogicalClass(hash), new Entry[BucketsStrategy.capacityGreaterThan(Layouts.HASH.getSize(hash) + Layouts.HASH.getSize(other))], 0, null, null, null, null, false);

for (Map.Entry<Object, Object> keyValue : BucketsStrategy.iterableKeyValues(Layouts.HASH.getFirstInSequence(hash))) {
for (KeyValue keyValue : BucketsStrategy.iterableKeyValues(Layouts.HASH.getFirstInSequence(hash))) {
setNode.executeSet(frame, merged, keyValue.getKey(), keyValue.getValue(), isCompareByIdentity);
}

@@ -1151,7 +1150,7 @@ public DynamicObject merge(VirtualFrame frame, DynamicObject hash, DynamicObject

int size = 0;

for (Map.Entry<Object, Object> keyValue : HashOperations.iterableKeyValues(hash)) {
for (KeyValue keyValue : HashOperations.iterableKeyValues(hash)) {
setNode.executeSet(frame, merged, keyValue.getKey(), keyValue.getValue(), false);
size++;
}
@@ -1161,7 +1160,7 @@ public DynamicObject merge(VirtualFrame frame, DynamicObject hash, DynamicObject
lookupEntryNode = insert(new LookupEntryNode(getContext(), getSourceSection()));
}

for (Map.Entry<Object, Object> keyValue : HashOperations.iterableKeyValues(other)) {
for (KeyValue keyValue : HashOperations.iterableKeyValues(other)) {
final HashLookupResult searchResult = lookupEntryNode.lookup(frame, merged, keyValue.getKey());

if (searchResult.getEntry() == null) {
Original file line number Diff line number Diff line change
@@ -9,16 +9,14 @@
*/
package org.jruby.truffle.core.hash;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.object.DynamicObject;
import org.jruby.truffle.Layouts;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.language.RubyGuards;
import org.jruby.truffle.util.StringUtils;

import java.util.Collections;
import java.util.Iterator;
import java.util.Map;

public abstract class HashOperations {

@@ -103,8 +101,8 @@ public static boolean verifyStore(RubyContext context, Object store, int size, E
return true;
}

@CompilerDirectives.TruffleBoundary
public static Iterator<Map.Entry<Object, Object>> iterateKeyValues(DynamicObject hash) {
@TruffleBoundary
public static Iterator<KeyValue> iterateKeyValues(DynamicObject hash) {
assert RubyGuards.isRubyHash(hash);

if (HashGuards.isNullHash(hash)) {
@@ -118,14 +116,14 @@ public static Iterator<Map.Entry<Object, Object>> iterateKeyValues(DynamicObject
}
}

@CompilerDirectives.TruffleBoundary
public static Iterable<Map.Entry<Object, Object>> iterableKeyValues(final DynamicObject hash) {
@TruffleBoundary
public static Iterable<KeyValue> iterableKeyValues(final DynamicObject hash) {
assert RubyGuards.isRubyHash(hash);

return new Iterable<Map.Entry<Object, Object>>() {
return new Iterable<KeyValue>() {

@Override
public Iterator<Map.Entry<Object, Object>> iterator() {
public Iterator<KeyValue> iterator() {
return iterateKeyValues(hash);
}

39 changes: 39 additions & 0 deletions truffle/src/main/java/org/jruby/truffle/core/hash/KeyValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2014, 2016 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.truffle.core.hash;

import java.util.Map;

public final class KeyValue implements Map.Entry<Object, Object> {

private Object key;
private Object value;

public KeyValue(Object key, Object value) {
this.key = key;
this.value = value;
}

@Override
public Object getKey() {
return key;
}

@Override
public Object getValue() {
return value;
}

@Override
public Object setValue(Object value) {
throw new UnsupportedOperationException();
}

}
Original file line number Diff line number Diff line change
@@ -15,7 +15,6 @@
import org.jruby.truffle.RubyContext;

import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;

public abstract class PackedArrayStrategy {
@@ -128,8 +127,8 @@ public static void promoteToBuckets(RubyContext context, DynamicObject hash, Obj
}

@TruffleBoundary
public static Iterator<Map.Entry<Object, Object>> iterateKeyValues(final Object[] store, final int size) {
return new Iterator<Map.Entry<Object, Object>>() {
public static Iterator<KeyValue> iterateKeyValues(final Object[] store, final int size) {
return new Iterator<KeyValue>() {

private int index = 0;

@@ -139,31 +138,16 @@ public boolean hasNext() {
}

@Override
public Map.Entry<Object, Object> next() {
public KeyValue next() {
if (!hasNext()) {
throw new NoSuchElementException();
}

final int finalIndex = index;

final Map.Entry<Object, Object> entryResult = new Map.Entry<Object, Object>() {

@Override
public Object getKey() {
return PackedArrayStrategy.getKey(store, finalIndex);
}

@Override
public Object getValue() {
return PackedArrayStrategy.getValue(store, finalIndex);
}

@Override
public Object setValue(Object value) {
throw new UnsupportedOperationException();
}

};
final KeyValue entryResult = new KeyValue(
PackedArrayStrategy.getKey(store, finalIndex),
PackedArrayStrategy.getValue(store, finalIndex));

index++;

Original file line number Diff line number Diff line change
@@ -68,6 +68,7 @@
import org.jruby.truffle.core.format.exceptions.FormatException;
import org.jruby.truffle.core.format.exceptions.InvalidFormatException;
import org.jruby.truffle.core.format.printf.PrintfCompiler;
import org.jruby.truffle.core.hash.KeyValue;
import org.jruby.truffle.core.hash.HashOperations;
import org.jruby.truffle.core.kernel.KernelNodesFactory.CopyNodeFactory;
import org.jruby.truffle.core.kernel.KernelNodesFactory.SameOrEqualNodeFactory;
@@ -134,7 +135,6 @@
import org.jruby.truffle.language.threadlocal.ThreadLocalObject;
import org.jruby.truffle.platform.UnsafeGroup;
import org.jruby.truffle.util.StringUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
@@ -144,7 +144,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@CoreClass("Kernel")
public abstract class KernelNodes {
@@ -174,7 +173,7 @@ private DynamicObject spawnAndCaptureOutput(DynamicObject command, final Dynamic
final List<String> envp = new ArrayList<>();

// TODO(CS): cast
for (Map.Entry<Object, Object> keyValue : HashOperations.iterableKeyValues(envAsHash)) {
for (KeyValue keyValue : HashOperations.iterableKeyValues(envAsHash)) {
envp.add(keyValue.getKey().toString() + "=" + keyValue.getValue().toString());
}

@@ -731,7 +730,7 @@ private void exec(RubyContext context, DynamicObject envAsHash, String[] command
final ProcessBuilder builder = new ProcessBuilder(commandLine);
builder.inheritIO();

for (Map.Entry<Object, Object> keyValue : HashOperations.iterableKeyValues(envAsHash)) {
for (KeyValue keyValue : HashOperations.iterableKeyValues(envAsHash)) {
builder.environment().put(keyValue.getKey().toString(), keyValue.getValue().toString());
}

Original file line number Diff line number Diff line change
@@ -15,13 +15,13 @@
import com.oracle.truffle.api.profiles.BranchProfile;
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.core.hash.KeyValue;
import org.jruby.truffle.core.hash.HashOperations;
import org.jruby.truffle.language.PerformanceWarnings;
import org.jruby.truffle.language.RubyGuards;
import org.jruby.truffle.language.RubyNode;
import org.jruby.truffle.language.control.RaiseException;
import org.jruby.truffle.language.methods.Arity;
import java.util.Map;

public class CheckKeywordArityNode extends RubyNode {

@@ -72,7 +72,7 @@ private void checkArityKeywordArguments(Object keywordArguments, int given) {

final DynamicObject keywordHash = (DynamicObject) keywordArguments;

for (Map.Entry<Object, Object> keyValue : HashOperations.iterableKeyValues(keywordHash)) {
for (KeyValue keyValue : HashOperations.iterableKeyValues(keywordHash)) {
if (arity.hasKeywordsRest()) {
if (RubyGuards.isRubySymbol(keyValue.getKey())) {
continue;
Original file line number Diff line number Diff line change
@@ -15,12 +15,11 @@
import com.oracle.truffle.api.profiles.ConditionProfile;
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.core.hash.KeyValue;
import org.jruby.truffle.core.hash.HashOperations;
import org.jruby.truffle.language.RubyGuards;
import org.jruby.truffle.language.RubyNode;

import java.util.Map;

public class ReadKeywordArgumentNode extends RubyNode {

private final String name;
@@ -58,7 +57,7 @@ private Object lookupKeywordInHash(DynamicObject hash) {

assert RubyGuards.isRubyHash(hash);

for (Map.Entry<Object, Object> keyValue : HashOperations.iterableKeyValues(hash)) {
for (KeyValue keyValue : HashOperations.iterableKeyValues(hash)) {
if (RubyGuards.isRubySymbol(keyValue.getKey()) && keyValue.getKey().toString().equals(name)) {
return keyValue.getValue();
}
Original file line number Diff line number Diff line change
@@ -17,13 +17,13 @@
import org.jruby.truffle.Layouts;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.core.hash.BucketsStrategy;
import org.jruby.truffle.core.hash.KeyValue;
import org.jruby.truffle.core.hash.HashOperations;
import org.jruby.truffle.language.PerformanceWarnings;
import org.jruby.truffle.language.RubyGuards;
import org.jruby.truffle.language.RubyNode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ReadKeywordRestArgumentNode extends RubyNode {

@@ -61,9 +61,9 @@ private Object extractKeywordHash(final Object hash) {

final DynamicObject hashObject = (DynamicObject) hash;

final List<Map.Entry<Object, Object>> entries = new ArrayList<>();
final List<KeyValue> entries = new ArrayList<>();

outer: for (Map.Entry<Object, Object> keyValue : HashOperations.iterableKeyValues(hashObject)) {
outer: for (KeyValue keyValue : HashOperations.iterableKeyValues(hashObject)) {
if (!RubyGuards.isRubySymbol(keyValue.getKey())) {
continue;
}

0 comments on commit e350be1

Please sign in to comment.