Skip to content

Commit 7da6f14

Browse files
authored
Replace list.List with Go slices (#1950)
* Add .vscode to .gitignore * Replace `list.List` with slices
1 parent 0f7f415 commit 7da6f14

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+767
-918
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
.sw?
44
.*.sw?
5-
tags
65
*~
6+
/.vscode
7+
8+
tags
9+
710
push2
811
data/.gitignore
912

pkg/dsl/cst/dump.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func (node *DumpStatementNode) dumpToStdout(
186186
//
187187
// The output channel is always non-nil, except for the Miller REPL.
188188
if state.OutputRecordsAndContexts != nil {
189-
state.OutputRecordsAndContexts.PushBack(types.NewOutputString(outputString, state.Context))
189+
*state.OutputRecordsAndContexts = append(*state.OutputRecordsAndContexts, types.NewOutputString(outputString, state.Context))
190190
} else {
191191
fmt.Println(outputString)
192192
}

pkg/dsl/cst/emit1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (node *Emit1StatementNode) Execute(state *runtime.State) (*BlockExitPayload
6666
}
6767

6868
if state.OutputRecordsAndContexts != nil {
69-
state.OutputRecordsAndContexts.PushBack(types.NewRecordAndContext(valueAsMap, state.Context))
69+
*state.OutputRecordsAndContexts = append(*state.OutputRecordsAndContexts, types.NewRecordAndContext(valueAsMap, state.Context))
7070
} else {
7171
fmt.Println(valueAsMap.String())
7272
}

pkg/dsl/cst/emit_emitp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ func (node *EmitXStatementNode) emitRecordToRecordStream(
975975
) error {
976976
// The output channel is always non-nil, except for the Miller REPL.
977977
if state.OutputRecordsAndContexts != nil {
978-
state.OutputRecordsAndContexts.PushBack(types.NewRecordAndContext(outrec, state.Context))
978+
*state.OutputRecordsAndContexts = append(*state.OutputRecordsAndContexts, types.NewRecordAndContext(outrec, state.Context))
979979
} else {
980980
fmt.Println(outrec.String())
981981
}

pkg/dsl/cst/emitf.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func (node *EmitFStatementNode) emitfToRecordStream(
173173
) error {
174174
// The output channel is always non-nil, except for the Miller REPL.
175175
if state.OutputRecordsAndContexts != nil {
176-
state.OutputRecordsAndContexts.PushBack(types.NewRecordAndContext(outrec, state.Context))
176+
*state.OutputRecordsAndContexts = append(*state.OutputRecordsAndContexts, types.NewRecordAndContext(outrec, state.Context))
177177
} else {
178178
fmt.Println(outrec.String())
179179
}

pkg/dsl/cst/print.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ func (node *PrintStatementNode) printToStdout(
332332

333333
// The output channel is always non-nil, except for the Miller REPL.
334334
if state.OutputRecordsAndContexts != nil {
335-
state.OutputRecordsAndContexts.PushBack(types.NewOutputString(outputString, state.Context))
335+
*state.OutputRecordsAndContexts = append(*state.OutputRecordsAndContexts, types.NewOutputString(outputString, state.Context))
336336
} else {
337337
fmt.Print(outputString)
338338
}

pkg/input/line_reader.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package input
55

66
import (
77
"bufio"
8-
"container/list"
98
"io"
109
"strings"
1110

@@ -172,14 +171,14 @@ func (r *MultiIRSLineReader) Read() (string, error) {
172171
// IRS) stripped off. So, callers get "a=1,b=2" rather than "a=1,b=2\n".
173172
func channelizedLineReader(
174173
lineReader ILineReader,
175-
linesChannel chan<- *list.List,
174+
linesChannel chan<- []string,
176175
downstreamDoneChannel <-chan bool, // for mlr head
177176
recordsPerBatch int64,
178177
) {
179178
i := int64(0)
180179
done := false
181180

182-
lines := list.New()
181+
lines := make([]string, 0, recordsPerBatch)
183182

184183
for {
185184
line, err := lineReader.Read()
@@ -194,7 +193,7 @@ func channelizedLineReader(
194193

195194
i++
196195

197-
lines.PushBack(line)
196+
lines = append(lines, line)
198197

199198
// See if downstream processors will be ignoring further data (e.g. mlr
200199
// head). If so, stop reading. This makes 'mlr head hugefile' exit
@@ -211,7 +210,7 @@ func channelizedLineReader(
211210
break
212211
}
213212
linesChannel <- lines
214-
lines = list.New()
213+
lines = make([]string, 0, recordsPerBatch)
215214
}
216215

217216
if done {

pkg/input/pseudo_reader_gen.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package input
22

33
import (
4-
"container/list"
54
"fmt"
65

76
"github.com/johnkerl/miller/v6/pkg/bifs"
@@ -28,7 +27,7 @@ func NewPseudoReaderGen(
2827
func (reader *PseudoReaderGen) Read(
2928
filenames []string, // ignored
3029
context types.Context,
31-
readerChannel chan<- *list.List, // list of *types.RecordAndContext
30+
readerChannel chan<- []*types.RecordAndContext, // list of *types.RecordAndContext
3231
errorChannel chan error,
3332
downstreamDoneChannel <-chan bool, // for mlr head
3433
) {
@@ -38,7 +37,7 @@ func (reader *PseudoReaderGen) Read(
3837

3938
func (reader *PseudoReaderGen) process(
4039
context *types.Context,
41-
readerChannel chan<- *list.List, // list of *types.RecordAndContext
40+
readerChannel chan<- []*types.RecordAndContext, // list of *types.RecordAndContext
4241
errorChannel chan error,
4342
downstreamDoneChannel <-chan bool, // for mlr head
4443
) {
@@ -71,7 +70,7 @@ func (reader *PseudoReaderGen) process(
7170
key := reader.readerOptions.GeneratorOptions.FieldName
7271
value := start.Copy()
7372

74-
recordsAndContexts := list.New()
73+
recordsAndContexts := make([]*types.RecordAndContext, 0, recordsPerBatch)
7574

7675
eof := false
7776
for !eof {
@@ -84,11 +83,11 @@ func (reader *PseudoReaderGen) process(
8483
record.PutCopy(key, value)
8584

8685
context.UpdateForInputRecord()
87-
recordsAndContexts.PushBack(types.NewRecordAndContext(record, context))
86+
recordsAndContexts = append(recordsAndContexts, types.NewRecordAndContext(record, context))
8887

89-
if int64(recordsAndContexts.Len()) >= recordsPerBatch {
88+
if int64(len(recordsAndContexts)) >= recordsPerBatch {
9089
readerChannel <- recordsAndContexts
91-
recordsAndContexts = list.New()
90+
recordsAndContexts = make([]*types.RecordAndContext, 0, recordsPerBatch)
9291

9392
// See if downstream processors will be ignoring further data (e.g.
9493
// mlr head). If so, stop reading. This makes 'mlr head hugefile'
@@ -111,7 +110,7 @@ func (reader *PseudoReaderGen) process(
111110
value = bifs.BIF_plus_binary(value, step)
112111
}
113112

114-
if recordsAndContexts.Len() > 0 {
113+
if len(recordsAndContexts) > 0 {
115114
readerChannel <- recordsAndContexts
116115
}
117116
}

pkg/input/record_reader.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33

44
package input
55

6-
import (
7-
"container/list"
8-
9-
"github.com/johnkerl/miller/v6/pkg/types"
10-
)
6+
import "github.com/johnkerl/miller/v6/pkg/types"
117

128
// Since Go is concurrent, the context struct (AWK-like variables such as
139
// FILENAME, NF, NF, FNR, etc.) needs to be duplicated and passed through the
@@ -19,7 +15,7 @@ type IRecordReader interface {
1915
Read(
2016
filenames []string,
2117
initialContext types.Context,
22-
readerChannel chan<- *list.List, // list of *types.RecordAndContext
18+
readerChannel chan<- []*types.RecordAndContext, // list of *types.RecordAndContext
2319
errorChannel chan error,
2420
downstreamDoneChannel <-chan bool, // for mlr head
2521
)

pkg/input/record_reader_benchmark_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ func BenchmarkXTABParse(b *testing.B) {
5757
assert.Nil(b, err)
5858

5959
stanza := newStanza()
60-
stanza.dataLines.PushBack("color yellow")
61-
stanza.dataLines.PushBack("shape triangle")
62-
stanza.dataLines.PushBack("flag true")
63-
stanza.dataLines.PushBack("k 1")
64-
stanza.dataLines.PushBack("index 11")
65-
stanza.dataLines.PushBack("quantity 43.6498")
66-
stanza.dataLines.PushBack("rate 9.8870")
60+
stanza.dataLines = append(stanza.dataLines, "color yellow")
61+
stanza.dataLines = append(stanza.dataLines, "shape triangle")
62+
stanza.dataLines = append(stanza.dataLines, "flag true")
63+
stanza.dataLines = append(stanza.dataLines, "k 1")
64+
stanza.dataLines = append(stanza.dataLines, "index 11")
65+
stanza.dataLines = append(stanza.dataLines, "quantity 43.6498")
66+
stanza.dataLines = append(stanza.dataLines, "rate 9.8870")
6767

6868
for i := 0; i < b.N; i++ {
6969
_, _ = reader.recordFromXTABLines(stanza.dataLines)

0 commit comments

Comments
 (0)