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

Promise内存布局优化,以及内存固定bug #143

Open
lee157953 opened this issue Nov 8, 2023 · 1 comment
Open

Promise内存布局优化,以及内存固定bug #143

lee157953 opened this issue Nov 8, 2023 · 1 comment

Comments

@lee157953
Copy link

lee157953 commented Nov 8, 2023

Promise由于其特殊性,必须是pin的,不能够移动在内存中的位置,所以new函数返回一个Promise指针依旧有可能不安全,且sync.Mutex和sync.WaitGroup虽然为了内存不可移动声明为指针类型,但是占用了更多内存,且*sync.Mutex是不必要的,建议将代码修改为如下布局,更安全,并节约资源

type Promise[T any] struct {
	*promise[T]
}

type promise[T any] struct {
	runnable func(resolve func(T), reject func(error))
	result   T
	err      error
	wait     sync.WaitGroup
	pending  atomic.Bool
}
@duke-git
Copy link
Owner

duke-git commented Nov 9, 2023

Promise由于其特殊性,必须是pin的,不能够移动在内存中的位置,所以new函数返回一个Promise指针依旧有可能不安全,且_sync.Mutex和_sync.WaitGroup虽然为了内存不可移动声明为指针类型,但是占用了更多内存,且*sync.Mutex是不必要的,建议将代码修改为如下布局,更安全,并节约资源

type Promise[T any] struct {
	*promise[T]
}

type promise[T any] struct {
	runnable func(resolve func(T), reject func(error))
	result   T
	err      error
	wait       sync.WaitGroup
	pending  atomic.Bool
}

@lee157953 赞👍,可以提个pr.

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