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

perf: use index access to Dictionary #1621

Merged
merged 6 commits into from
May 4, 2024

Conversation

Gounlaf
Copy link
Contributor

@Gounlaf Gounlaf commented May 1, 2024

Description

👋

Theses method doesn't use the result of TryGetValue and expect a non-null result:

public IChannelMoments GetChannel(PixelChannel channel)
{
_channels.TryGetValue(channel, out var moments);
return moments;
}

public IChannelPerceptualHash GetChannel(PixelChannel channel)
{
_channels.TryGetValue(channel, out var perceptualHash);
return perceptualHash;
}

This one can - explicitly - return a null value:

public IChannelStatistics? GetChannel(PixelChannel channel)
{
_channels.TryGetValue(channel, out var channelStatistics);
return channelStatistics;
}

This PR:

  • remove the TryGetValue for the first too method and use index access
  • tests are added accordingly
  • documentation is not (yet) updated (don't know if it's necessary)

If otherwise you prefer to not change the actual behavior (as it might be a breaking change, or just because the nullable returns is the wanted behavior), I will update the PR:

  • add tests
  • add nullable indicator to method return type

Regards.

Copy link
Owner

@dlemstra dlemstra left a comment

Choose a reason for hiding this comment

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

Wondering if we should not change the interface instead? Als a bit surprised this doesn't cause a warning?

@@ -37,10 +37,7 @@ public IChannelMoments Composite()
/// <param name="channel">The channel to get the moments for.</param>
/// <returns>The moments for the specified channel.</returns>
public IChannelMoments GetChannel(PixelChannel channel)
Copy link
Owner

Choose a reason for hiding this comment

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

Should we not change the interface here instead and do IChannelMoments?

src/Magick.NET/Statistics/PerceptualHash.cs Outdated Show resolved Hide resolved
public class TheGetChannelMethod
{
[Fact]
public void ShouldThrowExceptionWhenChannelDoesNotExist()
Copy link
Owner

Choose a reason for hiding this comment

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

This name is no longer correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup; renamed.

public class TheGetChannelMethod
{
[Fact]
public void ShouldThrowExceptionWhenChannelDoesNotExist()
Copy link
Owner

Choose a reason for hiding this comment

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

Same here :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed.

using ImageMagick;
using Xunit;

namespace Magick.NET.Tests;

public partial class PerceptualHashTests
{
public class TestPerceptualHash : IPerceptualHash
Copy link
Owner

Choose a reason for hiding this comment

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

I will move this to a separate file after merging this PR. Not yet sure where I want to put this. And maybe we could use NSubstitute instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm used to anonymous object that can implements an Interface in PHP or Kotlin 😜

I don't know NSubstitute (this one?), but I could use it instead of this Test class.

{
public IChannelPerceptualHash GetChannel(PixelChannel channel)
{
return null;
Copy link
Owner

Choose a reason for hiding this comment

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

Please use => null instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

using var image = new MagickImage(Files.ImageMagickJPG);
var phash = image.PerceptualHash();

Assert.Throws<NotSupportedException>(() => phash.SumSquaredDistance(new TestPerceptualHash()));
Copy link
Owner

Choose a reason for hiding this comment

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

You can also check the message of the exception like we do in other places.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added.

@Gounlaf
Copy link
Contributor Author

Gounlaf commented May 3, 2024

I can't rerun the pipeline; it seems stuck like "last time", not because of code, but for some other reasons

@dlemstra
Copy link
Owner

dlemstra commented May 4, 2024

You will probably need to rebase your PR?

@dlemstra dlemstra merged commit 417f8b4 into dlemstra:main May 4, 2024
26 checks passed
@Gounlaf Gounlaf deleted the opti-trygetvalue branch May 4, 2024 18:42
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