jsakamoto / ipaddressrange

.NET Class Library for range of IP address, both IPv4 and IPv6.
Mozilla Public License 2.0
368 stars 71 forks source link

Comparison performance leaks in Contains #38

Open Menur opened 6 years ago

Menur commented 6 years ago

NetTools.Bits static class is nice example of LINQ usage, but it's really slow.

Possible improvement ideas:

  1. You can get like 33% less time spent on this when you use for cycles.
  2. You can even optimize IPv6 comparison for IPv4ToIPv6 mapped addresses to reduce number of iterations from 16 to 4.
  3. You can completely avoid some comparisons for IPv6 by checking IsIPv4MappedToIPv6
  4. For the IPv4 address family you can use IPAddress.GetHashCode() which is 32-bit number as ip address. Casting hash code to uint would be still faster than the byte array comparison.
jsakamoto commented 6 years ago

Thank you for your advices!

Could you make those advices as a real code, and send to me as pull requests?

I'll not have enough time to spent improving this library in these days.
If you send pull requests, it will be helpful for the community, I think.

I can't promise that merge your pull requests certainly at this time, but I'll consider well.

jsakamoto commented 6 years ago

I published v.3.2.0 package.

Thanks @Menur 👍