-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tweak CreateByModelNameAsync (#7015)
- Add a CancellationToken to CreateByModelNameAsync, allowing the download and parsing to be canceled. - Use ReadLineAsync(cancellationToken), which not only allows it to be canceled, but avoids ~100K task allocations - Fix Helpers.FromBase64String to support lines longer than 300 chars
- Loading branch information
1 parent
3282f44
commit 2c9f775
Showing
4 changed files
with
85 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,41 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Buffers.Text; | ||
using System.Diagnostics; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using System.Threading; | ||
using System.Net.Http; | ||
|
||
namespace Microsoft.ML.Tokenizers | ||
{ | ||
internal static class Helpers | ||
{ | ||
public static ValueTask<string?> ReadLineAsync(StreamReader reader, CancellationToken cancellationToken) => | ||
reader.ReadLineAsync(cancellationToken); | ||
|
||
public static Task<Stream> GetStreamAsync(HttpClient client, string url, CancellationToken cancellationToken) => | ||
client.GetStreamAsync(url, cancellationToken); | ||
|
||
public static byte[] FromBase64String(string base64String, int offset, int length) | ||
{ | ||
Span<byte> bytes = stackalloc byte[300]; | ||
if (!Convert.TryFromBase64Chars(base64String.AsSpan().Slice(offset, length), bytes, out int bytesWritten)) | ||
if (!Base64.IsValid(base64String.AsSpan(offset, length), out int decodedLength)) | ||
{ | ||
throw new System.FormatException($"Invalid base64 string '{base64String.Substring(offset, length)}'"); | ||
throw new FormatException($"Invalid base64 string '{base64String.Substring(offset, length)}'"); | ||
} | ||
return bytes.Slice(0, bytesWritten).ToArray(); | ||
|
||
byte[] bytes = new byte[decodedLength]; | ||
bool success = Convert.TryFromBase64Chars(base64String.AsSpan(offset, length), bytes, out int bytesWritten); | ||
Debug.Assert(success); | ||
Debug.Assert(bytes.Length == bytesWritten); | ||
return bytes; | ||
} | ||
|
||
internal static bool TryParseInt32(string s, int offset, out int result) | ||
=> int.TryParse(s.AsSpan().Slice(offset), NumberStyles.None, CultureInfo.InvariantCulture, out result); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters