You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "kronic (via GitHub)" <gi...@apache.org> on 2026/08/04 07:23:59 UTC
[PR] StringViewArray: enable nullable reference types and optimize encoding with stackalloc/ArrayPool [arrow-dotnet]
kronic opened a new pull request, #400:
URL: https://github.com/apache/arrow-dotnet/pull/400
StringViewArray: Enable nullable reference types and improve encoding performance
- Enable #nullable enable and update interfaces to use nullable string (IReadOnlyList<string?>)
- Change DefaultEncoding to UTF8Encoding(false) to explicitly disable BOM
- In Builder.Append, use GetMaxByteCount and stackalloc (for <=1024 bytes) or ArrayPool<byte>
to avoid per‑string byte array allocation and reduce GC pressure
- Add using System.Buffers for ArrayPool
- Update Append and AppendRange to accept nullable strings and optional encoding
- Update GetString to return nullable string and accept optional encoding
- Use primary constructor for StringViewArray and Builder
- Adjust overloads and parameter defaults accordingly
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] StringViewArray: enable nullable reference types and optimize encoding with stackalloc/ArrayPool [arrow-dotnet]
Posted by "kronic (via GitHub)" <gi...@apache.org>.
kronic commented on PR #400:
URL: https://github.com/apache/arrow-dotnet/pull/400#issuecomment-5187953304
In general, I think the idea of temporary buffers is redundant. You can write directly to the ArrowBuffer, which will eliminate unnecessary memory copying and temporary buffer allocation.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] StringViewArray: enable nullable reference types and optimize encoding with stackalloc/ArrayPool [arrow-dotnet]
Posted by "adamreeve (via GitHub)" <gi...@apache.org>.
adamreeve commented on code in PR #400:
URL: https://github.com/apache/arrow-dotnet/pull/400#discussion_r3725825467
##########
src/Apache.Arrow/Arrays/StringViewArray.cs:
##########
@@ -22,33 +25,56 @@
namespace Apache.Arrow
{
- public class StringViewArray : BinaryViewArray, IReadOnlyList<string>
+ public class StringViewArray(ArrayData data) : BinaryViewArray(ArrowTypeId.StringView, data), IReadOnlyList<string?>
{
- public static readonly Encoding DefaultEncoding = Encoding.UTF8;
+ public static Encoding DefaultEncoding { get; } = new UTF8Encoding(false);
Review Comment:
In that case, I wonder if we should also change the [default for `StringArray`](https://github.com/apache/arrow-dotnet/blob/dc2c56617f5612cfd77750ba79b21f0e9a2ec5e6/src/Apache.Arrow/Arrays/StringArray.cs#L27). That could be slightly more risky given it's been around longer and is likely to be more widely used, but it would make sense to fix both at the same time.
Do you have an opinion on this @CurtHagenlocher?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] StringViewArray: enable nullable reference types and optimize encoding with stackalloc/ArrayPool [arrow-dotnet]
Posted by "adamreeve (via GitHub)" <gi...@apache.org>.
adamreeve commented on code in PR #400:
URL: https://github.com/apache/arrow-dotnet/pull/400#discussion_r3717996776
##########
src/Apache.Arrow/Arrays/StringViewArray.cs:
##########
@@ -22,33 +25,56 @@
namespace Apache.Arrow
{
- public class StringViewArray : BinaryViewArray, IReadOnlyList<string>
+ public class StringViewArray(ArrayData data) : BinaryViewArray(ArrowTypeId.StringView, data), IReadOnlyList<string?>
{
- public static readonly Encoding DefaultEncoding = Encoding.UTF8;
+ public static Encoding DefaultEncoding { get; } = new UTF8Encoding(false);
- public new class Builder : BuilderBase<StringViewArray, Builder>
+ public new class Builder() : BuilderBase<StringViewArray, Builder>(StringViewType.Default)
{
- public Builder() : base(StringViewType.Default) { }
-
protected override StringViewArray Build(ArrayData data)
{
return new StringViewArray(data);
}
- public Builder Append(string value, Encoding encoding = null)
+ public Builder Append(string? value, Encoding? encoding = null)
{
- if (value == null)
+ if (value is null)
{
return AppendNull();
}
- encoding = encoding ?? DefaultEncoding;
- byte[] span = encoding.GetBytes(value);
- return Append(span.AsSpan());
+
+ encoding ??= DefaultEncoding;
+ int maxByteCount = encoding.GetMaxByteCount(value.Length);
+ #if NETCOREAPP
+ byte[]? buffer = null;
+
+ Span<byte> span = maxByteCount <= 1024
+ ? stackalloc byte[maxByteCount]
+ : buffer = ArrayPool<byte>.Shared.Rent(maxByteCount);
+
+ int encodeBbytes = encoding.GetBytes(value, span);
Review Comment:
`encoding.GetBytes` could throw, depending on the encoding used, so this should probably be within the `try` block.
##########
src/Apache.Arrow/Arrays/StringViewArray.cs:
##########
@@ -22,33 +25,56 @@
namespace Apache.Arrow
{
- public class StringViewArray : BinaryViewArray, IReadOnlyList<string>
+ public class StringViewArray(ArrayData data) : BinaryViewArray(ArrowTypeId.StringView, data), IReadOnlyList<string?>
{
- public static readonly Encoding DefaultEncoding = Encoding.UTF8;
+ public static Encoding DefaultEncoding { get; } = new UTF8Encoding(false);
Review Comment:
I'm a little concerned that this could be a breaking behaviour change, but I checked that decoding is forgiving of whether or not a BOM is present, regardless of the `encoderShouldEmitUTF8Identifier` parameter. So I think this is OK.
I notice that we disable testing the `StringView` type in our C Data Interface Python integration tests: https://github.com/apache/arrow-dotnet/blob/dc2c56617f5612cfd77750ba79b21f0e9a2ec5e6/test/Apache.Arrow.Tests/CDataInterfacePythonTests.cs#L772-L773
Did you find that this was causing a problem when integrating with another Arrow implementation?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] StringViewArray: enable nullable reference types and optimize encoding with stackalloc/ArrayPool [arrow-dotnet]
Posted by "CurtHagenlocher (via GitHub)" <gi...@apache.org>.
CurtHagenlocher commented on code in PR #400:
URL: https://github.com/apache/arrow-dotnet/pull/400#discussion_r3728436503
##########
src/Apache.Arrow/Arrays/StringViewArray.cs:
##########
@@ -22,33 +25,56 @@
namespace Apache.Arrow
{
- public class StringViewArray : BinaryViewArray, IReadOnlyList<string>
+ public class StringViewArray(ArrayData data) : BinaryViewArray(ArrowTypeId.StringView, data), IReadOnlyList<string?>
{
- public static readonly Encoding DefaultEncoding = Encoding.UTF8;
+ public static Encoding DefaultEncoding { get; } = new UTF8Encoding(false);
Review Comment:
To the best of my ability to tell, the BOM is only used in certain specific contexts such as `StreamWriter`. It is not referenced from `GetByteCount` or `GetBytes` and so should have no impact on the scenarios we care about.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] StringViewArray: enable nullable reference types and optimize encoding with stackalloc/ArrayPool [arrow-dotnet]
Posted by "adamreeve (via GitHub)" <gi...@apache.org>.
adamreeve commented on code in PR #400:
URL: https://github.com/apache/arrow-dotnet/pull/400#discussion_r3717967813
##########
src/Apache.Arrow/Arrays/StringViewArray.cs:
##########
@@ -22,33 +25,56 @@
namespace Apache.Arrow
{
- public class StringViewArray : BinaryViewArray, IReadOnlyList<string>
+ public class StringViewArray(ArrayData data) : BinaryViewArray(ArrowTypeId.StringView, data), IReadOnlyList<string?>
{
- public static readonly Encoding DefaultEncoding = Encoding.UTF8;
+ public static Encoding DefaultEncoding { get; } = new UTF8Encoding(false);
Review Comment:
I'm a little concerned that this could be a breaking behaviour change, but I checked that decoding is forgiving of whether or not a BOM is present, regardless of the `encoderShouldEmitUTF8Identifier` parameter. So I think this is OK.
I notice that we disable testing the `StringView` type in our C Data Interface Python integration tests: https://github.com/apache/arrow-dotnet/blob/dc2c56617f5612cfd77750ba79b21f0e9a2ec5e6/test/Apache.Arrow.Tests/CDataInterfacePythonTests.cs#L772-L773
Did you find that emitting the BOM was causing a problem when integrating with another Arrow implementation?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] StringViewArray: enable nullable reference types and optimize encoding with stackalloc/ArrayPool [arrow-dotnet]
Posted by "CurtHagenlocher (via GitHub)" <gi...@apache.org>.
CurtHagenlocher commented on code in PR #400:
URL: https://github.com/apache/arrow-dotnet/pull/400#discussion_r3720443822
##########
src/Apache.Arrow/Arrays/StringViewArray.cs:
##########
@@ -22,33 +25,63 @@
namespace Apache.Arrow
{
- public class StringViewArray : BinaryViewArray, IReadOnlyList<string>
+ public class StringViewArray(ArrayData data) : BinaryViewArray(ArrowTypeId.StringView, data), IReadOnlyList<string?>
{
- public static readonly Encoding DefaultEncoding = Encoding.UTF8;
+ public static Encoding DefaultEncoding { get; } = new UTF8Encoding(false);
- public new class Builder : BuilderBase<StringViewArray, Builder>
+ public new class Builder() : BuilderBase<StringViewArray, Builder>(StringViewType.Default)
{
- public Builder() : base(StringViewType.Default) { }
-
protected override StringViewArray Build(ArrayData data)
{
return new StringViewArray(data);
}
- public Builder Append(string value, Encoding encoding = null)
+ public Builder Append(string? value, Encoding? encoding = null)
{
- if (value == null)
+ if (value is null)
{
return AppendNull();
}
- encoding = encoding ?? DefaultEncoding;
- byte[] span = encoding.GetBytes(value);
- return Append(span.AsSpan());
+
+ encoding ??= DefaultEncoding;
+ int maxByteCount = encoding.GetMaxByteCount(value.Length);
+ #if NETCOREAPP
+ byte[]? buffer = null;
+
+ Span<byte> span = maxByteCount <= 1024
Review Comment:
I've been using 256 as a conservative upper bound for stackalloc in libraries; see `VariantValueWriter.StackAllocThreshold`.
##########
src/Apache.Arrow/Arrays/StringViewArray.cs:
##########
@@ -22,33 +25,63 @@
namespace Apache.Arrow
{
- public class StringViewArray : BinaryViewArray, IReadOnlyList<string>
+ public class StringViewArray(ArrayData data) : BinaryViewArray(ArrowTypeId.StringView, data), IReadOnlyList<string?>
Review Comment:
I think a codebase is easier to read when it's consistent in its use of syntax and personally found this new form jarring. I'm curious what other people think.
##########
src/Apache.Arrow/Arrays/StringViewArray.cs:
##########
@@ -22,33 +25,63 @@
namespace Apache.Arrow
{
- public class StringViewArray : BinaryViewArray, IReadOnlyList<string>
+ public class StringViewArray(ArrayData data) : BinaryViewArray(ArrowTypeId.StringView, data), IReadOnlyList<string?>
{
- public static readonly Encoding DefaultEncoding = Encoding.UTF8;
+ public static Encoding DefaultEncoding { get; } = new UTF8Encoding(false);
- public new class Builder : BuilderBase<StringViewArray, Builder>
+ public new class Builder() : BuilderBase<StringViewArray, Builder>(StringViewType.Default)
{
- public Builder() : base(StringViewType.Default) { }
-
protected override StringViewArray Build(ArrayData data)
{
return new StringViewArray(data);
}
- public Builder Append(string value, Encoding encoding = null)
+ public Builder Append(string? value, Encoding? encoding = null)
{
- if (value == null)
+ if (value is null)
{
return AppendNull();
}
- encoding = encoding ?? DefaultEncoding;
- byte[] span = encoding.GetBytes(value);
- return Append(span.AsSpan());
+
+ encoding ??= DefaultEncoding;
+ int maxByteCount = encoding.GetMaxByteCount(value.Length);
+ #if NETCOREAPP
Review Comment:
Please put preprocessor directives in flush-left like the rest of the codebase.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] StringViewArray: enable nullable reference types and optimize encoding with stackalloc/ArrayPool [arrow-dotnet]
Posted by "CurtHagenlocher (via GitHub)" <gi...@apache.org>.
CurtHagenlocher commented on PR #400:
URL: https://github.com/apache/arrow-dotnet/pull/400#issuecomment-5191711850
In general and for a variety of reasons I think the existing builders kind of suck. I've often been skipping them and building buffers directly for better performance. For your own use cases, do you find that you typically have a known set of .NET `string`s that you want to convert into an Arrow array or are you also building them more incrementally?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] StringViewArray: enable nullable reference types and optimize encoding with stackalloc/ArrayPool [arrow-dotnet]
Posted by "kronic (via GitHub)" <gi...@apache.org>.
kronic commented on code in PR #400:
URL: https://github.com/apache/arrow-dotnet/pull/400#discussion_r3718091586
##########
src/Apache.Arrow/Arrays/StringViewArray.cs:
##########
@@ -22,33 +25,56 @@
namespace Apache.Arrow
{
- public class StringViewArray : BinaryViewArray, IReadOnlyList<string>
+ public class StringViewArray(ArrayData data) : BinaryViewArray(ArrowTypeId.StringView, data), IReadOnlyList<string?>
{
- public static readonly Encoding DefaultEncoding = Encoding.UTF8;
+ public static Encoding DefaultEncoding { get; } = new UTF8Encoding(false);
Review Comment:
@adamreeve Yes, BOM causes problems with DataFusion.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] StringViewArray: enable nullable reference types and optimize encoding with stackalloc/ArrayPool [arrow-dotnet]
Posted by "CurtHagenlocher (via GitHub)" <gi...@apache.org>.
CurtHagenlocher commented on code in PR #400:
URL: https://github.com/apache/arrow-dotnet/pull/400#discussion_r3728468858
##########
src/Apache.Arrow/Arrays/StringViewArray.cs:
##########
@@ -22,33 +25,56 @@
namespace Apache.Arrow
{
- public class StringViewArray : BinaryViewArray, IReadOnlyList<string>
+ public class StringViewArray(ArrayData data) : BinaryViewArray(ArrowTypeId.StringView, data), IReadOnlyList<string?>
{
- public static readonly Encoding DefaultEncoding = Encoding.UTF8;
+ public static Encoding DefaultEncoding { get; } = new UTF8Encoding(false);
Review Comment:
That is, the following code prints `true`:
```
string test = "test";
byte[] one = new System.Text.UTF8Encoding(true).GetBytes(test);
byte[] two = new System.Text.UTF8Encoding(false).GetBytes(test);
System.Console.WriteLine(one.Length == two.Length);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] StringViewArray: enable nullable reference types and optimize encoding with stackalloc/ArrayPool [arrow-dotnet]
Posted by "adamreeve (via GitHub)" <gi...@apache.org>.
adamreeve commented on code in PR #400:
URL: https://github.com/apache/arrow-dotnet/pull/400#discussion_r3721833538
##########
src/Apache.Arrow/Arrays/StringViewArray.cs:
##########
@@ -22,33 +25,63 @@
namespace Apache.Arrow
{
- public class StringViewArray : BinaryViewArray, IReadOnlyList<string>
+ public class StringViewArray(ArrayData data) : BinaryViewArray(ArrowTypeId.StringView, data), IReadOnlyList<string?>
Review Comment:
I also found it a bit jarring but I haven't been doing as much work in C# lately so maybe I'm just not used to this. I noticed Rider always suggests refactoring to this form.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] StringViewArray: enable nullable reference types and optimize encoding with stackalloc/ArrayPool [arrow-dotnet]
Posted by "kronic (via GitHub)" <gi...@apache.org>.
kronic commented on code in PR #400:
URL: https://github.com/apache/arrow-dotnet/pull/400#discussion_r3718193852
##########
src/Apache.Arrow/Arrays/StringViewArray.cs:
##########
@@ -22,33 +25,56 @@
namespace Apache.Arrow
{
- public class StringViewArray : BinaryViewArray, IReadOnlyList<string>
+ public class StringViewArray(ArrayData data) : BinaryViewArray(ArrowTypeId.StringView, data), IReadOnlyList<string?>
{
- public static readonly Encoding DefaultEncoding = Encoding.UTF8;
+ public static Encoding DefaultEncoding { get; } = new UTF8Encoding(false);
- public new class Builder : BuilderBase<StringViewArray, Builder>
+ public new class Builder() : BuilderBase<StringViewArray, Builder>(StringViewType.Default)
{
- public Builder() : base(StringViewType.Default) { }
-
protected override StringViewArray Build(ArrayData data)
{
return new StringViewArray(data);
}
- public Builder Append(string value, Encoding encoding = null)
+ public Builder Append(string? value, Encoding? encoding = null)
{
- if (value == null)
+ if (value is null)
{
return AppendNull();
}
- encoding = encoding ?? DefaultEncoding;
- byte[] span = encoding.GetBytes(value);
- return Append(span.AsSpan());
+
+ encoding ??= DefaultEncoding;
+ int maxByteCount = encoding.GetMaxByteCount(value.Length);
+ #if NETCOREAPP
+ byte[]? buffer = null;
+
+ Span<byte> span = maxByteCount <= 1024
+ ? stackalloc byte[maxByteCount]
+ : buffer = ArrayPool<byte>.Shared.Rent(maxByteCount);
+
+ int encodeBbytes = encoding.GetBytes(value, span);
Review Comment:
done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] StringViewArray: enable nullable reference types and optimize encoding with stackalloc/ArrayPool [arrow-dotnet]
Posted by "adamreeve (via GitHub)" <gi...@apache.org>.
adamreeve commented on code in PR #400:
URL: https://github.com/apache/arrow-dotnet/pull/400#discussion_r3732035066
##########
src/Apache.Arrow/Arrays/StringViewArray.cs:
##########
@@ -22,33 +25,56 @@
namespace Apache.Arrow
{
- public class StringViewArray : BinaryViewArray, IReadOnlyList<string>
+ public class StringViewArray(ArrayData data) : BinaryViewArray(ArrowTypeId.StringView, data), IReadOnlyList<string?>
{
- public static readonly Encoding DefaultEncoding = Encoding.UTF8;
+ public static Encoding DefaultEncoding { get; } = new UTF8Encoding(false);
Review Comment:
OK, I'm confused about how this could have caused problems with DataFusion then. We only call `GetBytes` and `GetString` on this encoding. I guess that means this change is harmless, but I'd like to understand what motivated it then.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org