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

cmd/compile: avoid using large stack to partially zero struct #18636

Open
dsnet opened this Issue Jan 12, 2017 · 8 comments

Comments

Projects
None yet
5 participants
@dsnet
Member

dsnet commented Jan 12, 2017

Using go1.7.3.

Consider the following program:

type Foo struct {
	A [1 << 20]byte
	B string
}

func run(c chan bool) {
	f := new(Foo)
	*f = Foo{B: "hello"}
	c <- true
}

func main() {
	debug.SetMaxStack(1 << 16)
	c := make(chan bool)
	go run(c)
	<-c
}

I currently see:

runtime: goroutine stack exceeds 65536-byte limit
fatal error: stack overflow

I expect to see:

Program exited (successfully).

The Foo struct contains a large embedded array. We assign to *f with Foo{B: "hello"}, which causes the stack to grow to accommodate the large embedded array. Since A was never named in the struct literal, the compiler should know that it is going to be all zeros and use a memclr directly to the heap, rather copying it through the stack.

It is common to use the pattern above in reset logic to reset everything to zero except for the logic that needs to be initialized:

*zr = Reader{
	rd:   zr.rd,
	step: (*Reader).readBlockHeader,
	dict: zr.dict,
	pd1:  zr.pd1,
	pd2:  zr.pd2,
}

\cc @randall77 @dr2chase

@dsnet dsnet added the Performance label Jan 12, 2017

@dsnet dsnet changed the title from cmd/compile: avoid using large stack to zero struct to cmd/compile: avoid using large stack to partially zero struct Jan 12, 2017

@bradfitz bradfitz modified the milestones: Go1.9, Go1.9Maybe Jan 12, 2017

@gopherbot

This comment has been minimized.

gopherbot commented Jan 12, 2017

CL https://golang.org/cl/35122 mentions this issue.

@randall77

This comment has been minimized.

Contributor

randall77 commented Jan 12, 2017

Same deal on 1.8.

It works if B is an int instead of a string. That's a bug with strings - we could treat constant strings as static.

For non-constant initialization we allocate a temp, initialize it, then write it to the destination. We shouldn't have to do that.

Let's make this bug about the latter. I'll open a separate one for the former.

@dsnet

This comment has been minimized.

Member

dsnet commented Jan 12, 2017

I didn't realize that strings made a difference. See the changes made in deflatefast.go in http://golang.org/cl/35122 for the specific use-case that was causing trouble.

@dsnet

This comment has been minimized.

Member

dsnet commented Jan 12, 2017

It seems the culprit is due to populating the field for a slice.

gopherbot pushed a commit that referenced this issue Jan 12, 2017

compress/flate: avoid large stack growth in fillDeflate
Ranging over an array causes the array to be copied over to the
stack, which cause large re-growths. Instead, we should iterate
over slices of the array.

Also, assigning a large struct literal uses the stack even
though the actual fields being populated are small in comparison
to the entirety of the struct (see #18636).

Fixing the stack growth does not alter CPU-time performance much
since the stack-growth and copying was such a tiny portion of the
compression work:

name                         old time/op    new time/op    delta
Encode/Digits/Default/1e4-8     332µs ± 1%     332µs ± 1%   ~     (p=0.796 n=10+10)
Encode/Digits/Default/1e5-8    5.07ms ± 2%    5.05ms ± 1%   ~       (p=0.815 n=9+8)
Encode/Digits/Default/1e6-8    53.7ms ± 1%    53.9ms ± 1%   ~     (p=0.075 n=10+10)
Encode/Twain/Default/1e4-8      380µs ± 1%     380µs ± 1%   ~     (p=0.684 n=10+10)
Encode/Twain/Default/1e5-8     5.79ms ± 2%    5.79ms ± 1%   ~      (p=0.497 n=9+10)
Encode/Twain/Default/1e6-8     61.5ms ± 1%    61.8ms ± 1%   ~     (p=0.247 n=10+10)

name                         old speed      new speed      delta
Encode/Digits/Default/1e4-8  30.1MB/s ± 1%  30.1MB/s ± 1%   ~     (p=0.753 n=10+10)
Encode/Digits/Default/1e5-8  19.7MB/s ± 2%  19.8MB/s ± 1%   ~       (p=0.795 n=9+8)
Encode/Digits/Default/1e6-8  18.6MB/s ± 1%  18.5MB/s ± 1%   ~     (p=0.072 n=10+10)
Encode/Twain/Default/1e4-8   26.3MB/s ± 1%  26.3MB/s ± 1%   ~     (p=0.616 n=10+10)
Encode/Twain/Default/1e5-8   17.3MB/s ± 2%  17.3MB/s ± 1%   ~      (p=0.484 n=9+10)
Encode/Twain/Default/1e6-8   16.3MB/s ± 1%  16.2MB/s ± 1%   ~     (p=0.238 n=10+10)

Updates #18636
Fixes #18625

Change-Id: I471b20339bf675f63dc56d38b3acdd824fe23328
Reviewed-on: https://go-review.googlesource.com/35122
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
@josharian

This comment has been minimized.

Contributor

josharian commented May 18, 2017

That program passes at tip (85b2940). @dsnet is there anything else you'd like to do here?

@dsnet

This comment has been minimized.

Member

dsnet commented May 18, 2017

If my repro passes at tip, then I'm happy. I'll file another issue if I run into anything. Thanks!

@dsnet dsnet closed this May 18, 2017

gopherbot pushed a commit that referenced this issue May 18, 2017

test: add test for issue 18636
Updates #18636

Change-Id: I143c670c3940231e29f1814e0a03165682f53243
Reviewed-on: https://go-review.googlesource.com/43621
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>

@dsnet dsnet reopened this May 18, 2017

@dsnet

This comment has been minimized.

Member

dsnet commented May 18, 2017

Re-opening as this still occurs with a minor variation of the above code:

type Foo struct {
	A [1 << 20]byte
	B []byte
}

func run(c chan bool) {
	f := new(Foo)
	*f = Foo{B: f.B[:0]}
	c <- true
}

func main() {
	debug.SetMaxStack(1 << 16)
	c := make(chan bool)
	go run(c)
	<-c
}
@gopherbot

This comment has been minimized.

gopherbot commented May 18, 2017

CL https://golang.org/cl/43621 mentions this issue.

@bradfitz bradfitz modified the milestones: Go1.9Maybe, Go1.10 Jul 20, 2017

@bradfitz bradfitz modified the milestones: Go1.10, Go1.11 Nov 28, 2017

@bradfitz bradfitz modified the milestones: Go1.11, Unplanned May 18, 2018

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