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

Prefer short circuit and membership test #11

Conversation

pjljvandelaar
Copy link

Dear HAC developers,
Dear Gautier de Montmollin,

Thinking about you request to change

if Code = SP_Open or Code = SP_Create or Code = SP_Append then

into

if Code in SP_Open | SP_Create | SP_Append then

I realized why we didn't have this find and replace pattern.

On my codebases, we always started with preferring short circuit operators.
See for more info adaic

When the code is changed and uses short circuit operators,
we apply the find-and-replace patterns to prefer membership tests.

So I exactly applied these two steps in succession on the hac archive.
And e.g. src/apps/hac_pkg.adb benefits from both!

Hopes this helps in making hac even better and easier to maintain!

Greetings,
Pierre

P.S. A pretty printer is applied to every rewrite to ensure that no warnings are introduced (such as line too long).
However not all your code seems to adhere to the pretty print settings as specified in your project file.

Problem detected and solved by Rejuvenation-Ada crate
vote for Rejuvenation-Ada as The 2022 Ada Crate Of The Year

if
((ND.TCB (Task_Entered).TS = WaitRendzv)
and then
(ND.TCB (Task_Entered).SUSPEND = Ident_Index_of_Called)) or
Copy link
Owner

Choose a reason for hiding this comment

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

Didn't the tool miss here an or else opportunity?

Copy link
Author

Choose a reason for hiding this comment

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

My suspicion is that the tool (conservatively) assumes that ND.TCB (Task_Entered).TS has a side effect,
and hence not always executing it (in case of "or else") will change the behaviour.

If I have time, I will double check!

if
((ND.TCB (Task_Entered).TS = WaitRendzv)
and then
(ND.TCB (Task_Entered).SUSPEND = Ident_Index_of_Called)) or
Copy link
Owner

Choose a reason for hiding this comment

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

Again a lost or else, seemingly...

Copy link
Author

Choose a reason for hiding this comment

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

See before!

if
((ND.TCB (Task_Entered).TS = WaitRendzv)
and then
(ND.TCB (Task_Entered).SUSPEND = Ident_Index_of_Called)) or
Copy link
Owner

Choose a reason for hiding this comment

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

Same pattern, same missed or else...

Copy link
Author

Choose a reason for hiding this comment

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

See before!

@@ -238,12 +238,13 @@ package body HAC_Sys.PCode.Interpreter.Tasking is
Pop (ND);
H2 := CD.Tasks_Definitions_Count + 1;
H3 := Integer (Random (ND.Gen) * Float (H2));
while H2 >= 0 and ND.TCB (H3).TS /= WaitSem and ND.TCB (H3).SUSPEND /= H1
while H2 >= 0 and ND.TCB (H3).TS /= WaitSem
Copy link
Owner

Choose a reason for hiding this comment

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

Here an and then could have been added, seemingly.

Copy link
Author

Choose a reason for hiding this comment

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

When ND.TCB (H3) is not a function call (with possible side effects) but just an array access, it can indeed be changed.

@zertovitch
Copy link
Owner

Integrated a good part of the suggest changes. Thanks! Commit: 8318810.

For cases like b1 or b2 where b1 and b2 are simple boolean variables, I have left the full evaluation.
The shortcuts introduce a branch (CPU-wise), or said otherwise, a hidden "if .. then .." which is not always a performance gain.
For cases where safety matters like suggested in the Ada 95 Quality and Style Guide, I already use the shortcut form.

@zertovitch zertovitch closed this Jun 10, 2023
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.

None yet

2 participants