Skip to content

hash/maphash: first call of the Reset method doesn't reset internal state  #37315

@ALTree

Description

@ALTree

The new hash/maphash package has a Reset method documented as:

Reset discards all bytes added to h. (The seed remains the same.)

Moreover, the Sum64 doc says:

Sum64 returns h's current 64-bit value, which depends on h's seed and the sequence of bytes added to h since the last call to Reset

So it appears it would be reasonable to expect this to print two times the same value:

package main

import (
	"fmt"
	"hash/maphash"
)

func main() {
	h := new(maphash.Hash)

	h.WriteString("hello")
	fmt.Printf("%#x\n", h.Sum64())

	h.Reset()

	h.WriteString("hello")
	fmt.Printf("%#x\n", h.Sum64())
}

But it doesn't:

$ gotip run hash.go
0xdf1aabcc36e56272
0x5a04b54a5a2174d6

This is because even if the seed remains the same and "all the previously added bytes are discarded", the internal state of the Hash is maintained, even after the h.Reset() call.

Is this the desired behaviour? If it is, the documentation should probably be amended to make it clear what to expect after a call to the Reset method.

cc @alandonovan @randall77

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions