Skip to content

Commit

Permalink
feat: Use IEnumerable in WriteApi to eliminate unnecessary memory all…
Browse files Browse the repository at this point in the history
…ocations
  • Loading branch information
birdalicious committed Aug 9, 2024
1 parent ce5f722 commit 5c457c6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 9 additions & 9 deletions Client/WriteApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void WriteRecord(string record, WritePrecision precision = WritePrecision.Ns, st
/// <param name="precision">specifies the precision for the unix timestamps within the body line-protocol; default Nanoseconds</param>
/// <param name="bucket">specifies the destination bucket for writes. If the bucket is not specified then is used config from <see cref="InfluxDBClientOptions.Bucket" />.</param>
/// <param name="org">specifies the destination organization for writes. If the org is not specified then is used config from <see cref="InfluxDBClientOptions.Org" />.</param>
void WriteRecords(List<string> records, WritePrecision precision = WritePrecision.Ns,
void WriteRecords(IEnumerable<string> records, WritePrecision precision = WritePrecision.Ns,
string bucket = null, string org = null);

/// <summary>
Expand All @@ -69,7 +69,7 @@ void WriteRecords(string[] records, WritePrecision precision = WritePrecision.Ns
/// <param name="points">specifies the Data points to write into bucket</param>
/// <param name="bucket">specifies the destination bucket for writes. If the bucket is not specified then is used config from <see cref="InfluxDBClientOptions.Bucket" />.</param>
/// <param name="org">specifies the destination organization for writes. If the org is not specified then is used config from <see cref="InfluxDBClientOptions.Org" />.</param>
void WritePoints(List<PointData> points, string bucket = null, string org = null);
void WritePoints(IEnumerable<PointData> points, string bucket = null, string org = null);

/// <summary>
/// Write Data points into specified bucket.
Expand Down Expand Up @@ -98,7 +98,7 @@ void WriteMeasurement<TM>(TM measurement, WritePrecision precision = WritePrecis
/// <param name="bucket">specifies the destination bucket for writes. If the bucket is not specified then is used config from <see cref="InfluxDBClientOptions.Bucket" />.</param>
/// <param name="org">specifies the destination organization for writes. If the org is not specified then is used config from <see cref="InfluxDBClientOptions.Org" />.</param>
/// <typeparam name="TM">measurement type</typeparam>
void WriteMeasurements<TM>(List<TM> measurements, WritePrecision precision = WritePrecision.Ns,
void WriteMeasurements<TM>(IEnumerable<TM> measurements, WritePrecision precision = WritePrecision.Ns,
string bucket = null, string org = null);

/// <summary>
Expand Down Expand Up @@ -388,10 +388,10 @@ public void WriteRecord(string record, WritePrecision precision = WritePrecision
/// <param name="precision">specifies the precision for the unix timestamps within the body line-protocol; default Nanoseconds</param>
/// <param name="bucket">specifies the destination bucket for writes. If the bucket is not specified then is used config from <see cref="InfluxDBClientOptions.Bucket" />.</param>
/// <param name="org">specifies the destination organization for writes. If the org is not specified then is used config from <see cref="InfluxDBClientOptions.Org" />.</param>
public void WriteRecords(List<string> records, WritePrecision precision = WritePrecision.Ns,
public void WriteRecords(IEnumerable<string> 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);
}

/// <summary>
Expand Down Expand Up @@ -430,7 +430,7 @@ public void WritePoint(PointData point, string bucket = null, string org = null)
/// <param name="points">specifies the Data points to write into bucket</param>
/// <param name="bucket">specifies the destination bucket for writes. If the bucket is not specified then is used config from <see cref="InfluxDBClientOptions.Bucket" />.</param>
/// <param name="org">specifies the destination organization for writes. If the org is not specified then is used config from <see cref="InfluxDBClientOptions.Org" />.</param>
public void WritePoints(List<PointData> points, string bucket = null, string org = null)
public void WritePoints(IEnumerable<PointData> points, string bucket = null, string org = null)
{
foreach (var point in points) WritePoint(point, bucket, org);
}
Expand All @@ -443,7 +443,7 @@ public void WritePoints(List<PointData> points, string bucket = null, string org
/// <param name="org">specifies the destination organization for writes. If the org is not specified then is used config from <see cref="InfluxDBClientOptions.Org" />.</param>
public void WritePoints(PointData[] points, string bucket = null, string org = null)
{
WritePoints(points.ToList(), bucket, org);
WritePoints(points.AsEnumerable(), bucket, org);
}

/// <summary>
Expand Down Expand Up @@ -475,7 +475,7 @@ public void WriteMeasurement<TM>(TM measurement, WritePrecision precision = Writ
/// <param name="bucket">specifies the destination bucket for writes. If the bucket is not specified then is used config from <see cref="InfluxDBClientOptions.Bucket" />.</param>
/// <param name="org">specifies the destination organization for writes. If the org is not specified then is used config from <see cref="InfluxDBClientOptions.Org" />.</param>
/// <typeparam name="TM">measurement type</typeparam>
public void WriteMeasurements<TM>(List<TM> measurements, WritePrecision precision = WritePrecision.Ns,
public void WriteMeasurements<TM>(IEnumerable<TM> measurements, WritePrecision precision = WritePrecision.Ns,
string bucket = null, string org = null)
{
foreach (var measurement in measurements) WriteMeasurement(measurement, precision, bucket, org);
Expand All @@ -492,7 +492,7 @@ public void WriteMeasurements<TM>(List<TM> measurements, WritePrecision precisio
public void WriteMeasurements<TM>(TM[] measurements, WritePrecision precision = WritePrecision.Ns,
string bucket = null, string org = null)
{
WriteMeasurements(measurements.ToList(), precision, bucket, org);
WriteMeasurements(measurements.AsEnumerable(), precision, bucket, org);
}

/// <summary>
Expand Down
27 changes: 13 additions & 14 deletions Client/WriteApiAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Task WriteRecordAsync(string record, WritePrecision precision = WritePrecision.N
/// <param name="bucket">specifies the destination bucket for writes. If the bucket is not specified then is used config from <see cref="InfluxDBClientOptions.Bucket" />.</param>
/// <param name="org">specifies the destination organization for writes. If the org is not specified then is used config from <see cref="InfluxDBClientOptions.Org" />.</param>
/// <param name="cancellationToken">specifies the token to monitor for cancellation requests</param>
Task WriteRecordsAsync(List<string> records, WritePrecision precision = WritePrecision.Ns,
Task WriteRecordsAsync(IEnumerable<string> records, WritePrecision precision = WritePrecision.Ns,
string bucket = null, string org = null, CancellationToken cancellationToken = default);

/// <summary>
Expand Down Expand Up @@ -78,7 +78,7 @@ Task WritePointAsync(PointData point, string bucket = null, string org = null,
/// <param name="bucket">specifies the destination bucket for writes. If the bucket is not specified then is used config from <see cref="InfluxDBClientOptions.Bucket" />.</param>
/// <param name="org">specifies the destination organization for writes. If the org is not specified then is used config from <see cref="InfluxDBClientOptions.Org" />.</param>
/// <param name="cancellationToken">specifies the token to monitor for cancellation requests</param>
Task WritePointsAsync(List<PointData> points, string bucket = null, string org = null,
Task WritePointsAsync(IEnumerable<PointData> points, string bucket = null, string org = null,
CancellationToken cancellationToken = default);

/// <summary>
Expand Down Expand Up @@ -124,7 +124,7 @@ Task WriteMeasurementAsync<TM>(TM measurement, WritePrecision precision = WriteP
/// <param name="org">specifies the destination organization for writes. If the org is not specified then is used config from <see cref="InfluxDBClientOptions.Org" />.</param>
/// <param name="cancellationToken">specifies the token to monitor for cancellation requests</param>
/// <typeparam name="TM">measurement type</typeparam>
Task WriteMeasurementsAsync<TM>(List<TM> measurements, WritePrecision precision = WritePrecision.Ns,
Task WriteMeasurementsAsync<TM>(IEnumerable<TM> measurements, WritePrecision precision = WritePrecision.Ns,
string bucket = null, string org = null, CancellationToken cancellationToken = default);

/// <summary>
Expand Down Expand Up @@ -200,11 +200,11 @@ public Task WriteRecordAsync(string record, WritePrecision precision = WritePrec
/// <param name="bucket">specifies the destination bucket for writes. If the bucket is not specified then is used config from <see cref="InfluxDBClientOptions.Bucket" />.</param>
/// <param name="org">specifies the destination organization for writes. If the org is not specified then is used config from <see cref="InfluxDBClientOptions.Org" />.</param>
/// <param name="cancellationToken">specifies the token to monitor for cancellation requests</param>
public Task WriteRecordsAsync(List<string> records, WritePrecision precision = WritePrecision.Ns,
public Task WriteRecordsAsync(IEnumerable<string> 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);
}
Expand All @@ -220,7 +220,7 @@ public Task WriteRecordsAsync(List<string> 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);
}

/// <summary>
Expand Down Expand Up @@ -269,15 +269,14 @@ public Task WritePointAsync(PointData point, string bucket = null, string org =
/// <param name="bucket">specifies the destination bucket for writes. If the bucket is not specified then is used config from <see cref="InfluxDBClientOptions.Bucket" />.</param>
/// <param name="org">specifies the destination organization for writes. If the org is not specified then is used config from <see cref="InfluxDBClientOptions.Org" />.</param>
/// <param name="cancellationToken">specifies the token to monitor for cancellation requests</param>
public async Task WritePointsAsync(List<PointData> points, string bucket = null, string org = null,
public async Task WritePointsAsync(IEnumerable<PointData> 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);
Expand All @@ -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);
}

/// <summary>
Expand Down Expand Up @@ -353,8 +352,9 @@ public Task WriteMeasurementAsync<TM>(TM measurement, WritePrecision precision =
/// <param name="org">specifies the destination organization for writes. If the org is not specified then is used config from <see cref="InfluxDBClientOptions.Org" />.</param>
/// <param name="cancellationToken">specifies the token to monitor for cancellation requests</param>
/// <typeparam name="TM">measurement type</typeparam>
public Task WriteMeasurementsAsync<TM>(List<TM> measurements, WritePrecision precision = WritePrecision.Ns,
string bucket = null, string org = null, CancellationToken cancellationToken = default)
public Task WriteMeasurementsAsync<TM>(IEnumerable<TM> measurements,
WritePrecision precision = WritePrecision.Ns, string bucket = null, string org = null,
CancellationToken cancellationToken = default)
{
var list = new List<BatchWriteData>();

Expand All @@ -380,7 +380,7 @@ public Task WriteMeasurementsAsync<TM>(List<TM> measurements, WritePrecision pre
public Task WriteMeasurementsAsync<TM>(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);
}

/// <summary>
Expand All @@ -404,7 +404,6 @@ public Task<RestResponse> WriteMeasurementsAsyncWithIRestResponse<TM>(IEnumerabl
return WriteDataAsyncWithIRestResponse(batch, bucket, org, precision, cancellationToken);
}


private Task WriteData(string org, string bucket, WritePrecision precision, IEnumerable<BatchWriteData> data,
CancellationToken cancellationToken)
{
Expand Down

0 comments on commit 5c457c6

Please sign in to comment.