Skip to content

Commit 61f9df8

Browse files
Improve performance of reading zip comments (#119457)
* use arraypooling to improve performance * apply suggestions * improve speed for getting comments bytes * add debug assert to prevent future misuse
1 parent 9d0a853 commit 61f9df8

File tree

1 file changed

+11
-18
lines changed
  • src/libraries/System.IO.Compression/src/System/IO/Compression

1 file changed

+11
-18
lines changed

src/libraries/System.IO.Compression/src/System/IO/Compression/ZipHelper.cs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ private static int SeekBackwardsAndRead(Stream stream, Span<byte> buffer, int ov
189189
// number of characters whose bytes do not add up to more than that number.
190190
internal static byte[] GetEncodedTruncatedBytesFromString(string? text, Encoding? encoding, int maxBytes, out bool isUTF8)
191191
{
192+
Debug.Assert(maxBytes >= 0, "maxBytes must be non-negative");
193+
192194
if (string.IsNullOrEmpty(text))
193195
{
194196
isUTF8 = false;
@@ -198,35 +200,26 @@ internal static byte[] GetEncodedTruncatedBytesFromString(string? text, Encoding
198200
encoding ??= GetEncoding(text);
199201
isUTF8 = encoding.CodePage == 65001;
200202

201-
if (maxBytes == 0) // No truncation
203+
if (maxBytes == 0)
202204
{
203205
return encoding.GetBytes(text);
204206
}
205207

206-
byte[] bytes;
207-
if (isUTF8 && encoding.GetMaxByteCount(text.Length) > maxBytes)
208+
byte[] bytes = encoding.GetBytes(text);
209+
210+
if (maxBytes < bytes.Length)
208211
{
209-
int totalCodePoints = 0;
210-
foreach (Rune rune in text.EnumerateRunes())
212+
if (isUTF8)
211213
{
212-
if (totalCodePoints + rune.Utf8SequenceLength > maxBytes)
214+
while ((bytes[maxBytes] & 0xC0) == 0x80)
213215
{
214-
break;
216+
maxBytes--;
215217
}
216-
totalCodePoints += rune.Utf8SequenceLength;
217218
}
218219

219-
bytes = encoding.GetBytes(text);
220-
221-
Debug.Assert(totalCodePoints > 0);
222-
Debug.Assert(totalCodePoints <= bytes.Length);
223-
224-
return bytes[0..totalCodePoints];
220+
bytes = bytes[0..maxBytes];
225221
}
226222

227-
bytes = encoding.GetBytes(text);
228-
return maxBytes < bytes.Length ? bytes[0..maxBytes] : bytes;
223+
return bytes;
229224
}
230-
231-
232225
}

0 commit comments

Comments
 (0)