Where the Records Live
CORE's records now live in SQLite, in the browser's own private file storage. I measured three ways of keeping them before choosing, and started moving everything every app depends on into one engine, so there is one of each.
One of everything
The tool had grown the way a site trailer fills up: a second filing cabinet because the first was in the other room. Counting turned up three pieces of code that each work out a schedule, two logs of who saved what, and four ways of turning a date into a day number. Each one worked. Together they were three answers to the same question, and sooner or later two of them would disagree in front of somebody.
So the program now has rings. At the centre is the engine: the records and the only way to keep them, the event loop, the log of who did what, the calendar, and the forecast. Around it, the parts that run on the records by themselves and only ever propose: the procedures, the continuous forecast, the assistant's queue. Then the apps (actions, minutes, RFIs, the schedule), each complete on its own but standing on the engine rather than on each other. Then the shell that hosts them. Xtrůd keeps the model and only the model. PULSE stays a standalone app on a phone, reads the records already on that phone so a report can name a real activity, and syncs later.
Why the records had to move
Until now, every record in CORE sat in the browser's localStorage as one block of text, rewritten in full after every change. It was simple and it was fast for the demo's 675 records. It also has a ceiling of about five megabytes, and every change pays for the whole set.
SQLite is a real database that runs inside the browser. The question was which way to run it, so I measured three on the demo's records, then ten and a hundred times as many: localStorage as it was; SQLite held in memory with the whole file saved after each change (sql.js); and the official SQLite build writing to the browser's private file storage, a change at a time.
The numbers
The time to keep one changed record, on a desktop browser:
- At the demo's size (675 records): localStorage 2.8 ms, SQLite in memory 4.2 ms, SQLite in file storage 8.7 ms.
- At ten times (6,750): localStorage 45 ms, in memory 23 ms, in file storage 9 ms.
- At a hundred times (67,500): localStorage refused (over its limit), in memory 240 ms, in file storage 9.8 ms.
With the processor slowed four times, as a rough stand-in for a phone, localStorage took 245 ms per change at ten times and SQLite in memory 690 ms at a hundred. SQLite in file storage stayed between 11 and 13 ms at every size. Keeping a change cost the same whether the project was small or large, which is the property I was after.
A caveat: these are Chromium numbers. I test iPhone Safari by emulating it, and emulation doesn't measure Safari's storage. The real phone may be slower, and I'll measure it there before trusting it.
What it costs
Nothing is free. The SQLite engine is about 0.9 MB to download the first time, and CORE's first load went from instant to just under a second; after that, about a quarter of a second. CORE still loads every record into memory when it opens: 30 ms at the demo's size, 300 ms at ten times, nearly three seconds at a hundred. Past that point the apps will have to ask the database for what they need instead of holding everything. The file storage lets one tab hold the database at a time, so tabs take turns under a lock and tell each other when they've written. A browser that has no private file storage keeps the records in localStorage, as before.
Every write also goes into a change log in the same database: what changed, and when, in order. That log is what a later sync from PULSE, an audit, or an undo will read.
Fewer lines
The other aim is less code. So far it has gone the wrong way. The new store and its worker added about 85 lines, the single time module 30, and removing the duplicate date code took out about 70. Net, the program is 100 lines longer than it was this morning. The next steps are where the lines come out: one critical path instead of three, one Monte Carlo instead of three, one log instead of two. They aren't tidy-ups. Xtrůd's forecast costs each run from the model's estimate and the engine's doesn't yet, so merging them changes numbers, and each will get its own entry in the decisions log saying by how much.
The decisions and the benchmark are written up in the decisions log, and the architecture diagram shows where each piece now lives.