Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prealloc confused by variable redeclaration #11

Open
rgeronimi opened this issue Jul 7, 2018 · 1 comment
Open

Prealloc confused by variable redeclaration #11

rgeronimi opened this issue Jul 7, 2018 · 1 comment

Comments

@rgeronimi
Copy link

rgeronimi commented Jul 7, 2018

In the following function, prealloc is confused by buf being redeclared:

func copySort(w io.Writer, r io.Reader, sep rune, keys []int) error {
	// Copy the header line.
	var buf [1]byte // THATS LINE 275 WHERE THE ERROR MESSAGES POINT TO
	for {
		n, err := r.Read(buf[:])
		if n != 0 {
			if _, err := w.Write(buf[:]); err != nil {
				return err
			}
		}
		if err != nil {
			if err == io.EOF {
				return nil
			}
			return err
		}
		if buf[0] == '\n' {
			break
		}
	}
	...
	for i, k := range keys {
		buf := make([]byte, 0, 16)
		buf = append(buf, "-k"...)
		n := int64(1 + k)
		buf = strconv.AppendInt(buf, n, 10)
		buf = append(buf, ',')
		buf = strconv.AppendInt(buf, n, 10)
		args[2+i] = alloc.DirectString(buf)
	}
	...
}

preallocreports these 2 error messages:

...:275 Consider preallocating buf
...:275 Consider preallocating buf

If I rename the first buf declaration with any other variable name (e.g., cell) the error messages disappear.

It seems a variable redeclaration is not always managed correctly by prealloc.

@alexkohler
Copy link
Owner

Interesting.. I'll try to look into this ASAP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants