comparison graal/com.oracle.truffle.object/src/com/oracle/truffle/object/ShapeImpl.java @ 21923:1b695a36c4d5

Truffle: remove cached property array
author Andreas Woess <andreas.woess@oracle.com>
date Thu, 11 Jun 2015 17:10:27 +0200
parents e9b787f8134f
children c0744b24b230
comparison
equal deleted inserted replaced
21922:2ee70257c9c4 21923:1b695a36c4d5
78 protected final int primitiveArrayCapacity; 78 protected final int primitiveArrayCapacity;
79 protected final boolean hasPrimitiveArray; 79 protected final boolean hasPrimitiveArray;
80 80
81 protected final int depth; 81 protected final int depth;
82 protected final int propertyCount; 82 protected final int propertyCount;
83 protected Property[] propertyArray;
84 83
85 protected final Assumption validAssumption; 84 protected final Assumption validAssumption;
86 @CompilationFinal protected volatile Assumption leafAssumption; 85 @CompilationFinal protected volatile Assumption leafAssumption;
87 86
88 /** 87 /**
119 this.primitiveArrayCapacity = capacityFromSize(primitiveArraySize); 118 this.primitiveArrayCapacity = capacityFromSize(primitiveArraySize);
120 this.hasPrimitiveArray = hasPrimitiveArray; 119 this.hasPrimitiveArray = hasPrimitiveArray;
121 120
122 if (parent != null) { 121 if (parent != null) {
123 this.propertyCount = makePropertyCount(parent, propertyMap); 122 this.propertyCount = makePropertyCount(parent, propertyMap);
124 this.propertyArray = makePropertiesList(parent, propertyMap);
125 this.depth = parent.depth + 1; 123 this.depth = parent.depth + 1;
126 } else { 124 } else {
127 this.propertyCount = 0; 125 this.propertyCount = 0;
128 this.propertyArray = null;
129 this.depth = 0; 126 this.depth = 0;
130 } 127 }
131 128
132 this.validAssumption = createValidAssumption(); 129 this.validAssumption = createValidAssumption();
133 130
152 this(layout, null, operations, sharedData, PropertyMap.empty(), null, layout.createAllocator(), id); 149 this(layout, null, operations, sharedData, PropertyMap.empty(), null, layout.createAllocator(), id);
153 } 150 }
154 151
155 private static int makePropertyCount(ShapeImpl parent, PropertyMap propertyMap) { 152 private static int makePropertyCount(ShapeImpl parent, PropertyMap propertyMap) {
156 return parent.propertyCount + ((propertyMap.size() > parent.propertyMap.size() && !propertyMap.getLastProperty().isHidden() && !propertyMap.getLastProperty().isShadow()) ? 1 : 0); 153 return parent.propertyCount + ((propertyMap.size() > parent.propertyMap.size() && !propertyMap.getLastProperty().isHidden() && !propertyMap.getLastProperty().isShadow()) ? 1 : 0);
157 }
158
159 private static Property[] makePropertiesList(ShapeImpl parent, PropertyMap propertyMap) {
160 Property[] properties = parent.propertyArray;
161 if (properties != null && propertyMap.size() != parent.propertyMap.size()) {
162 Property lastProperty = propertyMap.getLastProperty();
163 if (lastProperty != null && !lastProperty.isHidden()) {
164 propertyListAllocCount.inc();
165 if (!lastProperty.isShadow()) {
166 properties = Arrays.copyOf(properties, properties.length + 1);
167 properties[properties.length - 1] = lastProperty;
168 } else {
169 properties = Arrays.copyOf(properties, properties.length);
170 for (int i = 0; i < properties.length; i++) {
171 if (properties[i].isSame(lastProperty)) {
172 properties[i] = lastProperty;
173 }
174 }
175 }
176 } else {
177 propertyListShareCount.inc();
178 }
179 }
180 return properties;
181 } 154 }
182 155
183 @Override 156 @Override
184 public final Property getLastProperty() { 157 public final Property getLastProperty() {
185 return propertyMap.getLastProperty(); 158 return propertyMap.getLastProperty();
878 return this; 851 return this;
879 } 852 }
880 853
881 @Override 854 @Override
882 public final Iterable<Property> getProperties() { 855 public final Iterable<Property> getProperties() {
883 if (getPropertyCount() != 0 && propertyArray == null) { 856 return getPropertyList();
884 CompilerDirectives.transferToInterpreter();
885 propertyArray = createPropertiesArray();
886 }
887 return new Iterable<Property>() {
888 public Iterator<Property> iterator() {
889 return new Iterator<Property>() {
890 private int cursor;
891
892 public boolean hasNext() {
893 return cursor < getPropertyCount();
894 }
895
896 public Property next() {
897 if (hasNext()) {
898 return propertyArray[cursor++];
899 }
900 throw new NoSuchElementException();
901 }
902
903 public void remove() {
904 throw new UnsupportedOperationException();
905 }
906 };
907 }
908 };
909 }
910
911 private Property[] createPropertiesArray() {
912 propertyListAllocCount.inc();
913 Property[] propertiesArray = new Property[getPropertyCount()];
914 List<Property> ownProperties = getPropertyList();
915 assert ownProperties.size() == getPropertyCount();
916 for (int i = 0; i < getPropertyCount(); i++) {
917 propertiesArray[i] = ownProperties.get(i);
918 }
919 return propertiesArray;
920 } 857 }
921 858
922 @Override 859 @Override
923 public final DynamicObject newInstance() { 860 public final DynamicObject newInstance() {
924 return layout.newInstance(this); 861 return layout.newInstance(this);
1120 private static final DebugCounter shapeCount = DebugCounter.create("Shapes allocated total"); 1057 private static final DebugCounter shapeCount = DebugCounter.create("Shapes allocated total");
1121 private static final DebugCounter shapeCloneCount = DebugCounter.create("Shapes allocated cloned"); 1058 private static final DebugCounter shapeCloneCount = DebugCounter.create("Shapes allocated cloned");
1122 private static final DebugCounter shapeCacheHitCount = DebugCounter.create("Shape cache hits"); 1059 private static final DebugCounter shapeCacheHitCount = DebugCounter.create("Shape cache hits");
1123 private static final DebugCounter shapeCacheMissCount = DebugCounter.create("Shape cache misses"); 1060 private static final DebugCounter shapeCacheMissCount = DebugCounter.create("Shape cache misses");
1124 1061
1125 protected static final DebugCounter propertyListAllocCount = DebugCounter.create("Property lists allocated");
1126 protected static final DebugCounter propertyListShareCount = DebugCounter.create("Property lists shared");
1127
1128 public ForeignAccess getForeignAccessFactory() { 1062 public ForeignAccess getForeignAccessFactory() {
1129 return getObjectType().getForeignAccessFactory(); 1063 return getObjectType().getForeignAccessFactory();
1130 } 1064 }
1131 } 1065 }