@@ -150,6 +150,45 @@ static void WriteBack<T>(object? obj, TypeDescription field, bool writeBack) whe
150150 field . Accessor . SetValue ( obj ! , Scratch < T > . Output ! [ 0 ] ) ;
151151 }
152152
153+ static readonly List < string > PooledStrings = [ ] ;
154+ static readonly Dictionary < string , nint > PooledStringIds = [ ] ;
155+
156+ static nint AllocPooledString ( string ? value ) {
157+ if ( value == null )
158+ return 0 ;
159+
160+ if ( ! PooledStringIds . TryGetValue ( value , out nint id ) ) {
161+ PooledStrings . Add ( value ) ;
162+ id = PooledStrings . Count ;
163+ PooledStringIds . Add ( value , id ) ;
164+ }
165+ return id ;
166+ }
167+
168+ static string ? GetString ( bool isObject , object ? obj , Span < byte > frame , TypeDescription field ) {
169+ if ( ! isObject ) {
170+ nint id = MemoryMarshal . Read < nint > ( frame [ ( int ) field . PackedOffset .. ] ) ;
171+ return id == 0 ? null : PooledStrings [ ( int ) id - 1 ] ;
172+ }
173+
174+ if ( field . Accessor . TryGetSpan ( obj ! , 1 , out Span < string ? > span ) )
175+ return span [ 0 ] ;
176+
177+ return field . Accessor . GetValue < string ? > ( obj ! ) ;
178+ }
179+
180+ static void SetString ( bool isObject , object ? obj , Span < byte > frame , TypeDescription field , string ? value ) {
181+ if ( ! isObject ) {
182+ MemoryMarshal . Write ( frame [ ( int ) field . PackedOffset .. ] , AllocPooledString ( value ) ) ;
183+ return ;
184+ }
185+
186+ if ( field . Accessor . TryGetSpan ( obj ! , 1 , out Span < string ? > span ) )
187+ span [ 0 ] = value ;
188+ else
189+ field . Accessor . SetValue ( obj ! , value ) ;
190+ }
191+
153192 static readonly Dictionary < Type , PredictionCopyFieldsFn > GeneratedCopyFields = [ ] ;
154193
155194 public static void RegisterCopyFields ( Type dataClassType , PredictionCopyFieldsFn fn ) => GeneratedCopyFields [ dataClassType ] = fn ;
@@ -269,11 +308,12 @@ public static void CopyField(ref PredictionCopy self, int chainCount, DataMap ro
269308 }
270309 break ;
271310 case FieldType . String : {
272- Span < char > output = GetOutput < char > ( destIsObject , dest , destFrame , field , fieldSize , out writeBack ) ;
273- ReadOnlySpan < char > input = GetInput < char > ( srcIsObject , src , srcFrame , field , fieldSize ) ;
311+ string ? previous = GetString ( destIsObject , dest , destFrame , field ) , output = previous ;
312+ string ? input = GetString ( srcIsObject , src , srcFrame , field ) ;
274313 difftype = self . CompareString ( output , input ) ;
275- self . CopyString ( difftype , output , input ) ;
276- WriteBack < char > ( dest , field , writeBack ) ;
314+ self . CopyString ( difftype , ref output , input ) ;
315+ if ( ! ReferenceEquals ( output , previous ) )
316+ SetString ( destIsObject , dest , destFrame , field , output ) ;
277317 if ( self . ErrorCheck && self . ShouldDescribe ) self . DescribeString ( difftype , output , input ) ;
278318 if ( shouldWatch ) self . WatchString ( difftype , output , input ) ;
279319 }
0 commit comments