Repository navigation
Conversation
Restrict memory used in big slices/maps based on total available memory.
|
Is this your code, or where does it come from? |
|
Very originally it was your index sorting code, but it became quite unrelated (I rewrote almost the entire thing a few times...). However https://github.com/syncthing/syncthing/pull/4980/files#diff-dbf2b1d1fa6344048d9bc7c2ce21be8aR167 is probably still the same (or I copied it from some lib/db file, don't remember), in which case I should keep 2016 as copyright date (?). |
|
Oh alright I misunderstood your comment on the issue that was not original code, but then I get it. |
| if m.spilling { | ||
| m.inactive.iter(fn) | ||
| } | ||
| m.iter(fn) |
There was a problem hiding this comment.
This does not respect fn returning false, and calls iteration function again with the second container
| } | ||
|
|
||
| func (s ValueFileInfoSlice) FromByte(v []byte) { | ||
| var f protocol.FileInfo |
There was a problem hiding this comment.
These protocol specific types should not be in this package.
There was a problem hiding this comment.
Better in bep_extensions.go? It was a coin toss for me, because it's both diskoverflow specific (implemented interface) and protocol specific (underlying type).
| type Value interface { | ||
| Bytes() int64 // estimated size in memory in bytes | ||
| ToByte() []byte | ||
| FromByte([]byte) |
There was a problem hiding this comment.
Surely:
encoding.BinaryMarshaller
encoding.BinaryUnmarshaller
Size() int64
Makes more sense?
There was a problem hiding this comment.
These encoding interfaces allow for failure, i.e. return an error. I really don't know how to handle an error gracefully and littering err!=nil->panic all over the place is ugly. Naming is definitely shit, Marshal, Unmarshal and Size is much better.
| } | ||
|
|
||
| func shouldDebug() bool { | ||
| return l.ShouldDebug("diskoverflow") |
There was a problem hiding this comment.
Didn't see this used.
| Iter(fn func(v Value) bool, rev bool) | ||
| IterAndClose(fn func(v Value) bool, rev bool) | ||
| } | ||
|
|
There was a problem hiding this comment.
Probably want indexing
There was a problem hiding this comment.
I only implemented what's already needed, and indexing isn't. Can definitely be added.
| if !fn(s.values[i]) { | ||
| return | ||
| } | ||
| if closing { |
There was a problem hiding this comment.
What's the use case for this?
There was a problem hiding this comment.
I guess it would make sense if we were somehow appending as we iterate, but we never do that, so doing all of this stuff potentially slows things down, and doesn't alleviate memory pressure as I suspect it's unlikely for GC to be invoked while while we are dereferencing things.
There was a problem hiding this comment.
[...] as I suspect it's unlikely for GC to be invoked while while we are dereferencing things.
E.g. scanning can take a long time, where I'd expect GC to kick in. Also huge sync operations (e.g. resolving conflicts on initial sync) do take a lot of time on a "normal" laptop. And all of this only happens when the container is bigger than 10MB.
| func (s *sorted) iterImpl(fn func(v Value) bool, rev, closing bool) { | ||
| if closing { | ||
| defer s.Close() | ||
| } |
There was a problem hiding this comment.
WHy is this needed if IterAndClose calls s.Close() anyway?
| comp = 1 | ||
| } | ||
| for aok && iok { | ||
| if bytes.Compare(av.Key(), iv.Key()) == comp { |
There was a problem hiding this comment.
This could be insertion sort I guess. Also, this has the caveat that the key has to be unique, otherwise you lose values.
There was a problem hiding this comment.
Insertion sort is just a lot slower than the current insert sequentially and sort when a value is queried for the first time. And I indeed did overlook the uniqueness problem, but that's easily solvable by adding an insertion counter at the end of the key.
There was a problem hiding this comment.
Some general comments...
-
There's a lot of moving parts and channels and async and routines and stuff in the Sorted.iterImpl. I didn't understand it all and would like some comments. But more importantly it looks like most of it is to manage the fact that there may be several storage backends in play that need to be merged? This could be simplified by just moving everything from in-memory to on-disk when the first spill happens and always just having one backend?
-
I've grown less fond of the function based iterator and would prefer something like the iterators in leveldb - something you create, call Next() and Value() and so forth on in a loop, and then have a deferred Close() on. I think this makes for better code at the call site, maybe something to look into?
-
I don't think these types should concern themselves with memory sizing, instead just taking a maximum size at construction time. That size could potentially be something auto generated, but mostly I think it's good enough to have a configurable that defaults to something like 8 or 16 megs - just to avoid disk spilling for trivial operations.
I implemented this "object style" iterators. Personally I don't even like the code on the call site. On the implementation side it's anyway pretty ugly (maybe I just don't know how to make it pretty). And I didn't implement the full functionality of e.g. the leveldb iterator (that would be a huge pain to do). What do you think? 986af92
I'd like to avoid disk spilling at all times. In my personal experience disk io is the weakest point of syncthing's performance - especially when huge sync operations happen. That's exactly when disk spilling of these types might also happen. So I really only want it to happen, if OOM is a danger. With a per object limit or with a limit that does not depend on system RAM, even on a system with plenty of RAM Syncthing will do more disk trashing. |
|
🤖 beep boop I'm going to close this pull request as it has been idle for more than 90 days. This is not a rejection, merely a removal from the list of active pull requests that is periodically reviewed by humans. The pull request can be reopened when there is new activity, and merged once any remaining issues are resolved. |
Purpose
Initial implementation of what I talked about in #4976. The idea is to restrict memory used in big slices/maps based on total available memory.
Todo
Testing
Only existing ones so far, so only in-memory operations are tested, disk-spilling is untested.