nspcc-dev / neo-go

Go Node and SDK for the Neo blockchain
MIT License
123 stars 79 forks source link

Support basic DCE and build tags in the compiler #2673

Open fyrchik opened 2 years ago

fyrchik commented 2 years ago

There are 2 cases for the build tags:

  1. neofs-contract has different logic depending on whether Notary is enabled or disabled. This makes our contracts bigger, it would be nice to have the same code base and specify what we need during compilation.
  2. For tests we may want to compile have debugLog function declared non empty and removed from the release version.

I am hoping this could be done without touching compiler at all, this is just about the files we want to include.

As a consequence, it would be nice to support 2 basic DCE cases:

  1. Empty non-exported function call could be safely removed.
  2. If with constant condition can be replaced with a corresponding branch (e.g. if debug { }).

I don't want to complicate our compiler, but these cases (at least the second) seems simple enough.

roman-khimov commented 2 years ago

Do we have build tags anywhere in the package data? Do we want to parse comments? Constant conditions seem to be simple enough to try optimizing them, but conditional compilation (hi #ifdef) for contracts in Go? SCs are relatively simple and they should be simple enough for audit.

fyrchik commented 2 years ago

Do we have build tags anywhere in the package data? Do we want to parse comments?

Part of the task is to figure it out, but it seems like we could just pass through the build flags to the go command https://github.com/golang/tools/blob/master/go/packages/packages_test.go#L497

SCs are relatively simple and they should be simple enough for audit.

I see your point, but IMO code can be simpler with build tags: for every single scenario (with/without notary) you can see what is actually compiled and used. Also go:build isn't comparable to #ifdef in terms of possible complexity, because tags can only be provided on a per-file basis.