earthstar icon indicating copy to clipboard operation
earthstar copied to clipboard

Replica eraseExpiredDocs() is called too frequently

Open basham opened this issue 2 years ago • 2 comments

I'm profiling my browser app with the latest Earthstar 9.3.2. The replica's eraseExpiredDocs() method is being called continuously. This happens because there's a setInterval() with no delay argument. This means it is called at every tick. This seems like lots of unnecessary work and CPU time. I don't know what is a more reasonable default delay. Maybe once a minute? You could consider adding an option to the Replica constructor to allow this delay to be configured by the user.

Alternatively, on load, the Replica could query to find the timestamps of all the expiring docs. Then set up setTimeout() to call eraseExpiredDocs() at the designated time. Once that completes, initiate another setTimeout(). This is better than setting up timers for every expired docs, as it will scale better. When a new doc is added to the Replica that has an expiration, add that timestamp to the list. If it is sooner than the active timer, then stop the timer and set up a new one for that newest timestamp.

Even though its slightly more work, I'd advocate for the second option (or something like it). I don't currently plan to have any expiring docs, so I'd like something that has as minimal CPU/memory impact as possible.

basham avatar May 03 '22 14:05 basham

For reference, I'm noticing eraseExpiredDocs() takes about 1.5ms per call. After about 3ms, it is being called again. This repeats forever.

basham avatar May 03 '22 14:05 basham

Oh yeah. That's... me just forgetting to pass an argument. Thanks for catching this.

sgwilym avatar May 05 '22 08:05 sgwilym