11/*
2- * Copyright (c) 2017, 2021 , Oracle and/or its affiliates.
2+ * Copyright (c) 2017, 2022 , Oracle and/or its affiliates.
33 * Copyright (c) 2013, Regents of the University of California
44 *
55 * All rights reserved.
3030import com .oracle .graal .python .builtins .objects .ints .PInt ;
3131import com .oracle .graal .python .nodes .ErrorMessages ;
3232import com .oracle .graal .python .nodes .interop .PForeignToPTypeNode ;
33- import com .oracle .graal .python .nodes .literal .ListLiteralNode ;
3433import com .oracle .graal .python .runtime .GilNode ;
3534import com .oracle .graal .python .runtime .exception .PException ;
3635import com .oracle .graal .python .runtime .sequence .PSequence ;
36+ import com .oracle .graal .python .runtime .sequence .storage .BasicSequenceStorage ;
3737import com .oracle .graal .python .runtime .sequence .storage .SequenceStorage ;
3838import com .oracle .graal .python .util .OverflowException ;
3939import com .oracle .truffle .api .CompilerAsserts ;
5151
5252@ ExportLibrary (InteropLibrary .class )
5353public final class PList extends PSequence {
54- private final ListLiteralNode origin ;
54+ private final ListOrigin origin ;
5555 private SequenceStorage store ;
5656
5757 public PList (Object cls , Shape instanceShape , SequenceStorage store ) {
@@ -60,7 +60,7 @@ public PList(Object cls, Shape instanceShape, SequenceStorage store) {
6060 this .store = store ;
6161 }
6262
63- public PList (Object cls , Shape instanceShape , SequenceStorage store , ListLiteralNode origin ) {
63+ public PList (Object cls , Shape instanceShape , SequenceStorage store , ListOrigin origin ) {
6464 super (cls , instanceShape );
6565 this .origin = origin ;
6666 this .store = store ;
@@ -117,15 +117,15 @@ public final int hashCode() {
117117 return super .hashCode ();
118118 }
119119
120- public ListLiteralNode getOrigin () {
120+ public ListOrigin getOrigin () {
121121 return origin ;
122122 }
123123
124124 @ ExportMessage
125125 public SourceSection getSourceLocation (@ Exclusive @ Cached GilNode gil ) throws UnsupportedMessageException {
126126 boolean mustRelease = gil .acquire ();
127127 try {
128- ListLiteralNode node = getOrigin ();
128+ ListOrigin node = getOrigin ();
129129 SourceSection result = null ;
130130 if (node != null ) {
131131 result = node .getSourceSection ();
@@ -229,4 +229,38 @@ public void removeArrayElement(long index,
229229 gil .release (mustRelease );
230230 }
231231 }
232+
233+ public interface ListOrigin {
234+
235+ /**
236+ * This class serves the purpose of updating the size estimate for the lists constructed
237+ * here over time. The estimate is updated slowly, it takes {@link #NUM_DIGITS_POW2} lists
238+ * to reach a size one larger than the current estimate to increase the estimate for new
239+ * lists.
240+ */
241+ @ CompilerDirectives .ValueType
242+ public static final class SizeEstimate {
243+ private static final int NUM_DIGITS = 3 ;
244+ private static final int NUM_DIGITS_POW2 = 1 << NUM_DIGITS ;
245+
246+ @ CompilerDirectives .CompilationFinal private int shiftedStorageSizeEstimate ;
247+
248+ public SizeEstimate (int storageSizeEstimate ) {
249+ shiftedStorageSizeEstimate = storageSizeEstimate * NUM_DIGITS_POW2 ;
250+ }
251+
252+ public int estimate () {
253+ return shiftedStorageSizeEstimate >> NUM_DIGITS ;
254+ }
255+
256+ public int updateFrom (int newSizeEstimate ) {
257+ shiftedStorageSizeEstimate = shiftedStorageSizeEstimate + newSizeEstimate - estimate ();
258+ return shiftedStorageSizeEstimate ;
259+ }
260+ }
261+
262+ void reportUpdatedCapacity (BasicSequenceStorage newStore );
263+
264+ SourceSection getSourceSection ();
265+ }
232266}
0 commit comments