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

Use double checked locking to remove sync block #361

Merged
merged 1 commit into from
Dec 11, 2013

Conversation

stuartwdouglas
Copy link
Contributor

No description provided.

@cowtowncoder
Copy link
Member

You do know that double-locking is nearly universally condemned as an anti-pattern?
This case is not actually guaranteed to work, due to Java Memory Model not guaranteeing that changes would be visible without synchronization.

Beyond question of correctness, how much is expected improvement, and for what cases? Ideally there would be a benchmark to show performance improvement.

@stuartwdouglas
Copy link
Contributor Author

Pre Java 1.5 you would be correct. The Java memory model was fixed in Java 1.5, so now double checked locking works fine, as long as the variable is volatile. Most of the info on double checked locking if you just google for it is outdated, and refers to the old memory model before JSR 133.

There was also some FUD just after 1.5 about how synchronized was much cheaper now so it was still not necessary, however a volatile read is still much cheaper on most platforms. On server class systems with lots of processors all trying to acquire the lock it can become very expensive.

I can provide some numbers if you really want, but in general the more processors you have the bigger the difference it will make.

@cowtowncoder
Copy link
Member

Ah. I did not notice that you had used volatile correctly. My bad -- yes, usage is correct as of 1.5 and beyond.

I would be interested in just some validation; I assume that you saw lock contention and this is based on solving an actual problem. I just have occasionally received patches that have turned out to be more speculative, which is why I ask nowadays.

I think it'd be fine to just describe observed improvement (problem there was, how its solved), although of course numbers would be great if you already had some. But it is hard to get solid and meaningful numbers so I am ok without.

Does this make sense?

Also: thank you for the patch! There have been couple of other synchronization hot spots, and this one is on critical path (esp. when dealing with small objects), so it is not entirely surprising.

@stuartwdouglas
Copy link
Contributor Author

While profiling I observed an inordinate amount of time spent in the method, and YJP monitor profiling was showing contention.

I also just know from my general experience profiling Wildfly that even short held locks can have a big impact on performance if they are on a critical path that lots of threads are taking.

If you want numbers I should be able to get them early next week.

@cowtowncoder
Copy link
Member

Ok. The only concern I have is that sometimes profiled bottlenecks are more artifacts of the tool -- having said that, I don't think I have ever added harmful optimizations that way; at most things that may not have had same impact as what original snapshot would suggest.

But as I said this particular code path is something that I could see being in critical path.

If you can get the numbers, that'd be great. I will think about this a bit, and may just go ahead even before; depending on how fast I can process other contributions (there's been uptick on reports since 2.3.0, which is a good sign wrt adoption and getting new users).

@cowtowncoder
Copy link
Member

Actually I think this makes sense; and although I am still interested in numbers (if possible), will just integrate this in.

cowtowncoder added a commit that referenced this pull request Dec 11, 2013
Use double checked locking to remove sync block
@cowtowncoder cowtowncoder merged commit 767b662 into FasterXML:master Dec 11, 2013
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

Successfully merging this pull request may close these issues.

3 participants