Skip to content

Refactor multiply operation for better performance in 2 Commands.Utility functions#14148

Merged
anmenaga merged 5 commits into
PowerShell:masterfrom
xtqqczze:eliminate-mul
Dec 8, 2020
Merged

Refactor multiply operation for better performance in 2 Commands.Utility functions#14148
anmenaga merged 5 commits into
PowerShell:masterfrom
xtqqczze:eliminate-mul

Conversation

@xtqqczze
Copy link
Copy Markdown
Contributor

@xtqqczze xtqqczze commented Nov 19, 2020

PR Summary

Factor out multiply operation for slightly more readable code.

Replace all instances of x * (y ? 1 : -1) with y ? x : -x.

PR Context

As shown in following micro-benchmark results, these changes do not worsen performance, there is in fact a very small absolute gain.

| Method |      Mean |     Error |    StdDev |    Median | Code Size | Gen 0 | Gen 1 | Gen 2 | Allocated |
|------- |----------:|----------:|----------:|----------:|----------:|------:|------:|------:|----------:|
|    Mul | 0.6288 ns | 0.0518 ns | 0.0836 ns | 0.5843 ns |      25 B |     - |     - |     - |         - |
|    Neg | 0.0358 ns | 0.0275 ns | 0.0496 ns | 0.0000 ns |      16 B |     - |     - |     - |         - |

PR Checklist

@ghost ghost assigned anmenaga Nov 19, 2020
@iSazonov
Copy link
Copy Markdown
Collaborator

@xtqqczze Not for the PR: could you please fix !(this is pattern in ProviderBase.cs.

@iSazonov
Copy link
Copy Markdown
Collaborator

iSazonov commented Nov 20, 2020

there is in fact a very small gain.

0.0358 ns vs 0.6288 ns small?

It seems there are other places where we use the multiply.

@iSazonov iSazonov added CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log CL-Performance Indicates that a PR should be marked as a performance improvement in the Change Log and removed CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log labels Nov 20, 2020
@xtqqczze
Copy link
Copy Markdown
Contributor Author

there is in fact a very small gain.

0.0358 ns vs 0.6288 ns small?

Large relative gain but small absolute gain (in micro-benchmark).

It seems there are other places where we use the multiply.

This PR replaces all instances of the x * (y ? 1 : -1) pattern.

@iSazonov
Copy link
Copy Markdown
Collaborator

This PR replaces all instances of the x * (y ? 1 : -1) pattern.

See private object Compare(object objValue, object statMinOrMaxValue, bool isMin) in Measure-Object.
I guess we have others. I don't ask you to find all and fix but it would be great.

@xtqqczze xtqqczze force-pushed the eliminate-mul branch 2 times, most recently from 9a42619 to 154e218 Compare November 20, 2020 16:09
@anmenaga
Copy link
Copy Markdown

How the measurements were done / results table in PR Context was created?

@xtqqczze
Copy link
Copy Markdown
Contributor Author

How the measurements were done / results table in PR Context was created?

Using BenchmarkDotNet:

...
        bool _ascendingOrder = false;
        int result = 7;

        [Benchmark]
        public int Mul()
        {
            return result * (_ascendingOrder ? 1 : -1);
        }

        [Benchmark]
        public int Neg()
        {
            return _ascendingOrder ? result : -result;
        }
...

Not the most realistic benchmark, but the results do show performance is not harmed by the changes.

Comment thread src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs Outdated
@xtqqczze xtqqczze changed the title Simplify complex return statement Refactor multiply operation Nov 24, 2020
@xtqqczze xtqqczze mentioned this pull request Nov 24, 2020
14 tasks
@ghost ghost added the Review - Needed The PR is being reviewed label Dec 1, 2020
@ghost
Copy link
Copy Markdown

ghost commented Dec 1, 2020

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@xtqqczze
Copy link
Copy Markdown
Contributor Author

xtqqczze commented Dec 1, 2020

@iSazonov Please could you approve changes.

@iSazonov
Copy link
Copy Markdown
Collaborator

iSazonov commented Dec 1, 2020

@iSazonov Please could you approve changes.

I did but since the PR changes code we need more reviewers.

@ghost ghost removed the Review - Needed The PR is being reviewed label Dec 1, 2020
Copy link
Copy Markdown

@anmenaga anmenaga left a comment

Choose a reason for hiding this comment

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

LGTM

@anmenaga anmenaga changed the title Refactor multiply operation Refactor multiply operation for better performance in 2 Commands.Utility functions Dec 8, 2020
@anmenaga anmenaga merged commit dd4e5c7 into PowerShell:master Dec 8, 2020
@xtqqczze xtqqczze deleted the eliminate-mul branch December 8, 2020 23:26
@iSazonov iSazonov added this to the 7.2.0-preview.2 milestone Dec 9, 2020
@ghost
Copy link
Copy Markdown

ghost commented Dec 15, 2020

🎉v7.2.0-preview.2 has been released which incorporates this pull request.:tada:

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-Performance Indicates that a PR should be marked as a performance improvement in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants