Skip to content

Improve how byte arrays are created in ZipHelper.GetEncodedTruncatedBytesFromString #64767

Closed

Description

@carlossanlop

Follow up of PR #59442 (comment)

The byte array allocations can be potentially improved with ArrayPool.

This method is consumed by the Comment properties in ZipArchive and ZipArchiveEntry.

@jozkee @adamsitnik

Activity

  1. added
    enhancement
    on Feb 3, 2022
  2. ghost added
    untriaged
    on Feb 3, 2022
  3. removed
    untriaged
    on Feb 4, 2022
  4. added this to the 8.0.0 milestone on Jul 10, 2022
  5. added
    help wanted
    on Apr 25, 2023
  6. LeaFrock commented on Jun 25, 2023

    @LeaFrock
    Contributor

    Is this what you want? Maybe I can make a PR.

            internal static byte[] GetEncodedTruncatedBytesFromString(string? text, Encoding? encoding, int maxBytes, out bool isUTF8)
            {
                if (string.IsNullOrEmpty(text))
                {
                    isUTF8 = false;
                    return Array.Empty<byte>();
                }
    
                encoding ??= GetEncoding(text);
                isUTF8 = encoding.CodePage == 65001;
    
                if (maxBytes == 0) // No truncation
                {
                    return encoding.GetBytes(text);
                }
    
                var byteCount = encoding.GetByteCount(text);
                var buffer = ArrayPool<byte>.Shared.Rent(byteCount);
                try
                {
                    encoding.GetBytes(text, 0, text.Length, buffer, 0);
                }
                catch (Exception)
                {
                    ArrayPool<byte>.Shared.Return(buffer);
                    throw;
                }
    
                byte[] bytes;
                if (isUTF8)
                {
                    int totalCodePoints = 0;
                    foreach (Rune rune in text.EnumerateRunes())
                    {
                        if (totalCodePoints + rune.Utf8SequenceLength > maxBytes)
                        {
                            break;
                        }
                        totalCodePoints += rune.Utf8SequenceLength;
                    }
    
                    Debug.Assert(totalCodePoints > 0);
                    Debug.Assert(totalCodePoints <= byteCount);
    
                    bytes = buffer[0..totalCodePoints];
                    ArrayPool<byte>.Shared.Return(buffer);
                    return bytes;
                }
    
                bytes = buffer[0..Math.Min(maxBytes, byteCount)];
                ArrayPool<byte>.Shared.Return(buffer);
                return bytes;
            }
  7. modified the milestones: 8.0.0, Future on Jul 5, 2023
  8. carlossanlop commented on Jul 5, 2023

    @carlossanlop
    ContributorAuthor

    Yes, I think that's what was being asked in the PR, @LeaFrock, thank you so much. Yes, if you can send a PR, that would be awesome. I'll assign the issue to you. Tag @ViktorHofer and I when it's up so we can review it.

  9. ghost added
    in-pr
    on Jul 9, 2023
  10. ghost removed
    in-pr
    on Aug 9, 2023
  11. added
    in-pr
    on Sep 8, 2025
  12. modified the milestones: Future, 11.0.0 on Oct 13, 2025
  13. added
    tenet-performance
    on Oct 13, 2025
  14. locked and limited conversation to collaborators on Nov 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-System.IO.Compressionenhancementhelp wantedin-prtenet-performance

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions