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

Volatile Write around Sequence field #4

Merged
merged 3 commits into from
Jan 24, 2018
Merged

Conversation

alexandrnikitin
Copy link
Owner

No description provided.

@alexandrnikitin
Copy link
Owner Author

@lust4life, WDYT about this fix? When we just Volatile.Write around the Sequence field.

@lust4life
Copy link
Contributor

        [StructLayout(LayoutKind.Explicit, Size = 16, CharSet = CharSet.Ansi)]
        private struct Cell
        {
            [FieldOffset(0)]
            public int Sequence;
            [FieldOffset(8)]
            public object Element;

hi @alexandrnikitin , do we need this explicit structlayout in cell struct ? how about leave the compile generate for it (so it can be auto aligned)?

@alexandrnikitin
Copy link
Owner Author

Same reason as with var buffer = _buffer; in enqueue/dequeue. We always read Sequence first. If it's on the edge of a cache line, then hopefully prefetch can help with Element. But it's my speculation mostly, I don't have enough evidence. It's not visible in microbenchmarks as everything in L1 already.

When JIT alignes it puts Element first:

Type layout for 'CellAuto'
Size: 16 bytes. Paddings: 4 bytes (%25 of empty space)
|=================================|
|   0-7: Object Element (8 bytes) |
|---------------------------------|
|  8-11: Int32 Sequence (4 bytes) |
|---------------------------------|
| 12-15: padding (4 bytes)        |
|=================================|


Type layout for 'CellLayoutKindExplicit'
Size: 16 bytes. Paddings: 4 bytes (%25 of empty space)
|=================================|
|   0-3: Int32 Sequence (4 bytes) |
|---------------------------------|
|   4-7: padding (4 bytes)        |
|---------------------------------|
|  8-15: Object Element (8 bytes) |
|=================================|

Volatile Write around Sequence field
@alexandrnikitin alexandrnikitin merged commit 3750330 into master Jan 24, 2018
@alexandrnikitin alexandrnikitin deleted the volatile-write branch January 24, 2018 09:41
@lust4life
Copy link
Contributor

get it , thanks. 👍

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.

2 participants