1
1
/*
2
- * Copyright (c) 2021, 2022 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2021, 2023 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* The Universal Permissive License (UPL), Version 1.0
40
40
*/
41
41
package com .oracle .graal .python .builtins .modules .io ;
42
42
43
- import static com .oracle .graal .python .nodes .StringLiterals .T_EMPTY_STRING ;
44
43
import static com .oracle .graal .python .util .PythonUtils .TS_ENCODING ;
45
44
45
+ import com .oracle .truffle .api .dsl .Cached ;
46
+ import com .oracle .truffle .api .dsl .GenerateCached ;
47
+ import com .oracle .truffle .api .dsl .GenerateInline ;
48
+ import com .oracle .truffle .api .dsl .Specialization ;
49
+ import com .oracle .truffle .api .nodes .Node ;
46
50
import com .oracle .truffle .api .object .Shape ;
47
51
import com .oracle .truffle .api .strings .TruffleString ;
48
52
import com .oracle .truffle .api .strings .TruffleStringBuilder ;
@@ -58,24 +62,28 @@ public final class PStringIO extends PTextIOBase {
58
62
59
63
private boolean closed ;
60
64
61
- private TruffleString buf ;
65
+ private TruffleString cachedString ;
66
+ private TruffleStringBuilder buf ;
62
67
private TruffleStringBuilder sb ;
63
68
private int pos ;
64
69
private int stringSize ;
65
70
66
71
public PStringIO (Object cls , Shape instanceShape ) {
67
72
super (cls , instanceShape );
68
- buf = T_EMPTY_STRING ;
73
+ buf = null ;
69
74
}
70
75
71
- public TruffleString getBuf () {
72
- assert !isAccumulating ();
76
+ public TruffleStringBuilder getBuf () {
73
77
return buf ;
74
78
}
75
79
76
- public void setBuf (TruffleString buf ) {
77
- assert !isAccumulating ();
80
+ public void setBuf (TruffleStringBuilder buf ) {
78
81
this .buf = buf ;
82
+ cachedString = null ;
83
+ }
84
+
85
+ public void invalidateBufCache () {
86
+ cachedString = null ;
79
87
}
80
88
81
89
public int getPos () {
@@ -106,11 +114,12 @@ public boolean isClosed() {
106
114
return closed ;
107
115
}
108
116
109
- public void realize (TruffleStringBuilder . ToStringNode toStringNode ) {
117
+ public void realize () {
110
118
if (!isAccumulating ()) {
111
119
return ;
112
120
}
113
- buf = toStringNode .execute (sb );
121
+ buf = sb ;
122
+ cachedString = null ;
114
123
sb = null ;
115
124
}
116
125
@@ -121,6 +130,7 @@ public boolean isAccumulating() {
121
130
public void setAccumulating () {
122
131
assert stringSize == 0 && !isAccumulating ();
123
132
sb = TruffleStringBuilder .create (TS_ENCODING );
133
+ cachedString = null ;
124
134
}
125
135
126
136
public void append (TruffleString str , TruffleStringBuilder .AppendStringNode appendStringNode ) {
@@ -130,7 +140,7 @@ public void append(TruffleString str, TruffleStringBuilder.AppendStringNode appe
130
140
131
141
public void setRealized () {
132
142
sb = null ;
133
- buf = T_EMPTY_STRING ;
143
+ buf = TruffleStringBuilder . create ( TS_ENCODING ) ;
134
144
}
135
145
136
146
public TruffleString makeIntermediate (TruffleStringBuilder .ToStringNode toStringNode ) {
@@ -141,8 +151,31 @@ public TruffleString makeIntermediate(TruffleStringBuilder.ToStringNode toString
141
151
@ Override
142
152
public void clearAll () {
143
153
super .clearAll ();
144
- buf = T_EMPTY_STRING ;
154
+ buf = TruffleStringBuilder . create ( TS_ENCODING ) ;
145
155
sb = null ;
156
+ cachedString = null ;
146
157
setWriteNewline (null );
147
158
}
159
+
160
+ @ GenerateInline
161
+ @ GenerateCached (false )
162
+ abstract static class PStringIOBufToStringNode extends Node {
163
+ abstract TruffleString execute (Node inliningTarget , PStringIO self );
164
+
165
+ static boolean hasCache (PStringIO s ) {
166
+ return s .cachedString != null ;
167
+ }
168
+
169
+ @ Specialization (guards = "hasCache(self)" )
170
+ TruffleString doCached (PStringIO self ) {
171
+ return self .cachedString ;
172
+ }
173
+
174
+ @ Specialization (guards = "!hasCache(self)" )
175
+ TruffleString doUncached (PStringIO self ,
176
+ @ Cached (inline = false ) TruffleStringBuilder .ToStringNode toStringNode ) {
177
+ self .cachedString = toStringNode .execute (self .getBuf ());
178
+ return self .cachedString ;
179
+ }
180
+ }
148
181
}
0 commit comments