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

Replace gob encoding with own writing to hash. #15

Merged
merged 6 commits into from
Jan 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 43 additions & 37 deletions pkg/fakedoc/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
package fakedoc

import (
"encoding/gob"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"hash"
"hash/fnv"
"io"
"log"
"maps"
"math"
"math/rand/v2"
"os"
Expand Down Expand Up @@ -225,43 +224,48 @@ func (gen *Generator) randomString(minlength, maxlength int) string {
return builder.String()
}

func init() {
gob.Register(time.Time{})
gob.Register(reference{})
gob.Register([]any{})
func writeInt32(v int32, h hash.Hash64) {
var buf [4]byte
binary.NativeEndian.PutUint32(buf[:], uint32(v))
h.Write(buf[:])
}

func demap(v any) any {
switch a := v.(type) {
// hashing is used to create a 64 bit hash value for
// a generated item to speed up unique checks.
func hashing(v any, h hash.Hash64) {
switch x := v.(type) {
case string:
io.WriteString(h, x)
case []any:
writeInt32(int32(len(x)), h)
for _, y := range x {
hashing(y, h)
}
case time.Time:
if data, err := x.MarshalBinary(); err == nil {
h.Write(data)
}
case *reference:
io.WriteString(h, x.Namespace)
writeInt32(int32(x.Length), h)
writeInt32(int32(len(x.Values)), h)
for _, y := range x.Values {
io.WriteString(h, y)
}
case map[string]any:
keys := slices.Sorted(maps.Keys(a))
pairs := make([]any, 0, len(keys)*2)
for _, key := range keys {
pairs = append(pairs, key, demap(a[key]))
writeInt32(int32(len(x)), h)
// keys := slices.Sorted(maps.Keys(x)) is slower!
keys := make([]string, 0, len(x))
for key := range x {
keys = append(keys, key)
}
return pairs
case []any:
b := make([]any, 0, len(a))
for _, i := range a {
b = append(b, demap(i))
slices.Sort(keys)
for _, key := range keys {
io.WriteString(h, key)
hashing(x[key], h)
}
return b
default:
return v
}
}

func itemHasher() func(any) uint64 {
hash := fnv.New64()
return func(v any) uint64 {
v = demap(v)
hash.Reset()
enc := gob.NewEncoder(hash)
if err := enc.Encode(v); err != nil {
log.Printf("encoding failed: %v\n", err)
return 0
}
return hash.Sum64()
panic(fmt.Sprintf("unkown type: %T", v))
}
}

Expand Down Expand Up @@ -297,17 +301,19 @@ func (gen *Generator) randomArray(tmpl *TmplArray, limits *LimitNode, depth int)

var (
items []any
hash hash.Hash64
hashes map[uint64][]any
hasher func(any) uint64
key uint64
notInItems func(any) bool
)

if tmpl.UniqueItems {
hashes = map[uint64][]any{}
hasher = itemHasher()
hash = fnv.New64()
notInItems = func(v any) bool {
key = hasher(v)
hash.Reset()
hashing(v, hash)
key = hash.Sum64()
return !slices.ContainsFunc(hashes[key], func(item any) bool {
return reflect.DeepEqual(item, v)
})
Expand Down