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

Seqlock implementation extract #664

Merged
merged 1 commit into from
Dec 28, 2023

Conversation

nolan-veed
Copy link
Contributor

Why

For #599

What

  • Created a SeqlockImpl class and used it in SeqLock.

Testing

There are tests.

Copy link

🤖 Upon creation, pull request description does not have a link to an issue. If there is a related issue, please add it to the description using any of the supported formats.

@nolan-veed
Copy link
Contributor Author

For some reason, my tests are hanging here...
image

@nolan-veed
Copy link
Contributor Author

nolan-veed commented Dec 27, 2023

For some reason, my tests are hanging here...

Oh wait... the Timer is using Seqlock, so it's clearly uncovered a bug in my change.

@gavv gavv added ready for review Pull request can be reviewed contribution A pull-request by someone else except maintainers labels Dec 28, 2023
Copy link
Member

@gavv gavv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Comment on lines 49 to 50
void* val_;
size_t val_size_;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we will store pointer and size in members, then every call (try_store, try_load, etc) will involve two additional reads from memory, and the whole operation will depend on these reads.

If, instead, we will pass &val and sizeof(val) to these methods as arguments, then chances are that no memory reads are needed: &val can be calculated as this + offset, and this is likely already in register, and sizeof(val) is a constant. And then on many architectures like x86_64 ARM function arguments will be passed via registers.

In our code base Seqlock is often used for small frequently used variables, like counters. E.g. seqlock is a replacement for 64-bit atomics (because 64-bit atomics are not available everywhere). So it makes sense to keep seqlock overhead low and comparable with a regular atomic.

Comment on lines 44 to 45
static void
volatile_copy_(volatile void* dst, const volatile void* src, size_t val_size);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can move this method into .cpp file in anon namespace.

Comment on lines 9 to 11
#include "roc_core/seqlock_impl.h"

#include "roc_core/atomic_ops.h"
#include "roc_core/cpu_instructions.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: extra empty line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh... we don't want to include the header related to the cpp file first?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, it was intentional. Yes, current "defacto" code style uses:

<one block with stdlib headers>

<one block with external libs headers>

<one block with roc headers>

e.g.:

#include <stdio.h>
#include <stdlib.h>

#include <uv.h>

#include "roc_core/array.h"
#include "roc_core/time.h"

@gavv gavv added needs revision Pull request should be revised by its author and removed ready for review Pull request can be reviewed labels Dec 28, 2023
@nolan-veed nolan-veed requested a review from gavv December 28, 2023 16:21
@github-actions github-actions bot added ready for review Pull request can be reviewed and removed needs revision Pull request should be revised by its author labels Dec 28, 2023
@gavv gavv added this to the next milestone Dec 28, 2023
@gavv gavv force-pushed the 599-SeqLock-extract branch from 26f90d6 to f8e31b9 Compare December 28, 2023 20:28
@gavv gavv merged commit 49137bc into roc-streaming:develop Dec 28, 2023
@github-actions github-actions bot removed the ready for review Pull request can be reviewed label Dec 28, 2023
@gavv
Copy link
Member

gavv commented Dec 28, 2023

Thanks!

Small refactoring: 3ebb8c8 (see commit message for details)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution A pull-request by someone else except maintainers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants