dgraph-io / ristretto

A high performance memory-bound Go cache
https://dgraph.io/blog/post/introducing-ristretto-high-perf-go-cache/
Apache License 2.0
5.54k stars 364 forks source link

[BUG]: cost added always differ by 56 #373

Open toshinari123 opened 7 months ago

toshinari123 commented 7 months ago

What version of Ristretto are you using?

the latest

What version of Go are you using?

go version go1.21.5 linux/amd64

Have you tried reproducing the issue with the latest release?

Yes

What is the hardware spec (RAM, CPU, OS)?

linux mint victoria xfce

CPU: Info: quad core model: Intel Core i5-8265U bits: 64 type: MT MCP smt: enabled arch: Comet/Whiskey Lake note: check rev: C cache: L1: 256 KiB L2: 1024 KiB L3: 6 MiB Speed (MHz): avg: 800 min/max: 400/3900 cores: 1: 800 2: 800 3: 800 4: 800 5: 800 6: 800 7: 800 8: 800 bogomips: 28800 Flags: avx avx2 ht lm nx pae sse sse2 sse3 sse4_1 sse4_2 ssse3 vmx

RAM: 2 times the following Total Width: 64 bits Data Width: 64 bits Size: 8 GB Form Factor: SODIMM Set: None Locator: ChannelA-DIMM0 Bank Locator: BANK 0 Type: DDR4 Type Detail: Synchronous Speed: 2667 MT/s Manufacturer: Samsung Serial Number: 00000000 Asset Tag: None Part Number: M471A1K43BB1-CTD
Rank: 1 Configured Memory Speed: 2400 MT/s

What steps will reproduce the bug?

run go test with following test_test.go code:

package ristretto_test

import (
    "bytes"
    "time"
    "testing"

    "github.com/dgraph-io/ristretto"
    "github.com/stretchr/testify/assert"
)

func TestGetContent(t *testing.T) {
    cache, _ := ristretto.NewCache(&ristretto.Config{
        NumCounters: 1e7,
        MaxCost:     128,
        BufferItems: 64,
        Metrics:     true,
    })

    added := cache.Set("id1", bytes.NewBuffer([]byte("a")), 1)
    time.Sleep(100 * time.Millisecond) //https://github.com/dgraph-io/ristretto/issues/161
    assert.Equal(t, true, added)
    assert.Equal(t, uint64(1), cache.Metrics.CostAdded())

    added = cache.Set("id1", bytes.NewBuffer([]byte("b")), 2)
    time.Sleep(100 * time.Millisecond) 
    assert.Equal(t, true, added)
    assert.Equal(t, uint64(2), cache.Metrics.CostAdded())

    added = cache.Set("id1", bytes.NewBuffer([]byte("c")), 100)
    time.Sleep(100 * time.Millisecond) 
    assert.Equal(t, true, added)
    assert.Equal(t, uint64(100), cache.Metrics.CostAdded()) //will be 0x9c which is 156

    added = cache.Set("id2", bytes.NewBuffer([]byte("b")), 10)
    time.Sleep(100 * time.Millisecond) 
    assert.Equal(t, true, added)
    assert.Equal(t, uint64(110), cache.Metrics.CostAdded()) //will be 0xde which is 222 = 110 + 56 + 56
}

Expected behavior and actual result.

expected behaviour: cost added matches the cost i put in the Set calls

actual result: somehow added by 56 everytime

Additional information

please point me to correct resource if i missed anything on this

darkn3rd commented 7 months ago

This is interesting, anyone know where the extra 56 comes from.

matthewmcneely commented 7 months ago

If you add:

IgnoreInternalCost: true,

to your ristretto.Config, I think your test will pass. This is an indication that you are controlling cost calculation upon .Set. Not sure why internal cost for entries is 56.

github-actions[bot] commented 2 months ago

This issue has been stale for 60 days and will be closed automatically in 7 days. Comment to keep it open.