Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: hash: add Cloner #69293

Closed
qmuntal opened this issue Sep 5, 2024 · 2 comments
Closed

proposal: hash: add Cloner #69293

qmuntal opened this issue Sep 5, 2024 · 2 comments
Milestone

Comments

@qmuntal
Copy link
Contributor

qmuntal commented Sep 5, 2024

Most hashers in the standard library implement encoding.BinaryMarshaler and encoding.BinaryUnmarshaler. Using those interfaces does make sense in some situations, with the drawback that they won't work with hashers that don't implement them and that it is using encoding methods to clone feels hacky. For example, the Go team at Microsoft maintain some Go bindings for Windows CNG (see repo), and CNG doesn't support serializing the hash internal state.

See https://github.com/search?q=language%3AGo+h.%28encoding.BinaryMarshaler%29&type=code for a none-exhaustive list of projects that use the encoding.BinaryMarshaler interface.

After analyzing the code of the projects in the list, we found that the encoding.BinaryMarshaler interface is mostly used in the following ways:

  • To serialize the hasher state to disk or to a network connection so that it can be restored later.
  • To clone a hasher so that the cloned hasher does not have to recompute the hash over the same data, which might be expensive or even impossible due to the data not being available anymore.

I propose to define an interface that is easier to implement by hash providers:

type Cloner interface {
    Hash
    Clone() (hash.Hash, error)
}

In addition to the above, we would update all hashers in the stdlib that implement hash.Cloner.

Examples of projects cloning a hash:

@gopherbot gopherbot added this to the Proposal milestone Sep 5, 2024
@qmuntal qmuntal removed the Proposal label Sep 5, 2024
@qmuntal qmuntal closed this as not planned Won't fix, can't repro, duplicate, stale Sep 5, 2024
@cpu
Copy link

cpu commented Sep 18, 2024

See realexamples of Cloning a hash is much simpler and generic than marshaling/unmarshaling a hash, so it could potentially be implemented by more

Examples:

This reads a bit like your message was truncated accidentally (?). Did you happen to collect up a real-world set of examples where this is beneficial?

@qmuntal qmuntal changed the title proposal: hash: add HashCloner proposal: hash: add Cloner Sep 19, 2024
@qmuntal
Copy link
Contributor Author

qmuntal commented Sep 19, 2024

This reads a bit like your message was truncated accidentally (?). Did you happen to collect up a real-world set of examples where this is beneficial?

This is a half-backed proposal. It got submitted by mistake after a network connection glitch. I've updated the description with some more details I had in mind. Hope it helps.

I'll not reopen this issue, though. I like how #69521 is heading.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants