234 points · by soheilpro · 7y ago · 52 comments
Now, unused imports are mostly harmless (alright, they are not because of init), but they keep unrelated commits clean without needing ad-hoc linting or IDE functionality.
Use Case
The use case for warnings is the exploratory development or debugging phases, where you really don't care about leaving unused things lying around for the time being (for example, temporarily commenting something out), and would rather that the compiler just got out of your way until you've got something ready to compile normally and commit.
Usage
go build -gcflags=-warnunused somefile.go
go test -gcflags=-warnunused
When you're done with your exploratory/debugging phase, simply build or test without the flag: go build somefile.go
go test
Compiling without the flags will fail on unused things as normal.But during development that's just a needless obstacle. I may comment large blocks of code and I don't want to scan each import to make sure they are no longer used.
Part 2 is http://eli.thegreenplace.net/2019/go-compiler-internals-addi...
I have noticed that the AST nodes don't have a parent reference. I wonder how it knows for example when encountering a "continue LABEL" that the code is nested maybe deeply in a loop with that label. The only way I would think of is traversing the tree up but I think there is no way of doing this. How do they do it?
[1] https://github.com/golang/go/blob/master/src/cmd/compile/int... ("ctx" is a "targets" struct containing two entries, one for which statement 'break' targets and another for which statement 'continue' targets).
[2] https://github.com/golang/go/blob/master/src/cmd/compile/int...
[3] https://github.com/golang/go/blob/master/src/cmd/compile/int... (the targets{s, s} passed to innerBlock is then passed to blockBranches - s is the for statement node itself).
As a sidenote, it is interesting how easy Go reads even though i never wrote a single line of Go myself (though i do write a lot of C and Object Pascal and the code patterns look similar).
As to why do that, there are several reasons.
It's fiddly to construct trees when you need to patch the children with parent references. The most natural thing to do is create the children (usually via recursive parse) then construct the parent. If you then need to patch the children with a reference to their parent, it's more work. It also means your nodes can't be immutable (but they're often not wholly immutable anyway for other reasons, like annotating during passes that add semantic information).
It's easier to reason about tree manipulations when you only have downward pointers. For example, maybe you want to rewrite a common subexpression with a reference to a temporary, and reuse one of the common trees as the RHS on the assignment to the temporary. It's more effort if you need to patch both ways on the link, rather than just grab the tree and slot it into the assignment.
(It's possible that you have DAGs rather than trees, and have children with shared parents, but I think this isn't worth any extra representative or compression that it gives you because passes will want to mutate those nodes, and meeting the same nodes more than once makes invariants more complex.)
Finally, more interesting traversals, following control or data flow, can cut across and jump between tree branches, so parent links don't necessarily help you there either.
It helps that most languages don't have parse trees which would stress the runtime stack when processing recursively, outside of machine-generated code (and correspondingly, it's not that hard to get a stack overflow error or equivalent "too much nesting" error if you generate code targeting that failure mode).
So you can traverse up for symbols.
Not many, mostly simple peephole optimizations.
https://github.com/golang/go/blob/master/src/cmd/compile/int...
[0] https://github.com/golang/go/blob/master/src/cmd/compile/int...
Of course it really depends on your definition of "meaningful", but i think that as long as it isn't equal to "as many optimizations known to humankind as possible, everything else be damned", the compiler looks to perform a decent amount of them. At least for me it passes the subjectively vague "meaningful" check :-P.
[1] https://github.com/golang/go/blob/master/src/cmd/compile/int...
[2] https://github.com/golang/go/blob/master/src/cmd/compile/int...
[3] https://github.com/golang/go/tree/master/src/cmd/compile/int...
[4] https://github.com/golang/go/blob/master/src/cmd/compile/int... https://github.com/golang/go/blob/master/src/cmd/compile/int...
[5] https://github.com/golang/go/blob/master/src/cmd/compile/int...
[6] https://github.com/golang/go/blob/master/src/cmd/compile/int...
[7] https://github.com/golang/go/tree/master/src/cmd/compile/int...
[8] https://github.com/golang/go/blob/master/src/cmd/compile/int...
There are exceptions of course, but it's rare to find shaders with thousands of lines of code or more.
1. Queries are often repeated, i.e. most of backend DBs get the same requests over and over again. Even a slow jit compiler is fine here as things just get cached. 2. Queries usually take some time to complete, and this offsets the jit-related latency.
Also, Postgres has a very limited kind of jit compilation, i.e. for expressions only.
Notice that javascript jit compilers usually have a multi-tiered compilation. That's because proper compilation takes time, and sometimes it is more efficient to just do some basic template jiting (or no additional compilation) instead of firing the heavy guns.
None of them use LLVM, btw.
Writing a basic backend isn't a huge project for a language backed as thoroughly as Go. D, for example, has a non-GCC/LLVM backend which isn't as fast but still does some advanced optimisations (but compiles at warp speed)