Skip to content

Exact match for dependency - #36

Merged
BenMorris merged 8 commits into
BenMorris:masterfrom
siemens:ExactMatchForDependency
Mar 2, 2020
Merged

BenMorris merged 8 commits into
BenMorris:masterfrom
siemens:ExactMatchForDependency

Conversation

@AlexeyGorelenkov

Copy link
Copy Markdown
Contributor

Short description of the issue.

  • False positive detection of a dependency found; here is an example:
    var result = Types.InAssemblies(...) .ShouldNot().HaveDependencyOnAny(new[] {"Namespace.Rectangle"}) .GetResult();
    This predicate fails, if a type has dependency on the "Namespace.RectangleWithRoundCorners".
    Similarly, types from the "NamespaceTwo" are reported as dependencies from the "Namespace".
  • Current implementation of the DependencySearch class needs approx. 3 hours to scan 10000 source types against 12000 prohibited dependencies (yes, it was a sort of stress test for it).

Short description of the fix suggested.

  1. Right at the start full names of all dependencies are splitted into chains of names, e.g. Namespace.Rectangle from the example above produces the chain Namespace, Rectangle. That builds indexed tree of names.
  2. Instead of trying the string.StartsWith(...) method on each referenced type, its full name is also splitted, e.g. Namespace.RectangleWithRoundCorners ==> Namespace, RectangleWithRoundCorners.
  3. Each part is checked for presence in the indexed tree of names built at step 1. The result is positive only if there is terminated branch in the tree for the chain of names (see step 2).

Test cases

  • Suggested fix does not break any existing test case, i.e. currently covered scenarios are not affected by the fix.
  • There are test cases added which fail on false positive results of current implementation.
  • There are two performance test added to demonstrate algorithmic time complexity is reduced to (almost) linear.

P.S.: should I create more detailed issue for the project?

@BenMorris

Copy link
Copy Markdown
Owner

Hi Alexy,

Thanks for this. There's a lot going on in this pull request. I'll need to take some time to really understand the problems it is trying to solve before accepting it.

The false positives certainly need fixing. Good spot. However, is anybody out there really checking for 12,000 prohibited dependencies against 10,000 source types? It would be useful to know at which point this becomes genuinely problematic.

@AlexeyGorelenkov

Copy link
Copy Markdown
Contributor Author

Hi Ben,

Thank you for your reply to this pull request!

Unfortunately, yes, the example with 12,000 source types and 10,000 prohibited dependencies I've got from real life. The project I'm working on is more than 10 years old and you could imagine how much code base is.

It would be great, if we could prohibit whole namespaces, since it would drastically reduce the set of dependencies to scan (BTW, trying that I found those false positives). Bad luck for us: namespaces in our legacy are shared between assemblies and technical layers, so that we have to consider every single type.

Sure, we'd like to redesign it, but we need a tool to be able to formulate expected relations (e.g. dependencies, derivations etc.) and verify them and ideally every developer should be able to get a feedback as fast as possible. That is how the performance became a bottleneck for us.

So, solving the problem of false positives I also found that it improves the performance. Thus, on my laptop execution time of that stress test was reduced from 3 hours to 11 seconds.

Not sure, if anybody in the world (except our team) needs such an amount of source types and dependencies to work with, but for us it is the reality.

@@ -0,0 +1,193 @@
// Copyright (c) 2020 Siemens AG.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copyright by Siemens? 😄

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yes, this file is currently 100% Siemens code, given to the community under the terms of the MIT license, so all good 😃

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I'd prefer not to have copyright headers in individual files. Several reasons:

  • IMHO it's unnecessary - your copyright is automatic when you make the contribution and is tracked in the GitHub history.
  • It may actively discourage people from contributing. It implies that somebody "owns" the file, even though this is all under MIT. Copyright notices from large companies in particular can be off-putting.
  • Adding copyright notices to the top of source files doesn't really scale as an approach. What if half a dozen people make contributions to the file?
  • I would prefer a consistent approach - one person alone adding copyright notices does not foster clarity.

One of the better discussions I've seen on the subject is here: https://opensource.com/law/14/2/copyright-statements-source-files

@fgreinacher fgreinacher Feb 23, 2020 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I had the same remarks as you, but our legal team thinks it's better to have those copyright statements in the files. I'll talk to them again! I'm pretty sure we'll find a way that works for all of us, so feel free to continue reviewing the functional changes 👍

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you Flo!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

OK, we'll remove these copyright statements. Feel free to check out https://reuse.software/ to understand the reasoning for having copyright and licenses within source files and decide on your own what to do with this information 👍

@AlexeyGorelenkov AlexeyGorelenkov Feb 24, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Source branch for the pull request (e.g. siemens:ExactMatchForDependency) is now up-to-date: all copyright notices are removed.

@AlexeyGorelenkov

Copy link
Copy Markdown
Contributor Author

Merge conflicts with recent changes on master branch are now resolved.

@BenMorris

Copy link
Copy Markdown
Owner

I've finally had the time to sit down with this properly and understand the change.

It's a significant improvement - thanks very much for the code. It does add complexity to dependency searching, but the scalability gain is worth it IMHO. It also reduces the reliance on slightly sketchy string pattern matching by separating out each part of the namesapce.

Thanks very much for removing the copyright notices - I recognise that this is one of those rather nuanced debates with no clear consensus.

I'll release a new NuGet package tomorrow as there have been a few changes and bug fixes since the last release.

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.

4 participants