https://slatedb.io logo
Rust experts.. halp https://github.com/slatedb/sl...
# general
c
s
not an expert, but: I think that what he is saying is that you're "leaking" a
Bytes
reference into the block, which is also
Bytes
. that's safe, but it will keep the entire block alive as long as someone holds the value
to be clear: that's the whole point of
Bytes
though: that it is zero-copy.
so from an API perspective, the "bug" might just be that the API docs should highlight that this happens, and recommend that folks not hold onto the result of
Db::get
for too long... if they're going to take a
value
and put it in a cache, they should "trim" it somehow / copy it into a new buffer
c
Gotcha...
Ya I was lookin' at the code, and I was like.. it's Bytes all the way down
s
the other way to think about it though is that if you're creating a
Bytes
value for the whole block on each read (haven't looked deeper!) you might consider pre-trimming it for them.
c
But it's fair.. the block is 4k.. if you hold one value... it'll keep 4k of memory
We have a Bytes for the whole block (in sst_iter.rs)
s
alternatively, if the block is sitting in a cache already, then it might be totally fine to just give them "a handle into the cache"
c
Yea it's usually in cache
but not guaranteed
s
yea, so that's subtle-ish.
c
Yea
🤔
s
an alternative guarantee might for the docs to say something like: "it is always efficient to hold onto a bytes reference because it will either be a reference into a cache, OR because we have pre-trimmed it"
c
Ah.. but then we have to trim on eviction right
s
yea, technically.
c
Hmmm
s
...more than technically: you are correct, heh 🙂
c
Maybe we should just return copies... 🤔
s
too bad
Bytes
doesn't handle this internally, but it's a tricky problem.
(afaik)
c
Yea.. they'd have to track the references to ranges and then trim things as range references are freed
Yea looks like they just bump the refcount on the underlying object..
s
re: the "technically" point: i do think that there is a slight difference between "leaking" a reference into a cache, and leaking a reference to a fully anonymous block
the former at least has a chance to be reused in multiple values, whereas the latter definitely cannot be
c
Yea totally
s
but depending on your block and value size, it's unclear how helpful it is in the end (thanks pigeonhole principle ... birthday paradox...?)
c
Haha yea exactly
Copying the slices or tracking what needs to be trimmed could be costlier than just leaving it alone..
s
yea. so then i'd go back to this point: https://discord.com/channels/1232385660460204122/1299052993752338442/1299061114444845107 ... it's hard to guess what a user is doing with the value. suggesting that they should trim it themselves if they're going to hold it for a long time might be enough.
c
Yea. I agree. Probably a doc update is the best thing.
KISS 🙂