New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Save messages in append-StorageFile #1498
Conversation
Thanks! There's a lot there so I'll have to go through this later. It looks good but it's a surprising amount of extra code needed. I think it's probably worth not rewriting it in the messages app (as I imagine that'll be opened for most notifications anyway), but maybe reallocate if the size would grow above 40k (which is the point where storagefile would create a new file) |
Well, "needed"... The two main bulks are
I thought about looking at the size, but the docs says "This requires Espruino to read the file from scratch, which is not a fast operation.". So I figured checking that every time we append a message would kind of defeat the purpose. Unless most of the delay comes from parsing JSON? Also I wasn't sure if every event definitely has an |
9ebcc91
to
4de7803
Compare
I rebased it onto master, and got rid of separate |
Thanks! I will get on this, I'm just pretty busy at the moment |
Sure, there's no rush, and I definitely want you take a good look at changes like this before merging. |
Still TODO: look into the numbers, come up with a sensible value to compare to |
1cff7bc
to
9e04183
Compare
Rebased it onto master, and got rid of But we can probably do better:
|
9e04183
to
fb3d2c9
Compare
untested: updated to keep the StorageFile around until it grows to 40000bytes. |
Looks great - thanks! I'll have a play with this branch on mine and see how it's working :) |
If you've got some time now that's great, but note that I really meant the untested bit for my last changes: I think it should work, but haven't gotten round to actually testing it, at all. |
I tested it a bit in the emulator and fixed a few bugs, seems to work OK now:
(Edit: I'm planning to merge this into the code running on my watch this weekend for some real-world testing, but that probably won't hit 40KB for a while ;-) |
Ok, great! Maybe it's best to hold off on merging this just a little more then until you've been running with it on the watch itself for at least a few days. Just thinking aloud here but the actual file size is ~40930 bytes I think? So if you do a check at 40000 and then you add another 930 bytes of messages, a new file would get created anyway. I think it's unlikely but I guess we could drop the check to 38000, in which case it would be hugely unlikely that any one message would push it into a second file. I'd also be interested to see if you notice any slowness when exiting the messages app. While I like the idea of the ... but maybe that's one for later when we've got this merged ;) |
Good point, I'll adjust that.
Sure, but in most cases you probably open the app, open a message (marking it as read), and then delete it, so you end up writing a copy of the message except with
That way we don't need to parse the file on exit. |
Could do, but probably not worth the extra effort just yet. Let's set your this PR goes. Taking of which I used this branch and put in on my bangle and something is very wrong - short buzz and no notifications at all. But I'll have to look into it on Monday |
e5832e4
to
58588f4
Compare
Turns out it wasn't that much effort, so I added it before reading your comment. |
4bced88
to
de6ecc8
Compare
I gave it a try on my Bangle.js 1, but didn't have time to properly debug stuff yet.
That's from Firefox playing media being forwarded to my phone, so the "Body Machine | Priest" bit is fine, but all the |
Actually, I think what you have is fine... I've just been pasting in some test messages, eg |
e168736
to
555a137
Compare
Doh, the library code had an internal function named |
Ahh, that'd do it! Although if you're not in a clock, do message icons still appear? That didn't work for me either. Do you not get out of memory errors with the current latest messages app? I've noticed the newish message icon code appears to keep the entire messages list in RAM, which is pretty wasteful. How many messages did you have? It doesn't look to be like your code should use that much extra memory. |
Thanks - I'll file an issue for that as there should really be a way to just query the messages library/widget |
9f50a52
to
ab59105
Compare
Strange, I know I tested filling up the storage file before, and it worked fine. But today I finally filled up my watch "naturally",and it broke :-( Fixed now: const file = require("Storage").open("messages.jsonl", "a");
if (file.getLength()>38000) {
file.erase();
file.write(JSON.stringify(e)+"\n"); // throws "Can't write in this mode" So I changed it to let file = require("Storage").open("messages.jsonl", "a");
if (file.getLength()>38000) {
file.erase();
file = require("Storage").open("messages.jsonl", "a");
file.write(JSON.stringify(e)+"\n"); and now it works again. (I'm not surprised that writing to an erased file throws, just that it used to work before, maybe I broke it during some merge/rebase...) |
Ahh, interesting, thanks! It's good it fails though - that could have led to storage corruption before. I'm pretty sure you don't actually need an erase. You can just use
|
ab59105
to
0e86c3e
Compare
0e86c3e
to
5d6c3d3
Compare
Rebased it onto |
5d6c3d3
to
0b3acd2
Compare
0b3acd2
to
4e05cd2
Compare
Rebased onto |
8111ecf
to
8869618
Compare
Set it back to draft, as it will need a bit of work(+more testing) to apply onto #2205 Some good news: now that the |
You mean you're using append-storagefile? Thanks! and sorry It's taken so long for me to test/merge this one in - it just feels like quite a big one :) |
I have been for quite a while now, just not this exact code:
I totally understand, it's a big change and we definitely don't want the messages app to break. |
Even to the default app/widget
Basically always keep MESSAGES in memory until event handlers have had a chance to run, so they don't all cause Storage.read()s when they call require("messages").getMessages()
8869618
to
a762dc0
Compare
Putting this on hold for a bit, at least until we have a stable |
I guess we do now have a more stable API - and I think we could in fact just create a separate However, do you think there's a need for this now? I've made a few changes to storage in the last 6 months that has made slowdowns from lots of file writes basically disappear completely - even completely uncompacted it should be basically as fast as right after a compact. By contrast I'm a bit concerned that reading the messages line by line from a StorageFile may actually end up being slower than the current solution. With your recent changes to the library it seems we have the option to not save messages too, and we already fast-load the |
We could, but I don't think there would be much point to having two libraries providing the exact same functionality. And it would complicate things whenever anybody has a problem with messages.
Well, that definitely sounds like there isn't any need for this anymore 🙂
Yes, now that we have fast loading, that definitely seems like the way to go. (Also: we should optimize it to avoid writing a new file when nothing has changed, or at least just delete the file instead of writing |
Thanks for all your work on this - I know it's taken ages and I'm sorry not to have merged it after all that.
That's a great idea! I've filed a new issue at #2370 to keep track of these.
|
Implements #1460