The basic idea is that serialization type should not be initialized when it is not needed. Same for the serialization version. So do this here for HashWriter. CHashWriter remains in places where it is not yet possible.
Backport of core#25331 and core#28341.