phobos icon indicating copy to clipboard operation
phobos copied to clipboard

chore(sumtype): avoid quadratic memory complexity on opEquals

Open ljmf00 opened this issue 3 years ago • 6 comments

Signed-off-by: Luís Ferreira [email protected]


Due to match template being quadratic/exponential in terms of memory complexity, (-vtemplate reports a rediculous amount of templates) using opEquals will generate a lot of unnecessary template instances. Removing match here to prevent that.

ljmf00 avatar Oct 16 '22 19:10 ljmf00

Thanks for your pull request, @ljmf00!

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + phobos#8607"

dlang-bot avatar Oct 16 '22 19:10 dlang-bot

I would much, much rather improve match itself than avoid using it. Duplication like this increases maintenance burden and risks introducing bugs (in fact, I think it has introduced one already).

One simple optimization we can do is to replace the lambda handler in opEquals with a global function, so that instantiations will be shared between different SumType instances. This will also cut down on instantiations of canMatch.

pbackus avatar Oct 16 '22 19:10 pbackus

@ljmf00 The style checker fails:



Enforce whitespace before opening parenthesis
--
  | grep -nrE "\<(for\|foreach\|foreach_reverse\|if\|while\|switch\|catch\|version)\(" $(find etc std -name '*.d') ; test $? -eq 1
  | std/sumtype.d:717:            final switch(tag)
  | std/sumtype.d:719:                static foreach(idx, T; Types)
  | std/sumtype.d:724:                            static foreach(ridx, U; typeof(rhs).Types)
  | make: *** [posix.mak:587: style_lint_shellcmds] Error 1

  • Mind @pbackus 's comment.

RazvanN7 avatar Nov 01 '22 09:11 RazvanN7