Skip to content
This repository has been archived by the owner on Jan 11, 2019. It is now read-only.

fix concurrent map write issue by init all regexp when startup #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

aiquestion
Copy link

this p-r is to fix #19

please help to review it when you get a chance. thx~

i put layoutRegexpMap to fieldDescriptor, and use fieldDescriptor's pointer.
ut passed.
run benchmark, no obvious performance lose.
BenchMark Before:
BenchmarkParse-8 100000 16959 ns/op 4553 B/op 65 allocs/op
BenchmarkNext-8 300000 4873 ns/op 453 B/op 19 allocs/op

After:
BenchmarkParse-8 100000 17210 ns/op 4553 B/op 65 allocs/op
BenchmarkNext-8 300000 4890 ns/op 453 B/op 19 allocs/op

@elwinar
Copy link

elwinar commented Jan 6, 2017

Hi @gorhill. I know that you're not working on this anymore, but I would love to see this fixed one way or another as I'm using your lib and my tests are complaining about that. I didn't inspect the issue too deep when I saw that someone did it already, but if you don't have much time I would love to review it for you.

Cheers,
R;

@elwinar
Copy link

elwinar commented Jan 6, 2017

BTW, @aiquestion, you say in #20 that double-checked locks in golang aren't safe. I'm not aware of this so I would love to see an example, and anyway your PR didn't include a double-checked lock.

@aiquestion
Copy link
Author

@elwinar as golang doc : https://golang.org/ref/mem.
and this blog: http://blog.launchdarkly.com/golang-pearl-thread-safe-writes-and-double-checked-locking-in-go/
i need to use sync/atomic to load and set checked value in double checked locking. So i choose the suggested way once.Do(). :-) this function is a correct implementation of double checked locking :-)

but actually my previous p-r seems to have another problem: if a goroutine is setting the value, while another goroutine tries to read and check the value, a panic will still occurs. golang map doesn't not allow read and write at the same time.

@elwinar
Copy link

elwinar commented Jan 7, 2017 via email

@aiquestion
Copy link
Author

ah. you are right.
I think i wrote a java-pattern double checked locking in my previous p-r which is wrong in golang.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

multi-goroutine map write when init first time
2 participants