-
Notifications
You must be signed in to change notification settings - Fork 274
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
Make bundle safe for concurrent writes #54
Comments
Thanks for reporting this. You are correct go-i18n is not threadsafe. This is alluded to in a few locations in the docs, but perhaps it could be more explicit. In the i18n.go package documentation:
And in the doc for
Can you describe what your use-case is for concurrently reading and adding translations? |
my applicated loads default translations at init as well as customized translations from the database. its not my intention to concurrently load translations. it just seems to be a symptom of the design of my app. for now i have added a mutex around calls to AddTranslation. i will submit a pr if i have some time. |
@nicksnyder please take a look at my pr #59. benchmarks seem about the same. |
I have the same issue, but I'm indirectly concerned, this project come from one of my repository . I have made an issue linked to this one, I hope it can help to have some other feedbacks and usecases to make this issue fixed. Let me know @nicksnyder if you need more context |
@emosbaugh Can you post before and after benchmark results? |
@nicksnyder I did it for him to help a little. Before the change for concurrent writes
After the change
As I expected it seems that his changes will have no impact (the result even show that it's faster with the changes but I just ran benchmark once, I consider that we will have the same time in both case). The only impact we can have it's when concurrent writing occurred. Hope it helps. |
Seems on par with the current benchmarks. For me they run a bit slower although could be due to outside factors as i am running on my mac im using for dev. $ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
$ go test ./... -bench=. -cpu=1,2
PASS
ok github.com/nicksnyder/go-i18n/goi18n 0.019s
PASS
ok github.com/nicksnyder/go-i18n/i18n 0.007s
PASS
BenchmarkTranslateNonPluralWithMap 1000000 1254 ns/op
BenchmarkTranslateNonPluralWithMap-2 1000000 1174 ns/op
BenchmarkTranslateNonPluralWithStruct 1000000 1660 ns/op
BenchmarkTranslateNonPluralWithStruct-2 1000000 1495 ns/op
BenchmarkTranslateNonPluralWithStructPointer 1000000 1731 ns/op
BenchmarkTranslateNonPluralWithStructPointer-2 1000000 1589 ns/op
BenchmarkTranslatePluralWithMap 1000000 1670 ns/op
BenchmarkTranslatePluralWithMap-2 1000000 1565 ns/op
BenchmarkTranslatePluralWithMapAndCountField 1000000 1542 ns/op
BenchmarkTranslatePluralWithMapAndCountField-2 1000000 1425 ns/op
BenchmarkTranslatePluralWithStruct 1000000 2069 ns/op
BenchmarkTranslatePluralWithStruct-2 1000000 1959 ns/op
BenchmarkTranslatePluralWithStructPointer 1000000 2105 ns/op
BenchmarkTranslatePluralWithStructPointer-2 1000000 2000 ns/op
ok github.com/nicksnyder/go-i18n/i18n/bundle 23.513s
PASS
BenchmarkNewOperand 5000000 373 ns/op
BenchmarkNewOperand-2 5000000 342 ns/op
ok github.com/nicksnyder/go-i18n/i18n/language 4.338s
? github.com/nicksnyder/go-i18n/i18n/language/codegen [no test files]
PASS
BenchmarkExecuteNilTemplate 1000000000 2.64 ns/op
BenchmarkExecuteNilTemplate-2 1000000000 2.63 ns/op
BenchmarkExecuteHelloWorldTemplate 1000000000 2.63 ns/op
BenchmarkExecuteHelloWorldTemplate-2 1000000000 2.62 ns/op
BenchmarkExecuteHelloNameTemplate 1000000 1026 ns/op
BenchmarkExecuteHelloNameTemplate-2 2000000 946 ns/op
BenchmarkSprintf 10000000 203 ns/op
BenchmarkSprintf-2 10000000 181 ns/op
ok github.com/nicksnyder/go-i18n/i18n/translation 19.727s
? github.com/nicksnyder/go-i18n/vendor/gopkg.in/yaml.v2 [no test files]
$ git checkout concurrent-bundle
Switched to branch 'concurrent-bundle'
$ go test ./... -bench=. -cpu=1,2
PASS
ok github.com/nicksnyder/go-i18n/goi18n 0.019s
PASS
ok github.com/nicksnyder/go-i18n/i18n 0.007s
PASS
BenchmarkTranslateNonPluralWithMap 1000000 1335 ns/op
BenchmarkTranslateNonPluralWithMap-2 1000000 1236 ns/op
BenchmarkTranslateNonPluralWithStruct 1000000 1712 ns/op
BenchmarkTranslateNonPluralWithStruct-2 1000000 1608 ns/op
BenchmarkTranslateNonPluralWithStructPointer 1000000 1845 ns/op
BenchmarkTranslateNonPluralWithStructPointer-2 1000000 1806 ns/op
BenchmarkTranslatePluralWithMap 1000000 1978 ns/op
BenchmarkTranslatePluralWithMap-2 1000000 1652 ns/op
BenchmarkTranslatePluralWithMapAndCountField 1000000 1683 ns/op
BenchmarkTranslatePluralWithMapAndCountField-2 1000000 1613 ns/op
BenchmarkTranslatePluralWithStruct 1000000 2239 ns/op
BenchmarkTranslatePluralWithStruct-2 1000000 2301 ns/op
BenchmarkTranslatePluralWithStructPointer 1000000 2368 ns/op
BenchmarkTranslatePluralWithStructPointer-2 500000 2317 ns/op
ok github.com/nicksnyder/go-i18n/i18n/bundle 24.832s
PASS
BenchmarkNewOperand 5000000 386 ns/op
BenchmarkNewOperand-2 5000000 359 ns/op
ok github.com/nicksnyder/go-i18n/i18n/language 4.495s
? github.com/nicksnyder/go-i18n/i18n/language/codegen [no test files]
PASS
BenchmarkExecuteNilTemplate 1000000000 2.88 ns/op
BenchmarkExecuteNilTemplate-2 1000000000 2.73 ns/op
BenchmarkExecuteHelloWorldTemplate 1000000000 2.67 ns/op
BenchmarkExecuteHelloWorldTemplate-2 1000000000 2.81 ns/op
BenchmarkExecuteHelloNameTemplate 1000000 1113 ns/op
BenchmarkExecuteHelloNameTemplate-2 1000000 1041 ns/op
BenchmarkSprintf 10000000 201 ns/op
BenchmarkSprintf-2 10000000 203 ns/op
ok github.com/nicksnyder/go-i18n/i18n/translation 18.801s
? github.com/nicksnyder/go-i18n/vendor/gopkg.in/yaml.v2 [no test files] |
Thanks for the perf results. Here is the comparison using benchcmp. Some noticeable slowdown but in absolute terms it is still very small. From @ArthurHlt 's benchmarks:
From @emosbaugh 's benchmarks
|
Problem
Adding to the bundle in a concurrent program causes panics
Resolution
Make bundle safe for concurrent writes
Stack trace
The text was updated successfully, but these errors were encountered: