mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
reduce allocs in getSlotQueueKey (#778)
this somewhat minimally comes up in profiling, but it was an itch i needed to scratch. this does 10x less allocations and is 3x faster (with 3x less bytes), and they're the small painful kind of allocation. we're only reading these strings so the uses of unsafe are fine (I think audit me). the byte array we're casting to a string at the end is also heap allocated and does escape. I only count 2 allocations, but there's 3 (`hash.Sum` and `make([]string)`), using a pool of sha1 hash.Hash shaves 120 byte and an alloc off so seems worth it (it's minimal). if we set a max size of config vals with a constant we could avoid that allocation and we could probably find a checksum package that doesn't use the `hash.Hash` that would speed things up a little (no dynamic dispatch, doesn't allocate in Sum) but there's not one I know of in stdlib. master: ``` ✗: go test -run=yodawg -bench . -benchmem -benchtime 1s -cpuprofile cpu.out goos: linux goarch: amd64 pkg: github.com/fnproject/fn/api/agent BenchmarkSlotKey 200000 6068 ns/op 696 B/op 31 allocs/op PASS ok github.com/fnproject/fn/api/agent 1.454s ``` now: ``` ✗: go test -run=yodawg -bench . -benchmem -benchtime 1s -cpuprofile cpu.out goos: linux goarch: amd64 pkg: github.com/fnproject/fn/api/agent BenchmarkSlotKey 1000000 1901 ns/op 168 B/op 3 allocs/op PASS ok github.com/fnproject/fn/api/agent 2.092s ``` once we have versioned apps/routes we don't need to build a sha or sort configs so this will get a lot faster. anyway, mostly funsies here... my life is that sad now.
This commit is contained in:
@@ -3,9 +3,12 @@ package agent
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/fnproject/fn/api/models"
|
||||
)
|
||||
|
||||
type testSlot struct {
|
||||
@@ -260,3 +263,50 @@ func TestSlotQueueBasic3(t *testing.T) {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSlotKey(b *testing.B) {
|
||||
appName := "myapp"
|
||||
path := "/"
|
||||
image := "fnproject/fn-test-utils"
|
||||
const timeout = 1
|
||||
const idleTimeout = 20
|
||||
const memory = 256
|
||||
CPUs := models.MilliCPUs(1000)
|
||||
method := "GET"
|
||||
url := "http://127.0.0.1:8080/r/" + appName + path
|
||||
payload := "payload"
|
||||
typ := "sync"
|
||||
format := "default"
|
||||
cfg := models.Config{
|
||||
"FN_FORMAT": format,
|
||||
"FN_APP_NAME": appName,
|
||||
"FN_PATH": path,
|
||||
"FN_MEMORY": strconv.Itoa(memory),
|
||||
"FN_CPUS": CPUs.String(),
|
||||
"FN_TYPE": typ,
|
||||
"APP_VAR": "FOO",
|
||||
"ROUTE_VAR": "BAR",
|
||||
}
|
||||
|
||||
cm := &models.Call{
|
||||
Config: cfg,
|
||||
AppName: appName,
|
||||
Path: path,
|
||||
Image: image,
|
||||
Type: typ,
|
||||
Format: format,
|
||||
Timeout: timeout,
|
||||
IdleTimeout: idleTimeout,
|
||||
Memory: memory,
|
||||
CPUs: CPUs,
|
||||
Payload: payload,
|
||||
URL: url,
|
||||
Method: method,
|
||||
}
|
||||
|
||||
call := &call{Call: cm}
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = getSlotQueueKey(call)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user