Skip to content

Commit fd7f58c

Browse files
avoid repetitive memory allocations
1 parent 61eae9e commit fd7f58c

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

modules/git/foreachref/format.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ type Format struct {
2727
// for each reference. fieldDelim and refDelim should be selected to not
2828
// interfere with each other and to not be present in field values.
2929
fieldDelim []byte
30+
// fieldDelimStr is a string representation of fieldDelim. Used to save
31+
// us from repetitive reallocation whenever we need the delimiter as a
32+
// string.
33+
fieldDelimStr string
3034
// refDelim is the character sequence used to separate reference from
3135
// each other in the output. fieldDelim and refDelim should be selected
3236
// to not interfere with each other and to not be present in field
@@ -38,9 +42,10 @@ type Format struct {
3842
// git-for-each-ref(1) for available fields.
3943
func NewFormat(fieldNames ...string) Format {
4044
return Format{
41-
fieldNames: fieldNames,
42-
fieldDelim: nullChar,
43-
refDelim: dualNullChar,
45+
fieldNames: fieldNames,
46+
fieldDelim: nullChar,
47+
fieldDelimStr: string(nullChar),
48+
refDelim: dualNullChar,
4449
}
4550
}
4651

modules/git/foreachref/parser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func NewParser(r io.Reader, format Format) *Parser {
3535
scanner.Split(
3636
func(data []byte, atEOF bool) (advance int, token []byte, err error) {
3737
// Scan until delimiter, marking end of reference.
38-
delimIdx := bytes.Index(data, []byte(format.refDelim))
38+
delimIdx := bytes.Index(data, format.refDelim)
3939
if delimIdx >= 0 {
4040
token := data[:delimIdx]
4141
advance := delimIdx + len(format.refDelim)
@@ -93,7 +93,7 @@ func (p *Parser) parseRef(refBlock string) (map[string]string, error) {
9393

9494
fieldValues := make(map[string]string)
9595

96-
fields := strings.Split(refBlock, string(p.format.fieldDelim))
96+
fields := strings.Split(refBlock, p.format.fieldDelimStr)
9797
if len(fields) != len(p.format.fieldNames) {
9898
return nil, fmt.Errorf("unexpected number of reference fields: wanted %d, was %d",
9999
len(fields), len(p.format.fieldNames))

0 commit comments

Comments
 (0)