Skip to content

Commit 5c457c6

Browse files
committed
feat: Use IEnumerable in WriteApi to eliminate unnecessary memory allocations
1 parent ce5f722 commit 5c457c6

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Update dependencies:
7070
### Features
7171

7272
1. [#590](https://github.com/influxdata/influxdb-client-csharp/pull/590): Allows disable Trace verbose messages
73+
2. [#606](https://github.com/influxdata/influxdb-client-csharp/pull/606): Use IEnumerable in WriteApi to eliminate unnescessary memmory allocations
7374

7475
### Dependencies
7576
Update dependencies:

Client/WriteApi.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void WriteRecord(string record, WritePrecision precision = WritePrecision.Ns, st
4242
/// <param name="precision">specifies the precision for the unix timestamps within the body line-protocol; default Nanoseconds</param>
4343
/// <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>
4444
/// <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>
45-
void WriteRecords(List<string> records, WritePrecision precision = WritePrecision.Ns,
45+
void WriteRecords(IEnumerable<string> records, WritePrecision precision = WritePrecision.Ns,
4646
string bucket = null, string org = null);
4747

4848
/// <summary>
@@ -69,7 +69,7 @@ void WriteRecords(string[] records, WritePrecision precision = WritePrecision.Ns
6969
/// <param name="points">specifies the Data points to write into bucket</param>
7070
/// <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>
7171
/// <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>
72-
void WritePoints(List<PointData> points, string bucket = null, string org = null);
72+
void WritePoints(IEnumerable<PointData> points, string bucket = null, string org = null);
7373

7474
/// <summary>
7575
/// Write Data points into specified bucket.
@@ -98,7 +98,7 @@ void WriteMeasurement<TM>(TM measurement, WritePrecision precision = WritePrecis
9898
/// <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>
9999
/// <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>
100100
/// <typeparam name="TM">measurement type</typeparam>
101-
void WriteMeasurements<TM>(List<TM> measurements, WritePrecision precision = WritePrecision.Ns,
101+
void WriteMeasurements<TM>(IEnumerable<TM> measurements, WritePrecision precision = WritePrecision.Ns,
102102
string bucket = null, string org = null);
103103

104104
/// <summary>
@@ -388,10 +388,10 @@ public void WriteRecord(string record, WritePrecision precision = WritePrecision
388388
/// <param name="precision">specifies the precision for the unix timestamps within the body line-protocol; default Nanoseconds</param>
389389
/// <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>
390390
/// <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>
391-
public void WriteRecords(List<string> records, WritePrecision precision = WritePrecision.Ns,
391+
public void WriteRecords(IEnumerable<string> records, WritePrecision precision = WritePrecision.Ns,
392392
string bucket = null, string org = null)
393393
{
394-
records.ForEach(record => WriteRecord(record, precision, bucket, org));
394+
foreach (var record in records) WriteRecord(record, precision, bucket, org);
395395
}
396396

397397
/// <summary>
@@ -430,7 +430,7 @@ public void WritePoint(PointData point, string bucket = null, string org = null)
430430
/// <param name="points">specifies the Data points to write into bucket</param>
431431
/// <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>
432432
/// <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>
433-
public void WritePoints(List<PointData> points, string bucket = null, string org = null)
433+
public void WritePoints(IEnumerable<PointData> points, string bucket = null, string org = null)
434434
{
435435
foreach (var point in points) WritePoint(point, bucket, org);
436436
}
@@ -443,7 +443,7 @@ public void WritePoints(List<PointData> points, string bucket = null, string org
443443
/// <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>
444444
public void WritePoints(PointData[] points, string bucket = null, string org = null)
445445
{
446-
WritePoints(points.ToList(), bucket, org);
446+
WritePoints(points.AsEnumerable(), bucket, org);
447447
}
448448

449449
/// <summary>
@@ -475,7 +475,7 @@ public void WriteMeasurement<TM>(TM measurement, WritePrecision precision = Writ
475475
/// <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>
476476
/// <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>
477477
/// <typeparam name="TM">measurement type</typeparam>
478-
public void WriteMeasurements<TM>(List<TM> measurements, WritePrecision precision = WritePrecision.Ns,
478+
public void WriteMeasurements<TM>(IEnumerable<TM> measurements, WritePrecision precision = WritePrecision.Ns,
479479
string bucket = null, string org = null)
480480
{
481481
foreach (var measurement in measurements) WriteMeasurement(measurement, precision, bucket, org);
@@ -492,7 +492,7 @@ public void WriteMeasurements<TM>(List<TM> measurements, WritePrecision precisio
492492
public void WriteMeasurements<TM>(TM[] measurements, WritePrecision precision = WritePrecision.Ns,
493493
string bucket = null, string org = null)
494494
{
495-
WriteMeasurements(measurements.ToList(), precision, bucket, org);
495+
WriteMeasurements(measurements.AsEnumerable(), precision, bucket, org);
496496
}
497497

498498
/// <summary>

Client/WriteApiAsync.cs

+13-14
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Task WriteRecordAsync(string record, WritePrecision precision = WritePrecision.N
3434
/// <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>
3535
/// <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>
3636
/// <param name="cancellationToken">specifies the token to monitor for cancellation requests</param>
37-
Task WriteRecordsAsync(List<string> records, WritePrecision precision = WritePrecision.Ns,
37+
Task WriteRecordsAsync(IEnumerable<string> records, WritePrecision precision = WritePrecision.Ns,
3838
string bucket = null, string org = null, CancellationToken cancellationToken = default);
3939

4040
/// <summary>
@@ -78,7 +78,7 @@ Task WritePointAsync(PointData point, string bucket = null, string org = null,
7878
/// <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>
7979
/// <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>
8080
/// <param name="cancellationToken">specifies the token to monitor for cancellation requests</param>
81-
Task WritePointsAsync(List<PointData> points, string bucket = null, string org = null,
81+
Task WritePointsAsync(IEnumerable<PointData> points, string bucket = null, string org = null,
8282
CancellationToken cancellationToken = default);
8383

8484
/// <summary>
@@ -124,7 +124,7 @@ Task WriteMeasurementAsync<TM>(TM measurement, WritePrecision precision = WriteP
124124
/// <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>
125125
/// <param name="cancellationToken">specifies the token to monitor for cancellation requests</param>
126126
/// <typeparam name="TM">measurement type</typeparam>
127-
Task WriteMeasurementsAsync<TM>(List<TM> measurements, WritePrecision precision = WritePrecision.Ns,
127+
Task WriteMeasurementsAsync<TM>(IEnumerable<TM> measurements, WritePrecision precision = WritePrecision.Ns,
128128
string bucket = null, string org = null, CancellationToken cancellationToken = default);
129129

130130
/// <summary>
@@ -200,11 +200,11 @@ public Task WriteRecordAsync(string record, WritePrecision precision = WritePrec
200200
/// <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>
201201
/// <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>
202202
/// <param name="cancellationToken">specifies the token to monitor for cancellation requests</param>
203-
public Task WriteRecordsAsync(List<string> records, WritePrecision precision = WritePrecision.Ns,
203+
public Task WriteRecordsAsync(IEnumerable<string> records, WritePrecision precision = WritePrecision.Ns,
204204
string bucket = null, string org = null, CancellationToken cancellationToken = default)
205205
{
206206
var options = new BatchWriteOptions(bucket ?? _options.Bucket, org ?? _options.Org, precision);
207-
var list = records.Select(record => new BatchWriteRecord(options, record)).ToList();
207+
var list = records.Select(record => new BatchWriteRecord(options, record));
208208

209209
return WriteData(options.OrganizationId, options.Bucket, precision, list, cancellationToken);
210210
}
@@ -220,7 +220,7 @@ public Task WriteRecordsAsync(List<string> records, WritePrecision precision = W
220220
public Task WriteRecordsAsync(string[] records, WritePrecision precision = WritePrecision.Ns,
221221
string bucket = null, string org = null, CancellationToken cancellationToken = default)
222222
{
223-
return WriteRecordsAsync(records.ToList(), precision, bucket, org, cancellationToken);
223+
return WriteRecordsAsync(records.AsEnumerable(), precision, bucket, org, cancellationToken);
224224
}
225225

226226
/// <summary>
@@ -269,15 +269,14 @@ public Task WritePointAsync(PointData point, string bucket = null, string org =
269269
/// <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>
270270
/// <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>
271271
/// <param name="cancellationToken">specifies the token to monitor for cancellation requests</param>
272-
public async Task WritePointsAsync(List<PointData> points, string bucket = null, string org = null,
272+
public async Task WritePointsAsync(IEnumerable<PointData> points, string bucket = null, string org = null,
273273
CancellationToken cancellationToken = default)
274274
{
275275
foreach (var grouped in points.GroupBy(it => it.Precision))
276276
{
277277
var options = new BatchWriteOptions(bucket ?? _options.Bucket, org ?? _options.Org, grouped.Key);
278278
var groupedPoints = grouped
279-
.Select(it => new BatchWritePoint(options, _options, it))
280-
.ToList();
279+
.Select(it => new BatchWritePoint(options, _options, it));
281280

282281
await WriteData(options.OrganizationId, options.Bucket, grouped.Key, groupedPoints, cancellationToken)
283282
.ConfigureAwait(false);
@@ -294,7 +293,7 @@ await WriteData(options.OrganizationId, options.Bucket, grouped.Key, groupedPoin
294293
public Task WritePointsAsync(PointData[] points, string bucket = null, string org = null,
295294
CancellationToken cancellationToken = default)
296295
{
297-
return WritePointsAsync(points.ToList(), bucket, org, cancellationToken);
296+
return WritePointsAsync(points.AsEnumerable(), bucket, org, cancellationToken);
298297
}
299298

300299
/// <summary>
@@ -353,8 +352,9 @@ public Task WriteMeasurementAsync<TM>(TM measurement, WritePrecision precision =
353352
/// <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>
354353
/// <param name="cancellationToken">specifies the token to monitor for cancellation requests</param>
355354
/// <typeparam name="TM">measurement type</typeparam>
356-
public Task WriteMeasurementsAsync<TM>(List<TM> measurements, WritePrecision precision = WritePrecision.Ns,
357-
string bucket = null, string org = null, CancellationToken cancellationToken = default)
355+
public Task WriteMeasurementsAsync<TM>(IEnumerable<TM> measurements,
356+
WritePrecision precision = WritePrecision.Ns, string bucket = null, string org = null,
357+
CancellationToken cancellationToken = default)
358358
{
359359
var list = new List<BatchWriteData>();
360360

@@ -380,7 +380,7 @@ public Task WriteMeasurementsAsync<TM>(List<TM> measurements, WritePrecision pre
380380
public Task WriteMeasurementsAsync<TM>(TM[] measurements, WritePrecision precision = WritePrecision.Ns,
381381
string bucket = null, string org = null, CancellationToken cancellationToken = default)
382382
{
383-
return WriteMeasurementsAsync(measurements.ToList(), precision, bucket, org, cancellationToken);
383+
return WriteMeasurementsAsync(measurements.AsEnumerable(), precision, bucket, org, cancellationToken);
384384
}
385385

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

407-
408407
private Task WriteData(string org, string bucket, WritePrecision precision, IEnumerable<BatchWriteData> data,
409408
CancellationToken cancellationToken)
410409
{

0 commit comments

Comments
 (0)