Skip to content

Commit f0a7c58

Browse files
authored
Performance and style fixes (#1981)
* Switch to integer ranges in for loops Signed-off-by: Stephen Kitt <steve@sk2.org> * Switch to slices functions where appropriate A number of utility functions can be replaced outright; since Miller can technically be used as a library, these are deprecated rather than removed. go:fix directives ensure that they can be replaced automatically. Signed-off-by: Stephen Kitt <steve@sk2.org> * Switch to reflect.TypeFor This is slightly more efficient than TypeOf when the type is known at compile time. Signed-off-by: Stephen Kitt <steve@sk2.org> * Switch to strings.SplitSeq instead of strings.Split SplitSeq results in fewer allocations. Signed-off-by: Stephen Kitt <steve@sk2.org> * Drop obsolete build directives Signed-off-by: Stephen Kitt <steve@sk2.org> * Use min/max instead of explicit comparisons Signed-off-by: Stephen Kitt <steve@sk2.org> * Append slices instead of looping Signed-off-by: Stephen Kitt <steve@sk2.org> --------- Signed-off-by: Stephen Kitt <steve@sk2.org>
1 parent e8c4187 commit f0a7c58

Some content is hidden

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

47 files changed

+117
-175
lines changed

pkg/auxents/hex.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func hexDumpFile(istream *os.File, doRaw bool) {
108108
}
109109

110110
// Print hex payload
111-
for i := 0; i < bufferSize; i++ {
111+
for i := range bufferSize {
112112
if i < numBytesRead {
113113
fmt.Printf("%02x ", buffer[i])
114114
} else {
@@ -125,7 +125,7 @@ func hexDumpFile(istream *os.File, doRaw bool) {
125125
if !doRaw {
126126
fmt.Printf("|")
127127

128-
for i := 0; i < numBytesRead; i++ {
128+
for i := range numBytesRead {
129129
if buffer[i] >= 0x20 && buffer[i] <= 0x7e {
130130
fmt.Printf("%c", buffer[i])
131131
} else {

pkg/bifs/stats.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ func BIF_sort_collection(collection *mlrval.Mlrval) *mlrval.Mlrval {
458458
arrayval := collection.AcquireArrayValue()
459459
n := len(arrayval)
460460
array = make([]*mlrval.Mlrval, n)
461-
for i := 0; i < n; i++ {
461+
for i := range n {
462462
array[i] = arrayval[i].Copy()
463463
}
464464
} else {

pkg/bifs/stats_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
func stats_test_array(n int) *mlrval.Mlrval {
1313
a := make([]*mlrval.Mlrval, n)
14-
for i := 0; i < n; i++ {
14+
for i := range n {
1515
a[i] = mlrval.FromInt(int64(i))
1616
}
1717
return mlrval.FromArray(a)
@@ -20,7 +20,7 @@ func stats_test_array(n int) *mlrval.Mlrval {
2020
func array_to_map_for_test(a *mlrval.Mlrval) *mlrval.Mlrval {
2121
array := a.AcquireArrayValue()
2222
m := mlrval.NewMlrmap()
23-
for i := 0; i < len(array); i++ {
23+
for i := range array {
2424
key := fmt.Sprint(i)
2525
val := array[i]
2626
m.PutCopy(key, val)
@@ -34,7 +34,7 @@ func TestBIF_count(t *testing.T) {
3434
output := BIF_count(input)
3535
assert.True(t, output.IsError())
3636

37-
for n := 0; n < 5; n++ {
37+
for n := range 5 {
3838
input = stats_test_array(n)
3939
assert.True(t, mlrval.Equals(BIF_count(input), mlrval.FromInt(int64(n))))
4040

pkg/cli/flag_types.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ package cli
3737

3838
import (
3939
"fmt"
40+
"slices"
4041
"sort"
4142
"strings"
4243

@@ -390,12 +391,7 @@ func (flag *Flag) Owns(input string) bool {
390391
if flag.name == input {
391392
return true
392393
}
393-
for _, name := range flag.altNames {
394-
if name == input {
395-
return true
396-
}
397-
}
398-
return false
394+
return slices.Contains(flag.altNames, input)
399395
}
400396

401397
// Matches is like Owns but is for substring matching, for on-line help with

pkg/dsl/ast_print.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (node *ASTNode) Print() {
5757
// printAux is a recursion-helper for Print.
5858
func (node *ASTNode) printAux(depth int) {
5959
// Indent
60-
for i := 0; i < depth; i++ {
60+
for range depth {
6161
fmt.Print(" ")
6262
}
6363

@@ -83,14 +83,14 @@ func (node *ASTNode) PrintParex() {
8383
// printParexAux is a recursion-helper for PrintParex.
8484
func (node *ASTNode) printParexAux(depth int) {
8585
if node.IsLeaf() {
86-
for i := 0; i < depth; i++ {
86+
for range depth {
8787
fmt.Print(" ")
8888
}
8989
fmt.Println(node.Text())
9090

9191
} else if node.ChildrenAreAllLeaves() {
9292
// E.g. (= sum 0) or (+ 1 2)
93-
for i := 0; i < depth; i++ {
93+
for range depth {
9494
fmt.Print(" ")
9595
}
9696
fmt.Print("(")
@@ -104,7 +104,7 @@ func (node *ASTNode) printParexAux(depth int) {
104104

105105
} else {
106106
// Parent and opening parenthesis on first line
107-
for i := 0; i < depth; i++ {
107+
for range depth {
108108
fmt.Print(" ")
109109
}
110110
fmt.Print("(")
@@ -116,7 +116,7 @@ func (node *ASTNode) printParexAux(depth int) {
116116
}
117117

118118
// Closing parenthesis on last line
119-
for i := 0; i < depth; i++ {
119+
for range depth {
120120
fmt.Print(" ")
121121
}
122122
fmt.Println(")")

pkg/dsl/cst/hofs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ func sortM(
695695

696696
// Make a new map with entries in the new sort order.
697697
outmap := mlrval.NewMlrmap()
698-
for i := int64(0); i < n; i++ {
698+
for i := range n {
699699
entry := entries[i]
700700
outmap.PutCopy(entry.Key, entry.Value)
701701
}

pkg/input/record_reader_csv.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ func (reader *RecordReaderCSV) getRecordBatch(
229229
if reader.header == nil { // implicit CSV header
230230
n := len(csvRecord)
231231
reader.header = make([]string, n)
232-
for i := 0; i < n; i++ {
232+
for i := range n {
233233
reader.header[i] = strconv.Itoa(i + 1)
234234
}
235235
}
@@ -240,7 +240,7 @@ func (reader *RecordReaderCSV) getRecordBatch(
240240
nd := int64(len(csvRecord))
241241

242242
if nh == nd {
243-
for i := int64(0); i < nh; i++ {
243+
for i := range nh {
244244
key := reader.header[i]
245245
value := mlrval.FromDeferredType(csvRecord[i])
246246
_, err := record.PutReferenceMaybeDedupe(key, value, dedupeFieldNames)

pkg/input/record_reader_csvlite.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ func getRecordBatchImplicitCSVHeader(
332332
if reader.headerStrings == nil {
333333
n := len(fields)
334334
reader.headerStrings = make([]string, n)
335-
for i := 0; i < n; i++ {
335+
for i := range n {
336336
reader.headerStrings[i] = strconv.Itoa(i + 1)
337337
}
338338
} else {

pkg/input/record_reader_json.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func (bsr *JSONCommentEnabledReader) populateFromLine(p []byte) int {
283283
numBytesWritten = len(bsr.lineBytes)
284284
bsr.lineBytes = nil
285285
} else {
286-
for i := 0; i < len(p); i++ {
286+
for i := range p {
287287
p[i] = bsr.lineBytes[i]
288288
}
289289
numBytesWritten = len(p)

pkg/input/record_reader_pprint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ func getRecordBatchImplicitPprintHeader(
367367
if reader.headerStrings == nil {
368368
n := len(fields)
369369
reader.headerStrings = make([]string, n)
370-
for i := 0; i < n; i++ {
370+
for i := range n {
371371
reader.headerStrings[i] = strconv.Itoa(i + 1)
372372
}
373373
} else {

0 commit comments

Comments
 (0)