feat: Improve memory allocation

This commit is contained in:
rjianu
2023-07-12 16:26:54 +03:00
parent 3d421a8534
commit 3bd76e879b

16
csv.go
View File

@@ -23,16 +23,22 @@ func avg(data []float64) float64 {
func csv2float(r io.Reader, column int) ([]float64, error) {
cr := csv.NewReader(r)
// set this to reuse the same slice for each read
// operation to reduce the memory allocation
cr.ReuseRecord = true
if column < 1 {
return nil, fmt.Errorf("%w: please provide a valid column number", ErrInvalidColumn)
}
column--
allData, err := cr.ReadAll()
if err != nil {
return nil, fmt.Errorf("cannot read data from file: %w", err)
}
var data []float64
for i, row := range allData {
for i := 0; ; i++ {
row, err := cr.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, fmt.Errorf("cannot read data from file: %w", err)
}
if i == 0 {
continue
}