Skip to content

Commit

Permalink
fix benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Apr 18, 2023
1 parent d1fc4e8 commit c3c24f7
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 60 deletions.
2 changes: 2 additions & 0 deletions benches/bench.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ rm -rf bin
mkdir -p bin

cd mock
go mod tidy
go build -o mock-generator main.go
cp mock-generator ../bin/
cd ..

cd ristretto-go
go mod tidy
go build -o ristretto-go main.go
cp ristretto-go ../bin/
cd ..
Expand Down
2 changes: 1 addition & 1 deletion benches/mock/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/al8n/mock

go 1.16

require github.com/dgraph-io/ristretto v0.1.0
require github.com/dgraph-io/ristretto v0.1.1
13 changes: 9 additions & 4 deletions benches/mock/go.sum
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgraph-io/ristretto v0.1.0 h1:Jv3CGQHp9OjuMBSne1485aDpUkTKEcUqF+jm/LuerPI=
github.com/dgraph-io/ristretto v0.1.0/go.mod h1:fux0lOrBhrVCJd3lcTHsIJhq1T2rokOu6v9Vcb3Q9ug=
github.com/dgraph-io/ristretto v0.1.1 h1:6CWw5tJNgpegArSHpNHJKldNeq03FQCwYvfMVWajOK8=
github.com/dgraph-io/ristretto v0.1.1/go.mod h1:S1GPSBCYCIhmVNfcth17y2zZtQT6wzkzgwUve0VDWWA=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14 h1:k5II8e6QD8mITdi+okbbmR/cIyEbeXLBhy5Ha4nevyc=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
2 changes: 1 addition & 1 deletion benches/moka-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async = ["moka/future", "tokio/full"]

[dependencies]
mimalloc = { version = "0.1.26", default-features = false }
moka = {version = "0.6.1", optional = true }
moka = {version = "0.10", optional = true }
serde = {version = "1", features = ["serde_derive"] }
serde_json = "1.0.68"
tokio = {version = "1.12.0", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion benches/ristretto-go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/al8n/stretto

go 1.16

require github.com/dgraph-io/ristretto v0.1.0
require github.com/dgraph-io/ristretto v0.1.1
13 changes: 9 additions & 4 deletions benches/ristretto-go/go.sum
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgraph-io/ristretto v0.1.0 h1:Jv3CGQHp9OjuMBSne1485aDpUkTKEcUqF+jm/LuerPI=
github.com/dgraph-io/ristretto v0.1.0/go.mod h1:fux0lOrBhrVCJd3lcTHsIJhq1T2rokOu6v9Vcb3Q9ug=
github.com/dgraph-io/ristretto v0.1.1 h1:6CWw5tJNgpegArSHpNHJKldNeq03FQCwYvfMVWajOK8=
github.com/dgraph-io/ristretto v0.1.1/go.mod h1:S1GPSBCYCIhmVNfcth17y2zZtQT6wzkzgwUve0VDWWA=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14 h1:k5II8e6QD8mITdi+okbbmR/cIyEbeXLBhy5Ha4nevyc=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
17 changes: 4 additions & 13 deletions benches/ristretto-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,17 @@ func main() {
NumCounters: 12960, // 36^2 * 10
MaxCost: 1e6, // 1mb
BufferItems: 64,
Metrics: true,
KeyToHash: func(key interface{}) (uint64, uint64) {
kc := key.(KC)
return kc.hash, kc.conflict
},
Metrics: true,
})

if err != nil {
panic(err)
}

t := time.Now()
for _, kv := range dataset.Data {
kc := KC{
hash: kv.Hash,
conflict: kv.Conflict,
}
if _, ok := c.Get(kc); !ok {

c.Set(kc, kv.Val, kv.Cost)
for _, kv := range dataset.Data {
if _, ok := c.Get(kv.Key); !ok {
c.Set(kv.Key, kv.Val, kv.Cost)
}
}
c.Wait()
Expand Down
40 changes: 5 additions & 35 deletions benches/ristretto-rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
extern crate serde;

use std::path::Path;
use stretto::KeyBuilder;


#[global_allocator]
static GLOBAL_ALLOCATOR: mimalloc::MiMalloc = mimalloc::MiMalloc;
Expand All @@ -21,26 +21,6 @@ struct KV {
cost: i64,
}

#[derive(Hash, Eq, PartialEq)]
struct KC {
hash: u64,
conflict: u64,
}

#[derive(Default)]
struct KH;

impl KeyBuilder for KH {
type Key = KC;

fn hash_index(&self, key: &KC) -> u64 {
key.hash
}

fn hash_conflict(&self, key: &KC) -> u64 {
key.conflict
}
}

#[cfg(feature = "sync")]
fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand All @@ -52,19 +32,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let dataset: Dataset = serde_json::from_slice(content.as_slice())?;

let c = Cache::builder(12960, 1e6 as i64)
.set_key_builder(KH::default())
.set_metrics(true)
.finalize()
.unwrap();

let time = Instant::now();
for kv in dataset.data {
let kc = KC {
hash: kv.hash,
conflict: kv.conflict,
};
if c.get(&kc).is_none() {
c.insert(kc, kv.val, kv.cost);
if c.get(&kv.key).is_none() {
c.insert(kv.key, kv.val, kv.cost);
}
}
c.wait().unwrap();
Expand All @@ -86,19 +61,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let dataset: Dataset = serde_json::from_slice(content.as_slice())?;

let c = AsyncCache::builder(12960, 1e6 as i64)
.set_key_builder(KH::default())
.set_metrics(true)
.finalize(tokio::spawn)
.unwrap();

let time = Instant::now();
for kv in dataset.data {
let kc = KC {
hash: kv.hash,
conflict: kv.conflict,
};
if c.get(&kc).is_none() {
c.insert(kc, kv.val, kv.cost).await;
if c.get(&kv.key).await.is_none() {
c.insert(kv.key, kv.val, kv.cost).await;
}
}
c.wait().await.unwrap();
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,10 @@ impl<K: Hash + Eq> KeyBuilder for DefaultKeyBuilder<K> {
}
}

/// Dummy hasher will do nothing. Used by [`TransparentKeyBuilder`].
#[derive(Default, Copy, Clone, Eq, PartialEq, Debug)]
#[repr(transparent)]
struct TransparentHasher {
pub struct TransparentHasher {
data: u64,
}

Expand Down

0 comments on commit c3c24f7

Please sign in to comment.