From 5c457c6a05e3b5c3f462402440ee37444e6f80fc Mon Sep 17 00:00:00 2001 From: Jack Bird Date: Wed, 17 Jan 2024 11:44:55 +0000 Subject: [PATCH] feat: Use IEnumerable in WriteApi to eliminate unnecessary memory allocations --- CHANGELOG.md | 1 + Client/WriteApi.cs | 18 +++++++++--------- Client/WriteApiAsync.cs | 27 +++++++++++++-------------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 36b231e56..57c5804d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -70,6 +70,7 @@ Update dependencies: ### Features 1. [#590](https://github.com/influxdata/influxdb-client-csharp/pull/590): Allows disable Trace verbose messages +2. [#606](https://github.com/influxdata/influxdb-client-csharp/pull/606): Use IEnumerable in WriteApi to eliminate unnescessary memmory allocations ### Dependencies Update dependencies: diff --git a/Client/WriteApi.cs b/Client/WriteApi.cs index bff60a8e1..bbfe6f212 100644 --- a/Client/WriteApi.cs +++ b/Client/WriteApi.cs @@ -42,7 +42,7 @@ void WriteRecord(string record, WritePrecision precision = WritePrecision.Ns, st /// specifies the precision for the unix timestamps within the body line-protocol; default Nanoseconds /// specifies the destination bucket for writes. If the bucket is not specified then is used config from . /// specifies the destination organization for writes. If the org is not specified then is used config from . - void WriteRecords(List records, WritePrecision precision = WritePrecision.Ns, + void WriteRecords(IEnumerable records, WritePrecision precision = WritePrecision.Ns, string bucket = null, string org = null); /// @@ -69,7 +69,7 @@ void WriteRecords(string[] records, WritePrecision precision = WritePrecision.Ns /// specifies the Data points to write into bucket /// specifies the destination bucket for writes. If the bucket is not specified then is used config from . /// specifies the destination organization for writes. If the org is not specified then is used config from . - void WritePoints(List points, string bucket = null, string org = null); + void WritePoints(IEnumerable points, string bucket = null, string org = null); /// /// Write Data points into specified bucket. @@ -98,7 +98,7 @@ void WriteMeasurement(TM measurement, WritePrecision precision = WritePrecis /// specifies the destination bucket for writes. If the bucket is not specified then is used config from . /// specifies the destination organization for writes. If the org is not specified then is used config from . /// measurement type - void WriteMeasurements(List measurements, WritePrecision precision = WritePrecision.Ns, + void WriteMeasurements(IEnumerable measurements, WritePrecision precision = WritePrecision.Ns, string bucket = null, string org = null); /// @@ -388,10 +388,10 @@ public void WriteRecord(string record, WritePrecision precision = WritePrecision /// specifies the precision for the unix timestamps within the body line-protocol; default Nanoseconds /// specifies the destination bucket for writes. If the bucket is not specified then is used config from . /// specifies the destination organization for writes. If the org is not specified then is used config from . - public void WriteRecords(List records, WritePrecision precision = WritePrecision.Ns, + public void WriteRecords(IEnumerable records, WritePrecision precision = WritePrecision.Ns, string bucket = null, string org = null) { - records.ForEach(record => WriteRecord(record, precision, bucket, org)); + foreach (var record in records) WriteRecord(record, precision, bucket, org); } /// @@ -430,7 +430,7 @@ public void WritePoint(PointData point, string bucket = null, string org = null) /// specifies the Data points to write into bucket /// specifies the destination bucket for writes. If the bucket is not specified then is used config from . /// specifies the destination organization for writes. If the org is not specified then is used config from . - public void WritePoints(List points, string bucket = null, string org = null) + public void WritePoints(IEnumerable points, string bucket = null, string org = null) { foreach (var point in points) WritePoint(point, bucket, org); } @@ -443,7 +443,7 @@ public void WritePoints(List points, string bucket = null, string org /// specifies the destination organization for writes. If the org is not specified then is used config from . public void WritePoints(PointData[] points, string bucket = null, string org = null) { - WritePoints(points.ToList(), bucket, org); + WritePoints(points.AsEnumerable(), bucket, org); } /// @@ -475,7 +475,7 @@ public void WriteMeasurement(TM measurement, WritePrecision precision = Writ /// specifies the destination bucket for writes. If the bucket is not specified then is used config from . /// specifies the destination organization for writes. If the org is not specified then is used config from . /// measurement type - public void WriteMeasurements(List measurements, WritePrecision precision = WritePrecision.Ns, + public void WriteMeasurements(IEnumerable measurements, WritePrecision precision = WritePrecision.Ns, string bucket = null, string org = null) { foreach (var measurement in measurements) WriteMeasurement(measurement, precision, bucket, org); @@ -492,7 +492,7 @@ public void WriteMeasurements(List measurements, WritePrecision precisio public void WriteMeasurements(TM[] measurements, WritePrecision precision = WritePrecision.Ns, string bucket = null, string org = null) { - WriteMeasurements(measurements.ToList(), precision, bucket, org); + WriteMeasurements(measurements.AsEnumerable(), precision, bucket, org); } /// diff --git a/Client/WriteApiAsync.cs b/Client/WriteApiAsync.cs index bd926637b..ad7859fa2 100644 --- a/Client/WriteApiAsync.cs +++ b/Client/WriteApiAsync.cs @@ -34,7 +34,7 @@ Task WriteRecordAsync(string record, WritePrecision precision = WritePrecision.N /// specifies the destination bucket for writes. If the bucket is not specified then is used config from . /// specifies the destination organization for writes. If the org is not specified then is used config from . /// specifies the token to monitor for cancellation requests - Task WriteRecordsAsync(List records, WritePrecision precision = WritePrecision.Ns, + Task WriteRecordsAsync(IEnumerable records, WritePrecision precision = WritePrecision.Ns, string bucket = null, string org = null, CancellationToken cancellationToken = default); /// @@ -78,7 +78,7 @@ Task WritePointAsync(PointData point, string bucket = null, string org = null, /// specifies the destination bucket for writes. If the bucket is not specified then is used config from . /// specifies the destination organization for writes. If the org is not specified then is used config from . /// specifies the token to monitor for cancellation requests - Task WritePointsAsync(List points, string bucket = null, string org = null, + Task WritePointsAsync(IEnumerable points, string bucket = null, string org = null, CancellationToken cancellationToken = default); /// @@ -124,7 +124,7 @@ Task WriteMeasurementAsync(TM measurement, WritePrecision precision = WriteP /// specifies the destination organization for writes. If the org is not specified then is used config from . /// specifies the token to monitor for cancellation requests /// measurement type - Task WriteMeasurementsAsync(List measurements, WritePrecision precision = WritePrecision.Ns, + Task WriteMeasurementsAsync(IEnumerable measurements, WritePrecision precision = WritePrecision.Ns, string bucket = null, string org = null, CancellationToken cancellationToken = default); /// @@ -200,11 +200,11 @@ public Task WriteRecordAsync(string record, WritePrecision precision = WritePrec /// specifies the destination bucket for writes. If the bucket is not specified then is used config from . /// specifies the destination organization for writes. If the org is not specified then is used config from . /// specifies the token to monitor for cancellation requests - public Task WriteRecordsAsync(List records, WritePrecision precision = WritePrecision.Ns, + public Task WriteRecordsAsync(IEnumerable records, WritePrecision precision = WritePrecision.Ns, string bucket = null, string org = null, CancellationToken cancellationToken = default) { var options = new BatchWriteOptions(bucket ?? _options.Bucket, org ?? _options.Org, precision); - var list = records.Select(record => new BatchWriteRecord(options, record)).ToList(); + var list = records.Select(record => new BatchWriteRecord(options, record)); return WriteData(options.OrganizationId, options.Bucket, precision, list, cancellationToken); } @@ -220,7 +220,7 @@ public Task WriteRecordsAsync(List records, WritePrecision precision = W public Task WriteRecordsAsync(string[] records, WritePrecision precision = WritePrecision.Ns, string bucket = null, string org = null, CancellationToken cancellationToken = default) { - return WriteRecordsAsync(records.ToList(), precision, bucket, org, cancellationToken); + return WriteRecordsAsync(records.AsEnumerable(), precision, bucket, org, cancellationToken); } /// @@ -269,15 +269,14 @@ public Task WritePointAsync(PointData point, string bucket = null, string org = /// specifies the destination bucket for writes. If the bucket is not specified then is used config from . /// specifies the destination organization for writes. If the org is not specified then is used config from . /// specifies the token to monitor for cancellation requests - public async Task WritePointsAsync(List points, string bucket = null, string org = null, + public async Task WritePointsAsync(IEnumerable points, string bucket = null, string org = null, CancellationToken cancellationToken = default) { foreach (var grouped in points.GroupBy(it => it.Precision)) { var options = new BatchWriteOptions(bucket ?? _options.Bucket, org ?? _options.Org, grouped.Key); var groupedPoints = grouped - .Select(it => new BatchWritePoint(options, _options, it)) - .ToList(); + .Select(it => new BatchWritePoint(options, _options, it)); await WriteData(options.OrganizationId, options.Bucket, grouped.Key, groupedPoints, cancellationToken) .ConfigureAwait(false); @@ -294,7 +293,7 @@ await WriteData(options.OrganizationId, options.Bucket, grouped.Key, groupedPoin public Task WritePointsAsync(PointData[] points, string bucket = null, string org = null, CancellationToken cancellationToken = default) { - return WritePointsAsync(points.ToList(), bucket, org, cancellationToken); + return WritePointsAsync(points.AsEnumerable(), bucket, org, cancellationToken); } /// @@ -353,8 +352,9 @@ public Task WriteMeasurementAsync(TM measurement, WritePrecision precision = /// specifies the destination organization for writes. If the org is not specified then is used config from . /// specifies the token to monitor for cancellation requests /// measurement type - public Task WriteMeasurementsAsync(List measurements, WritePrecision precision = WritePrecision.Ns, - string bucket = null, string org = null, CancellationToken cancellationToken = default) + public Task WriteMeasurementsAsync(IEnumerable measurements, + WritePrecision precision = WritePrecision.Ns, string bucket = null, string org = null, + CancellationToken cancellationToken = default) { var list = new List(); @@ -380,7 +380,7 @@ public Task WriteMeasurementsAsync(List measurements, WritePrecision pre public Task WriteMeasurementsAsync(TM[] measurements, WritePrecision precision = WritePrecision.Ns, string bucket = null, string org = null, CancellationToken cancellationToken = default) { - return WriteMeasurementsAsync(measurements.ToList(), precision, bucket, org, cancellationToken); + return WriteMeasurementsAsync(measurements.AsEnumerable(), precision, bucket, org, cancellationToken); } /// @@ -404,7 +404,6 @@ public Task WriteMeasurementsAsyncWithIRestResponse(IEnumerabl return WriteDataAsyncWithIRestResponse(batch, bucket, org, precision, cancellationToken); } - private Task WriteData(string org, string bucket, WritePrecision precision, IEnumerable data, CancellationToken cancellationToken) {