Use Set for unique name checking for efficiency

A list is fine in most cases, but we were getting some pathologically
bad cases especially where there were lots of ambiguous names in large
functions.
Merge pull request #1674 from edwinb/uniquename-set

Use Set for unique name checking for efficiency