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

Circular buffer should use conditional statement instead of modulo #7890

Merged
merged 2 commits into from
Sep 27, 2018

Conversation

deepikabhavnani
Copy link

Description

  1. Resolves: Circular buffer should use if statement instead of % to handle index overruns #7701
  2. Moved Circular buffer test to platform folder

Pull request type

[X] Fix
[X] Refactor
[ ] Target update
[ ] Functionality change
[ ] Breaking change

@cmonr cmonr requested a review from a team August 25, 2018 02:41
Copy link
Contributor

@0xc0170 0xc0170 left a comment

Choose a reason for hiding this comment

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

AStyle CI review (fix needed)

@deepikabhavnani
Copy link
Author

@SenRamakri @geky - Please review

Copy link
Contributor

@kegilbert kegilbert left a comment

Choose a reason for hiding this comment

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

LGTM!

@cmonr
Copy link
Contributor

cmonr commented Sep 26, 2018

...huh. Kinda surprised to find that we're using modulo, since I would expect masking arythmetic to be used for this kind of rollover. Still, an if statement is a bit of an improvement.

/morph build

@mbed-ci
Copy link

mbed-ci commented Sep 26, 2018

Build : SUCCESS

Build number : 3158
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/7890/

Triggering tests

/morph test
/morph export-build
/morph mbed2-build

@mbed-ci
Copy link

mbed-ci commented Sep 26, 2018

@mbed-ci
Copy link

mbed-ci commented Sep 26, 2018

@geky
Copy link
Contributor

geky commented Sep 26, 2018

...huh. Kinda surprised to find that we're using modulo, since I would expect masking arythmetic to be used for this kind of rollover. Still, an if statement is a bit of an improvement.

In this case BufferSize is a compile-time constant (template). So the compiler can trivially turn it into a mask when it's a power of 2.

@cmonr
Copy link
Contributor

cmonr commented Sep 26, 2018

06:46:19 FATAL: java.io.IOException: Unexpected termination of the channel

Wow. It's been a while since we last saw something like this.

/morph export

@cmonr
Copy link
Contributor

cmonr commented Sep 26, 2018

In this case BufferSize is a compile-time constant (template). So the compiler can trivially turn it into a mask when it's a power of 2.

Gotcha. Is the compiler still able to do the optimization with this PR change?

@geky
Copy link
Contributor

geky commented Sep 26, 2018

Nope: https://godbolt.org/z/aYf-MO

Though note how much worse it gets when you change SIZE to a non-power of two.

Note that is we really wanted to, we could have a template specialization for powers of two, and a template specialization for non-powers of two. But this may take much much more effort than it's really worth.

@pan-
Copy link
Member

pan- commented Sep 26, 2018

@cmonr No the compiler doesn't apply the optimisation: https://godbolt.org/z/yEKolO .
However the proposed change is much better when the buffer size is not a power of 2: https://godbolt.org/z/uDriqd

Edit: As @geky pointed out there's performance salvation with partial specialisation however it'll be maintenance hell for uninitiated programmers.

@0xc0170
Copy link
Contributor

0xc0170 commented Sep 27, 2018

/morph export-build

@mbed-ci
Copy link

mbed-ci commented Sep 27, 2018

@cmonr cmonr merged commit 4403a56 into ARMmbed:master Sep 27, 2018
@deepikabhavnani deepikabhavnani deleted the cb_issue_7701 branch September 27, 2018 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants