Remove synchronization of JyAttribute.getAttr to improve performance in multithreaded scenarios.#361
Remove synchronization of JyAttribute.getAttr to improve performance in multithreaded scenarios.#361Stewori wants to merge 3 commits into
Conversation
jeff5
left a comment
There was a problem hiding this comment.
It all hangs on the correctness of the argument. I find these things hard to convince myself of from just the theory.
An example of a test that arranges intense thread competition is this one I wrote to hammer the (prospective) type system. There was a risk, if I hadn't got it right, of deadlock or publication of half-finished objects.
The time measurements are made to prove the order of events rather than to measure performance. Synchronisation is explicit, so I'm not too worried about code re-ordering in my case.
The claim that the reader sees a coherent state as last written would be easier to accept if setAttr were to make a new list, in which everything could be constructed final, and only the head pointer would need to be volatile (and the nonBuiltin* counters). Assuming a setAttr is much less common than a getAttr, and lists are mostly short, the copy would not be expensive. You are best placed to say whether that's true.
|
Before we think about writing a stress test I suggest to await response of @newprogrammer101 . If the PR does not solve the issue or blows something up, we would need to rethink the solution anyway. |
fayazkhan121
left a comment
There was a problem hiding this comment.
if (nonBuiltinAttrTypeOffset == 0) { throw new IllegalStateException("No more attr types available."); }
Magic numbers are being used in conditional checks.
Recommendation: Define these values as constants to improve code readability and maintainability
public static byte reserveCustomAttrType() {
public static synchronized byte reserveCustomAttrType() {
The method reserveCustomAttrType has been changed to a synchronised method. This is a good practice for thread safety, but it introduces a potential bottleneck under heavy load.
Recommendation: Assess if finer-grained synchronisation (e.g., using atomic variables) could replace the synchronisation here to improve concurrency without sacrificing thread safety.
Thanks @Stewori for the quick change in this CR. This commit is to resolve concurrency issue on Jython package. But currently when I created multi JythonExecutor instance for testing purpose, I found CPU was at BLOCK state on |
|
I finally took the time to stress test the implementation and it works like expected. Generally, the implementation performs nicely. Trouble is, it also performs nicely without using With that, I'm now even more confident in this PR and think it should be included into the 2.7.5 release. |
|
Okay, And no violations means that the test passed. The number of items for each key indicates how many accesses to the dict have been recorded and evaluated for consistency. Then recompile ( which confirms that the test is sharp. In all tries so far (perhaps ~50) I have not encountered a single failure with |
|
Ok, I believe. Since we first considered this I have been back to the textbooks on this difficult subject. I see how Do you think this should be a test in |
Please, share more details. What other context?
No, it is too resource hungry and too non-telling. I think the stress level for a meaningful test is machine dependent and one would have to assert in non-volatile version that the test triggers. To know that the stress is meaningful. I don't see how this check could be done on a regular basis in normal tests. In fact like one in ten times it crashes my JVM with out-of-memory error. Maybe it would work better if the number of calls is limited rather than the time. Because the code that terminates it after the specified time lags behind due to stress. OTOH, if this is relieved, we might stop seeing the failures again. It would be tricky to tune the test to reliably trigger a violation on every machine. Especially without wasting too many resources. Perhaps it could be added as optional test. We should definitely make it available for off-stream testing and investigations. However, maybe it's enough to just link to my previous post from the source code (perhaps even from a doc comment) in |
I think this is resolved by better understanding and practical tests.
Fixes #360