@@ -1706,7 +1706,7 @@ internal TdsOperationStatus TryReadStringWithEncoding(int length, System.Text.En
17061706 {
17071707 if ( isContinuing || isStarting )
17081708 {
1709- buf = ( byte [ ] ) TryTakeSnapshotStorage ( ) ;
1709+ buf = TryTakeSnapshotStorage ( ) as byte [ ] ;
17101710 Debug . Assert ( buf == null || buf . Length == length , "stored buffer length must be null or must have been created with the correct length" ) ;
17111711 }
17121712 if ( buf != null )
@@ -1850,19 +1850,25 @@ internal int ReadPlpBytesChunk(byte[] buff, int offset, int len)
18501850
18511851 internal TdsOperationStatus TryReadPlpBytes ( ref byte [ ] buff , int offset , int len , out int totalBytesRead )
18521852 {
1853- ( _ , bool isStarting , bool isContinuing ) = GetSnapshotStatuses ( ) ;
1854- return TryReadPlpBytes ( ref buff , offset , len , out totalBytesRead , isStarting || isContinuing ) ;
1853+ bool isStarting = false ;
1854+ bool isContinuing = false ;
1855+ bool compatibilityMode = LocalAppContextSwitches . UseCompatibilityAsyncBehaviour ;
1856+ if ( ! compatibilityMode )
1857+ {
1858+ ( _ , isStarting , isContinuing ) = GetSnapshotStatuses ( ) ;
1859+ }
1860+ return TryReadPlpBytes ( ref buff , offset , len , out totalBytesRead , isStarting || isContinuing , compatibilityMode ) ;
18551861 }
18561862 // Reads the requested number of bytes from a plp data stream, or the entire data if
18571863 // requested length is -1 or larger than the actual length of data. First call to this method
18581864 // should be preceeded by a call to ReadPlpLength or ReadDataLength.
18591865 // Returns the actual bytes read.
18601866 // NOTE: This method must be retriable WITHOUT replaying a snapshot
18611867 // Every time you call this method increment the offset and decrease len by the value of totalBytesRead
1862- internal TdsOperationStatus TryReadPlpBytes ( ref byte [ ] buff , int offset , int len , out int totalBytesRead , bool writeDataSizeToSnapshot )
1868+ internal TdsOperationStatus TryReadPlpBytes ( ref byte [ ] buff , int offset , int len , out int totalBytesRead , bool writeDataSizeToSnapshot , bool compatibilityMode )
18631869 {
18641870 totalBytesRead = 0 ;
1865-
1871+
18661872 if ( _longlen == 0 )
18671873 {
18681874 Debug . Assert ( _longlenleft == 0 ) ;
@@ -1888,17 +1894,19 @@ internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len
18881894 {
18891895 // if there is a snapshot and it contains a stored plp buffer take it
18901896 // and try to use it if it is the right length
1891- buff = ( byte [ ] ) TryTakeSnapshotStorage ( ) ;
1897+ buff = TryTakeSnapshotStorage ( ) as byte [ ] ;
18921898 if ( buff != null )
18931899 {
18941900 offset = _snapshot . GetPacketDataOffset ( ) ;
18951901 totalBytesRead = offset ;
18961902 }
18971903 }
1898- else if ( _snapshot != null )
1904+ else if ( compatibilityMode && _snapshot != null && _snapshotStatus != SnapshotStatus . NotActive )
18991905 {
19001906 // legacy replay path perf optimization
1901- buff = ( byte [ ] ) TryTakeSnapshotStorage ( ) ;
1907+ // if there is a snapshot and it contains a stored plp buffer take it
1908+ // and try to use it if it is the right length
1909+ buff = TryTakeSnapshotStorage ( ) as byte [ ] ;
19021910 }
19031911
19041912 if ( ( ulong ) ( buff ? . Length ?? 0 ) != _longlen )
@@ -1957,11 +1965,13 @@ internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len
19571965 // so it can be re-used when another packet arrives and we read again
19581966 SetSnapshotStorage ( buff ) ;
19591967 SetSnapshotDataSize ( bytesRead ) ;
1960-
1968+
19611969 }
1962- else if ( _snapshot != null )
1970+ else if ( compatibilityMode && _snapshot != null )
19631971 {
19641972 // legacy replay path perf optimization
1973+ // a partial read has happened so store the target buffer in the snapshot
1974+ // so it can be re-used when another packet arrives and we read again
19651975 SetSnapshotStorage ( buff ) ;
19661976 }
19671977 return result ;
@@ -1973,18 +1983,19 @@ internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len
19731983 result = TryReadPlpLength ( false , out _ ) ;
19741984 if ( result != TdsOperationStatus . Done )
19751985 {
1976- if ( result == TdsOperationStatus . NeedMoreData )
1986+ if ( writeDataSizeToSnapshot )
19771987 {
1978- if ( writeDataSizeToSnapshot )
1988+ if ( result == TdsOperationStatus . NeedMoreData )
19791989 {
19801990 SetSnapshotStorage ( buff ) ;
19811991 SetSnapshotDataSize ( bytesRead ) ;
19821992 }
1983- else if ( _snapshot != null )
1984- {
1985- // legacy replay path perf optimization
1986- SetSnapshotStorage ( buff ) ;
1987- }
1993+ }
1994+ else if ( compatibilityMode && _snapshot != null )
1995+ {
1996+ // a partial read has happened so store the target buffer in the snapshot
1997+ // so it can be re-used when another packet arrives and we read again
1998+ SetSnapshotStorage ( buff ) ;
19881999 }
19892000 return result ;
19902001 }
0 commit comments