steveyen / gkvlite

Simple, ordered, key-value persistence library for the Go Language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reusing byte slice

beeker1121 opened this issue · comments

Thanks for the awesome library!

I'm trying to reuse a byte slice as a key in a for loop, and it appears that gkvlite keeps using the same key.

Some quick code to show an example:

b := make([]byte, 4)

var i uint32 = 0
for {
    if i >= 10 {
        break
    }

    binary.LittleEndian.PutUint32(b, i)
    c.Set(b, []byte(""))

    i++
}

This seems to keep reusing the same key (the file size is equal to that of one entry). If you create a new byte slice though on each iteration and use that, it seems to work as expected:

var i uint32 = 0
for {
    if i >= 10 {
        break
    }

    b := make([]byte, 4)
    binary.LittleEndian.PutUint32(b, i)
    c.Set(b, []byte(""))

    i++
}

Any ideas?

Hi @beeker1121,
Thanks for the kind words.

Not sure offhand what the issue is. Wondering, though, are you using bytes.Compare() as your key comparator or perhaps a custom key comparator?

I'm not sure to be honest, I'm just using the Set function as shown above so I think it would be the default.

Basically I have a file with 1 million entries, and I loop through these entries and add it to the gkvlite store. When using the first example code where the byte slice is reused, the ending file is only 147 bytes (the size of one entry in the store). The second example ends up at the true size of 81M.

Hi - looks like gkvlite doesn't make it's own copy of the Key and Value inputs, so that explains what's going on...

https://github.com/steveyen/gkvlite/blob/master/collection.go#L161

Ahh ok, thank you for looking into it!