Skip to content

cmd/compile: Array/slice clearing idiom inconsistently recognized #56954

Closed
@greatroar

Description

What version of Go are you using (go version)?

$ gotip version
go version devel go1.20-b1678e508b Wed Nov 16 04:04:52 2022 +0000 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What did you do?

package p

const N = 1 << 22

type T struct{ a, b int32 }

func arrayclear1(p *[N]T) { *p = [N]T{} }

func arrayclear2(p *[N]T) {
	for i := range p {
		p[i] = T{}
	}
}

func arrayclear3(p *[N]T) {
	for i := range p[:] {
		p[i] = T{}
	}
}

What did you expect to see?

Same code generated for all three.

What did you see instead?

arrayclear1 becomes REP STOSQ on amd64, REP STOSL on 386, a loop doing STP from ZR on arm64 and a simple loop on arm.

arrayclear2 becomes a call to runtime.memclrNoHeapPointers.

arrayclear3, which represents a library function that I found to be a hot spot, becomes a loop that sets one element at a time. On 386 and arm, that loop even includes a bounds check.

I suspected this might have to do with conversion to slice, but slice clearing is similarly inconsistent, producing different code depending on whether [:] (or [:n]) is present or not.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions