replace the implementation of canonicalForm() (8x faster on -O0)