Skip to content
This repository was archived by the owner on Apr 18, 2021. It is now read-only.
Open
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
6 changes: 3 additions & 3 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package httpcache
import (
"bufio"
"bytes"
"crypto/sha256"
"errors"
"fmt"
"io"
Expand All @@ -16,6 +15,7 @@ import (
"strings"
"time"

"hash/fnv"
"github.com/rainycape/vfs"
)

Expand Down Expand Up @@ -190,8 +190,8 @@ func (c *Cache) Freshen(res *Resource, keys ...string) error {
}

func hashKey(key string) string {
h := sha256.New()
io.WriteString(h, key)
h := fnv.New64a()
h.Write([]byte(key))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would imagine a part of the speedup comes from using Write method directly, instead of going through io.WriteString which incurs a func call and additional computation (a type assertion which fails).

Have you tried benchmarking with sha256 but rewriting it to do h.Write([]byte(key)) as well? How does that compare?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did in there : https://gist.github.com/hectorj/8ac959c071d44ec4d718#file-cache_key_hash_test-go-L19

Though I only included the fastest results in the gist, and I don't have the numbers right here right now (will get them once I'm home)
IIRC Write([]byte(key)) instead of WriteString does help a bit, but FNV stays faster than sha256

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC Write([]byte(key)) instead of WriteString does help a bit, but FNV stays faster than sha256

Cool, thanks. I was just curious.

return fmt.Sprintf("%x", h.Sum(nil))
}

Expand Down