{"action":"created","issue":{"url":"https://api.github.com/repos/netty/netty/issues/4726","repository_url":"https://api.github.com/repos/netty/netty","labels_url":"https://api.github.com/repos/netty/netty/issues/4726/labels{/name}","comments_url":"https://api.github.com/repos/netty/netty/issues/4726/comments","events_url":"https://api.github.com/repos/netty/netty/issues/4726/events","html_url":"https://github.com/netty/netty/pull/4726","id":127396575,"number":4726,"title":"Guard against false-sharing when allocate from PooledByteBufAllocator","user":{"login":"normanmaurer","id":439362,"avatar_url":"https://avatars.githubusercontent.com/u/439362?v=3","gravatar_id":"","url":"https://api.github.com/users/normanmaurer","html_url":"https://github.com/normanmaurer","followers_url":"https://api.github.com/users/normanmaurer/followers","following_url":"https://api.github.com/users/normanmaurer/following{/other_user}","gists_url":"https://api.github.com/users/normanmaurer/gists{/gist_id}","starred_url":"https://api.github.com/users/normanmaurer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/normanmaurer/subscriptions","organizations_url":"https://api.github.com/users/normanmaurer/orgs","repos_url":"https://api.github.com/users/normanmaurer/repos","events_url":"https://api.github.com/users/normanmaurer/events{/privacy}","received_events_url":"https://api.github.com/users/normanmaurer/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":{"login":"normanmaurer","id":439362,"avatar_url":"https://avatars.githubusercontent.com/u/439362?v=3","gravatar_id":"","url":"https://api.github.com/users/normanmaurer","html_url":"https://github.com/normanmaurer","followers_url":"https://api.github.com/users/normanmaurer/followers","following_url":"https://api.github.com/users/normanmaurer/following{/other_user}","gists_url":"https://api.github.com/users/normanmaurer/gists{/gist_id}","starred_url":"https://api.github.com/users/normanmaurer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/normanmaurer/subscriptions","organizations_url":"https://api.github.com/users/normanmaurer/orgs","repos_url":"https://api.github.com/users/normanmaurer/repos","events_url":"https://api.github.com/users/normanmaurer/events{/privacy}","received_events_url":"https://api.github.com/users/normanmaurer/received_events","type":"User","site_admin":false},"milestone":null,"comments":10,"created_at":"2016-01-19T09:02:08Z","updated_at":"2016-03-15T14:31:44Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/netty/netty/pulls/4726","html_url":"https://github.com/netty/netty/pull/4726","diff_url":"https://github.com/netty/netty/pull/4726.diff","patch_url":"https://github.com/netty/netty/pull/4726.patch"},"body":"Motivation:\r\n\r\nBecause we not took care to allign allocations on the cacheline size it could produce false-sharing effects.\r\n\r\nModifications:\r\n\r\n- Ensure we correctly use a offset when slice out buffers so we not share cache lines\r\n\r\nResult:\r\n\r\nBetter performance\r\n\r\n```\r\nBenchmark                                                   (size)   Mode  Cnt       Score       Error  Units\r\nPooledByteBufAllocatorFalseSharingBenchmark.falseSharing     00512  thrpt   20  685296.357 ± 30092.225  ops/s\r\nPooledByteBufAllocatorFalseSharingBenchmark.falseSharing     01024  thrpt   20  370008.716 ±  3509.795  ops/s\r\nPooledByteBufAllocatorFalseSharingBenchmark.falseSharing     02048  thrpt   20  185731.575 ±  4806.616  ops/s\r\nPooledByteBufAllocatorFalseSharingBenchmark.falseSharing     04096  thrpt   20   90603.308 ±  3860.461  ops/s\r\nPooledByteBufAllocatorFalseSharingBenchmark.falseSharing     08192  thrpt   20   43513.344 ±  3038.603  ops/s\r\nPooledByteBufAllocatorFalseSharingBenchmark.noFalseSharing   00512  thrpt   20  719899.937 ± 10043.278  ops/s\r\nPooledByteBufAllocatorFalseSharingBenchmark.noFalseSharing   01024  thrpt   20  370943.544 ± 10816.982  ops/s\r\nPooledByteBufAllocatorFalseSharingBenchmark.noFalseSharing   02048  thrpt   20  188452.511 ±  4935.453  ops/s\r\nPooledByteBufAllocatorFalseSharingBenchmark.noFalseSharing   04096  thrpt   20   94369.012 ±  3636.229  ops/s\r\nPooledByteBufAllocatorFalseSharingBenchmark.noFalseSharing   08192  thrpt   20   47478.232 ±  2165.613  ops/s\r\n```"},"comment":{"url":"https://api.github.com/repos/netty/netty/issues/comments/196846217","html_url":"https://github.com/netty/netty/pull/4726#issuecomment-196846217","issue_url":"https://api.github.com/repos/netty/netty/issues/4726","id":196846217,"user":{"login":"buchgr","id":1388179,"avatar_url":"https://avatars.githubusercontent.com/u/1388179?v=3","gravatar_id":"","url":"https://api.github.com/users/buchgr","html_url":"https://github.com/buchgr","followers_url":"https://api.github.com/users/buchgr/followers","following_url":"https://api.github.com/users/buchgr/following{/other_user}","gists_url":"https://api.github.com/users/buchgr/gists{/gist_id}","starred_url":"https://api.github.com/users/buchgr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/buchgr/subscriptions","organizations_url":"https://api.github.com/users/buchgr/orgs","repos_url":"https://api.github.com/users/buchgr/repos","events_url":"https://api.github.com/users/buchgr/events{/privacy}","received_events_url":"https://api.github.com/users/buchgr/received_events","type":"User","site_admin":false},"created_at":"2016-03-15T14:31:44Z","updated_at":"2016-03-15T14:31:44Z","body":"@normanmaurer  \r\n\r\n- I would set the number of threads to 2. In this benchmark design, everything else will just introduce randomness and doesn't help.\r\n\r\n- On my system the addresses of the direct buffers are always 64 byte aligned anyway(with and without your code). You need to make sure in the setup method that the addresses are not aligned and actually overlap within a cache line.\r\n\r\n- I don't think that trick with incrementing an atomic integer helps. You are playing a statistical game and chances are the odds won't be in your favour. Also it looks to me that your benchmark is actually measuring the throughput of `AtomicInteger.getAndIncrement()` and not that of `ByteBuf.setByte()`. You are doing a CAS + 4 byte write for ever 1 byte write that you want to measure. Best guess, that CAS will be at least an order of magnitude more expensive than that write.\r\n\r\nTo be honest, I don't think \"protecting against false sharing\" is useful for ByteBufs. Pretty much all ByteBufs are thread-local. There is generally no opportunity for false sharing to happen.\r\n\r\njemalloc doesn't have this either. I recall a discussion where they played with it, but concluded that it's best that, if one runs into this, to just allocate a bigger buffer. \r\n\r\n"}}