Skip to content

cmd/compile: optimized range memclr doesn't work with pointers to arrays #52635

Closed

Description

@josharian
package p

type T struct {
	a *[10]int
	b [10]int
}

func (t *T) resetA() {
	for i := range t.a {
		t.a[i] = 0
	}
}

func (t *T) resetB() {
	for i := range t.b {
		t.b[i] = 0
	}
}

resetA compiles to a loop. resetB compiles to a call to runtime.memclrNoHeapPointers. I believe that resetA should also call memclrNoHeapPointers.

This showed up as a hot spot in my code today (with much larger arrays).

cc @randall77 @cuonglm @mdempsky

Activity

  1. josharian commented on Apr 30, 2022

    @josharian
    ContributorAuthor

    As evidence that this is just a missed optimization, here is equivalent code using a new intermediate type:

    type T struct {
    	c *A
    }
    
    type A struct {
    	x [10]int
    }
    
    func (t *T) resetC() {
    	for i := range t.c.x {
    		t.c.x[i] = 0
    	}
    }

    This generates runtime.memclrNoHeapPointers. (And also provides a workaround.)

  2. renthraysk commented on May 1, 2022

    @renthraysk

    There is a simpler workaround for i := range *t.a {.

  3. josharian commented on May 1, 2022

    @josharian
    ContributorAuthor

    @renthraysk I am astonished that that works, and I wrote the original optimization. Now that you say it, I can imagine why, although it has to do with weird quirks in the guts of the compiler. Anyway, I still think this is worth fixing. :P

  4. renthraysk commented on May 1, 2022

    @renthraysk

    Yea, agreed. Actually thought I filed an issue for this, but seems just tweeted about it back in 2017, so my bad.

    Subtle diff in
    func f1(in *[96]byte) { for i := range in { in[i] = 0 } }
    func f2(in *[96]byte) { for i := range *in { in[i] = 0 } }

  5. gopherbot commented on May 1, 2022

    @gopherbot
    Contributor

    Change https://go.dev/cl/403337 mentions this issue: cmd/compile: support pointers to arrays in arrayClear

  6. rip-create-your-account commented on May 1, 2022

    @rip-create-your-account

    The following generates slightly more efficient code for my computer.

    func (t *T) resetC() {
    	*t.a = [10]int{}
    }

    There's no runtime.memclrNoHeapPointers call at all. Instead the compiler seems to choose a suitable algorithm based on the size of the array. To me it seems like all of the loops above should compile to this.

  7. zigo101 commented on May 2, 2022

    @zigo101
    Contributor

    It looks the memclr way is more efficient than the assignment way for small arrays.
    Should the runtime internal unify the implementation for the two ways?

  8. locked and limited conversation to collaborators on May 3, 2023
  9. added a commit that references this issue on Sep 19, 2024
    0668e3c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions