英文:
Golang error function arguments too large for new goroutine
问题
我正在使用Go 1.4运行一个程序,尝试将一个大的结构体传递给一个Go函数。
我得到了这个错误:
runtime.newproc: function arguments too large for new goroutine
我已经改为通过引用传递,这有所帮助,但我想知道是否有一种方法可以在Go函数中传递大的结构体。
谢谢。
英文:
I am running a program with go 1.4 and I am trying to pass a large struct to a go function.
go ProcessImpression(network, &logImpression, campaign, actualSpent, partnerAccount, deviceId, otherParams)
I get this error:
runtime.newproc: function arguments too large for new goroutine
I have moved to pass by reference which helps but I am wondering if there is some way to pass large structs in a go function.
Thanks,
答案1
得分: 2
不,我不知道任何相关的信息。
我认为你不应该过于激进地进行调优以避免复制,但从源代码中可以看出,当参数超过可用的堆栈空间时,会发出此错误,这个空间应该是以千字节为单位的。在这一点上,复制开销是真实存在的,特别是如果这不是唯一一次复制这些东西的时候。也许某个结构体要么明确地比预期要大,这要归功于一个大的结构体成员(比如1kb的数组而不是切片),要么间接地比预期要大。如果不是这样的情况,那么像你所做的那样使用指针是有意义的,如果你担心创建垃圾,可以使用sync.Pool
来回收指向的结构体。
英文:
No, none I know of.
I don't think you should be too aggressive tuning to avoid copying, but it appears from the source that this error is emitted when parameters exceed the usable stack space for a new goroutine, which should be kilobytes. The copying overhead is real at that point, especially if this isn't the only time these things are copied. Perhaps some struct either explicitly is larger than expected thanks to a large struct member (1kb array rather than a slice, say) or indirectly. If not, just using a pointer as you have makes sense, and if you're worried about creating garbage, recycle the structs pointed to using sync.Pool
.
答案2
得分: 2
我能够通过将参数从
func doStuff(prev, next User)
更改为
func doStuff(prev, next *User)
来解决这个问题。@twotwotwo在这里的答案非常有帮助。
英文:
I was able to fix this issue by changing the arguments from
func doStuff(prev, next User)
to
func doStuff(prev, next *User)
The answer from @twotwotwo in here is very helpful.
答案3
得分: 0
在处理大结构体的值列表([]BigType
)时遇到了这个问题:
for _, stct := range listBigStcts {
go func(stct BigType) {
...处理 stct ...
}(stct) // < -- 错误发生在这里
}
解决方法是将 []BigType
替换为 []*BigType
。
英文:
Got this issue at processing list of values([]BigType
) of big struct:
for _, stct := range listBigStcts {
go func(stct BigType) {
...process stct ...
}(stct) // <-- error occurs here
}
Workaround is to replace []BigType
with []*BigType
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论