Skip to content

Commit

Permalink
GH-36816: [C#] Reduce allocations (#36817)
Browse files Browse the repository at this point in the history
### 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 <curt@hagenlocher.org>
Signed-off-by: David Li <li.davidm96@gmail.com>
  • Loading branch information
CurtHagenlocher authored Aug 18, 2023
1 parent fc96fd2 commit 4fa6719
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 17 deletions.
2 changes: 1 addition & 1 deletion csharp/src/Apache.Arrow/Arrays/Time32Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Builder()
: 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.
Expand Down
2 changes: 1 addition & 1 deletion csharp/src/Apache.Arrow/Arrays/Time64Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Builder()
: 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.
Expand Down
4 changes: 2 additions & 2 deletions csharp/src/Apache.Arrow/C/CArrowArrayStreamExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public static unsafe void ExportArrayStream(IArrowArrayStream arrayStream, CArro
{
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);
Expand Down
14 changes: 7 additions & 7 deletions csharp/src/Apache.Arrow/C/CArrowSchemaImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,14 @@ public ArrowType GetAsType()
// 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.")
};
}
Expand Down
6 changes: 3 additions & 3 deletions csharp/src/Apache.Arrow/Ipc/MessageSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ private static Types.IArrowType GetFieldArrowType(Flatbuf.Field field, Field[] c
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");
}
Expand All @@ -178,7 +178,7 @@ private static Types.IArrowType GetFieldArrowType(Flatbuf.Field field, Field[] c
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:
Expand Down
11 changes: 10 additions & 1 deletion csharp/src/Apache.Arrow/Types/IntervalUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,5 +40,10 @@ public IntervalType(IntervalUnit unit = IntervalUnit.YearMonth)
}

public override void Accept(IArrowTypeVisitor visitor) => Accept(this, visitor);

public static IntervalType FromIntervalUnit(IntervalUnit unit)
{
return _types[(int)unit];
}
}
}
2 changes: 1 addition & 1 deletion csharp/src/Apache.Arrow/Types/Time32Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion csharp/src/Apache.Arrow/Types/Time64Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
11 changes: 11 additions & 0 deletions csharp/src/Apache.Arrow/Types/TimeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,22 @@ public enum TimeUnit

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];
}
}
}

0 comments on commit 4fa6719

Please sign in to comment.