This is an automated email from the ASF dual-hosted git repository.

lidavidm pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/main by this push:
     new 4fa6719528 GH-36816: [C#] Reduce allocations (#36817)
4fa6719528 is described below

commit 4fa671952895b39a08d8ea1140e96814175f2cc7
Author: Curt Hagenlocher <[email protected]>
AuthorDate: Fri Aug 18 11:55:30 2023 -0700

    GH-36816: [C#] Reduce allocations (#36817)
    
    ### What changes are included in this PR?
    
    A small refactoring to make a small improvement in memory utilization.
    
    ### Are these changes tested?
    
    Existing test coverage should be sufficient.
    
    * Closes: #36816
    
    Authored-by: Curt Hagenlocher <[email protected]>
    Signed-off-by: David Li <[email protected]>
---
 csharp/src/Apache.Arrow/Arrays/Time32Array.cs          |  2 +-
 csharp/src/Apache.Arrow/Arrays/Time64Array.cs          |  2 +-
 csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs |  4 ++--
 csharp/src/Apache.Arrow/C/CArrowSchemaImporter.cs      | 14 +++++++-------
 csharp/src/Apache.Arrow/Ipc/MessageSerializer.cs       |  6 +++---
 csharp/src/Apache.Arrow/Types/IntervalUnit.cs          | 11 ++++++++++-
 csharp/src/Apache.Arrow/Types/Time32Type.cs            |  2 +-
 csharp/src/Apache.Arrow/Types/Time64Type.cs            |  2 +-
 csharp/src/Apache.Arrow/Types/TimeType.cs              | 11 +++++++++++
 9 files changed, 37 insertions(+), 17 deletions(-)

diff --git a/csharp/src/Apache.Arrow/Arrays/Time32Array.cs 
b/csharp/src/Apache.Arrow/Arrays/Time32Array.cs
index 31d17d06a1..824694cd6d 100644
--- a/csharp/src/Apache.Arrow/Arrays/Time32Array.cs
+++ b/csharp/src/Apache.Arrow/Arrays/Time32Array.cs
@@ -46,7 +46,7 @@ namespace Apache.Arrow
                 : this(Time32Type.Default) { }
 
             public Builder(TimeUnit unit)
-                : this(new Time32Type(unit)) { }
+                : this((Time32Type)TimeType.FromTimeUnit(unit)) { }
 
             /// <summary>
             /// Construct a new instance of the <see cref="Builder"/> class.
diff --git a/csharp/src/Apache.Arrow/Arrays/Time64Array.cs 
b/csharp/src/Apache.Arrow/Arrays/Time64Array.cs
index 95faf18fe9..9fc2ae4be1 100644
--- a/csharp/src/Apache.Arrow/Arrays/Time64Array.cs
+++ b/csharp/src/Apache.Arrow/Arrays/Time64Array.cs
@@ -49,7 +49,7 @@ namespace Apache.Arrow
                 : this(Time64Type.Default) { }
 
             public Builder(TimeUnit unit)
-                : this(new Time64Type(unit)) { }
+                : this((Time64Type)TimeType.FromTimeUnit(unit)) { }
 
             /// <summary>
             /// Construct a new instance of the <see cref="Builder"/> class.
diff --git a/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs 
b/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs
index 0a0f1cc837..17b320f869 100644
--- a/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs
+++ b/csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs
@@ -61,9 +61,9 @@ namespace Apache.Arrow.C
             {
                 throw new ArgumentNullException(nameof(arrayStream));
             }
-            if (arrayStream == null)
+            if (cArrayStream == null)
             {
-                throw new ArgumentNullException(nameof(arrayStream));
+                throw new ArgumentNullException(nameof(cArrayStream));
             }
 
             cArrayStream->private_data = 
ExportedArrayStream.Export(arrayStream);
diff --git a/csharp/src/Apache.Arrow/C/CArrowSchemaImporter.cs 
b/csharp/src/Apache.Arrow/C/CArrowSchemaImporter.cs
index b21f24edba..ec613d59d2 100644
--- a/csharp/src/Apache.Arrow/C/CArrowSchemaImporter.cs
+++ b/csharp/src/Apache.Arrow/C/CArrowSchemaImporter.cs
@@ -268,14 +268,14 @@ namespace Apache.Arrow.C
                     // Date and time
                     "tdD" => Date32Type.Default,
                     "tdm" => Date64Type.Default,
-                    "tts" => new Time32Type(TimeUnit.Second),
-                    "ttm" => new Time32Type(TimeUnit.Millisecond),
-                    "ttu" => new Time64Type(TimeUnit.Microsecond),
-                    "ttn" => new Time64Type(TimeUnit.Nanosecond),
+                    "tts" => TimeType.Second,
+                    "ttm" => TimeType.Millisecond,
+                    "ttu" => TimeType.Microsecond,
+                    "ttn" => TimeType.Nanosecond,
                     // TODO: duration not yet implemented
-                    "tiM" => new IntervalType(IntervalUnit.YearMonth),
-                    "tiD" => new IntervalType(IntervalUnit.DayTime),
-                    //"tin" => new 
IntervalType(IntervalUnit.MonthDayNanosecond), // Not yet implemented
+                    "tiM" => IntervalType.YearMonth,
+                    "tiD" => IntervalType.DayTime,
+                    //"tin" => IntervalType.MonthDayNanosecond, // Not yet 
implemented
                     _ => throw new NotSupportedException("Data type is not yet 
supported in import.")
                 };
             }
diff --git a/csharp/src/Apache.Arrow/Ipc/MessageSerializer.cs 
b/csharp/src/Apache.Arrow/Ipc/MessageSerializer.cs
index 7426ba4e86..f5031fbfb5 100644
--- a/csharp/src/Apache.Arrow/Ipc/MessageSerializer.cs
+++ b/csharp/src/Apache.Arrow/Ipc/MessageSerializer.cs
@@ -165,9 +165,9 @@ namespace Apache.Arrow.Ipc
                     switch (timeMeta.BitWidth)
                     {
                         case 32:
-                            return new 
Types.Time32Type(timeMeta.Unit.ToArrow());
+                            return 
(Time32Type)TimeType.FromTimeUnit(timeMeta.Unit.ToArrow());
                         case 64:
-                            return new 
Types.Time64Type(timeMeta.Unit.ToArrow());
+                            return 
(Time64Type)TimeType.FromTimeUnit(timeMeta.Unit.ToArrow());
                         default:
                             throw new InvalidDataException("Unsupported time 
bit width");
                     }
@@ -178,7 +178,7 @@ namespace Apache.Arrow.Ipc
                     return new Types.TimestampType(unit, timezone);
                 case Flatbuf.Type.Interval:
                     Flatbuf.Interval intervalMetadata = 
field.Type<Flatbuf.Interval>().Value;
-                    return new 
Types.IntervalType(intervalMetadata.Unit.ToArrow());
+                    return 
Types.IntervalType.FromIntervalUnit(intervalMetadata.Unit.ToArrow());
                 case Flatbuf.Type.Utf8:
                     return Types.StringType.Default;
                 case Flatbuf.Type.FixedSizeBinary:
diff --git a/csharp/src/Apache.Arrow/Types/IntervalUnit.cs 
b/csharp/src/Apache.Arrow/Types/IntervalUnit.cs
index 6dda0cfe94..bcd67e112d 100644
--- a/csharp/src/Apache.Arrow/Types/IntervalUnit.cs
+++ b/csharp/src/Apache.Arrow/Types/IntervalUnit.cs
@@ -19,11 +19,15 @@ namespace Apache.Arrow.Types
     public enum IntervalUnit
     {
         YearMonth = 0,
-        DayTime = 1
+        DayTime = 1,
     }
 
     public sealed class IntervalType : FixedWidthType
     {
+        public static readonly IntervalType YearMonth = new 
IntervalType(IntervalUnit.YearMonth);
+        public static readonly IntervalType DayTime = new 
IntervalType(IntervalUnit.DayTime);
+        private static readonly IntervalType[] _types = new IntervalType[] { 
YearMonth, DayTime };
+
         public override ArrowTypeId TypeId => ArrowTypeId.Interval;
         public override string Name => "date";
         public override int BitWidth => 64;
@@ -36,5 +40,10 @@ namespace Apache.Arrow.Types
         }
 
         public override void Accept(IArrowTypeVisitor visitor) => Accept(this, 
visitor);
+
+        public static IntervalType FromIntervalUnit(IntervalUnit unit)
+        {
+            return _types[(int)unit];
+        }
     }
 }
diff --git a/csharp/src/Apache.Arrow/Types/Time32Type.cs 
b/csharp/src/Apache.Arrow/Types/Time32Type.cs
index 99c409babd..80a23d4934 100644
--- a/csharp/src/Apache.Arrow/Types/Time32Type.cs
+++ b/csharp/src/Apache.Arrow/Types/Time32Type.cs
@@ -18,7 +18,7 @@ namespace Apache.Arrow.Types
 {
     public sealed class Time32Type : TimeType
     {
-        public static readonly Time32Type Default = new Time32Type();
+        public static readonly Time32Type Default = Millisecond;
 
         public override ArrowTypeId TypeId => ArrowTypeId.Time32;
         public override string Name => "time32";
diff --git a/csharp/src/Apache.Arrow/Types/Time64Type.cs 
b/csharp/src/Apache.Arrow/Types/Time64Type.cs
index 2777deb228..e7d911c277 100644
--- a/csharp/src/Apache.Arrow/Types/Time64Type.cs
+++ b/csharp/src/Apache.Arrow/Types/Time64Type.cs
@@ -18,7 +18,7 @@ namespace Apache.Arrow.Types
 {
     public sealed class Time64Type : TimeType
     {
-        public static readonly Time64Type Default = new Time64Type();
+        public static readonly Time64Type Default = Nanosecond;
 
         public override ArrowTypeId TypeId => ArrowTypeId.Time64;
         public override string Name => "time64";
diff --git a/csharp/src/Apache.Arrow/Types/TimeType.cs 
b/csharp/src/Apache.Arrow/Types/TimeType.cs
index 9afa3fb62c..48c7fdb5f1 100644
--- a/csharp/src/Apache.Arrow/Types/TimeType.cs
+++ b/csharp/src/Apache.Arrow/Types/TimeType.cs
@@ -26,11 +26,22 @@ namespace Apache.Arrow.Types
 
     public abstract class TimeType: FixedWidthType
     {
+        public static readonly Time32Type Second = new 
Time32Type(TimeUnit.Second);
+        public static readonly Time32Type Millisecond = new 
Time32Type(TimeUnit.Millisecond);
+        public static readonly Time64Type Microsecond = new 
Time64Type(TimeUnit.Microsecond);
+        public static readonly Time64Type Nanosecond = new 
Time64Type(TimeUnit.Nanosecond);
+        private static readonly TimeType[] _types = new TimeType[] { Second, 
Millisecond, Microsecond, Nanosecond };
+
         public TimeUnit Unit { get; }
 
         protected TimeType(TimeUnit unit)
         {
             Unit = unit;
         }
+
+        public static TimeType FromTimeUnit(TimeUnit unit)
+        {
+            return _types[(int)unit];
+        }
     }
 }

Reply via email to