Encrypted memory on the phone: 10,000 records in half a second
How we put an encrypted copy of personal memory on the phone, what we measured on a real iPhone, and how a slower search turned into a faster one.
Technical post. This one is about the software side: database, encryption and measurements. The numbers come from synthetic data.
Our app has a memory layer. When a person agrees to save something, it can come back later in a chat, a lesson or a search. This post covers two pieces of work. The first puts an encrypted copy of that memory on the phone. The second made search smarter, noticed it had become slower, and then made it faster than before.
The server keeps the record, the phone only reads
Our first decision: the server owns the memory. The copy on the phone is a read-only copy that can be deleted and rebuilt at any moment. The phone never writes to it on its own.
The reason is simple. Someone may work on the web first and then switch to the phone, and nothing they saved in between should be lost. With the real record in one place, moving to the phone stops being a “move everything” problem. It becomes a question of building the right copy and keeping it current.
The server sends the phone one signed full copy, then only the changes. When a record is withdrawn, the change package carries a “deleted” marker, and the record disappears from the phone too.
A half-finished install never breaks the old copy
Phones stop at bad moments: the battery dies, or the system kills the app in the background. So every new copy is built in its own folder first. Counts and checksums are verified. Only when everything matches does a single atomic step point “the active copy” at the new one. If the process dies halfway, the old copy stays where it was.
We did not leave this on paper. In tests we killed the process at three points: while taking the lock, after the data was written, and after the pointer changed. Each time, either the old copy was intact or the new copy read back complete.
No text in the database
The graph database on the phone holds only meaningless IDs and the links between them. The text, title and source of each record are encrypted one by one. The key lives only in that device’s key store; it does not go into backups or to other devices. Reading with the wrong key is refused. When the person signs out, the whole copy and its key are deleted.
What we measured on a real phone
No simulator. We measured on a physical iPhone 16 Pro Max, with synthetic data: 10,000 records mixing Turkish, English and Japanese, and 20,000 links between them. No personal data.
| Operation | Time |
|---|---|
| Install and verify the encrypted copy | 0.44 s |
| Read one record (integrity check included) | 0.08 s |
| Find a concept’s neighbours | 0.08 s |
| Apply a change package (as a new copy) | 0.41 s |
| Delete everything | 0.01 s |
The limits matter too. These are numbers from one device and one test run. The memory figure (about 200–237 MB) includes the test harness itself. We have not yet measured battery use or larger sizes such as 100,000 records, and network time is not in this table.
Smarter search made us slower
On the search side we wanted more human-like behaviour. When someone says “I love apples”, their own fact should come to mind first. An apple cake recipe and an article on the cell structure of apples should wake up too, only more weakly. If the first meaning is wrong, the next one should take over. In a scientific context, the science records should be checked first.
We added this and measured it. On 50,000 synthetic records, a typical search went from 27.8 ms to 35.5 ms. Smarter, but slower. We had expected the opposite: narrowing where to look should have made it faster.
Profiling made the cause clear. On every search, for every meaning, we were recounting “how many approved records does this person have with this meaning”. Now we keep those counts up to date as records are added and removed, so search does not count again. We also check branch membership only on the records we actually visit.
| Version | Typical search | Slowest 5% |
|---|---|---|
| Before | 27.8 ms | 35.7 ms |
| With meaning priority | 35.5 ms | 44.0 ms |
| After the fix | 20.0 ms | 21.7 ms |
All of this stayed in Python. Our rule: measure first, and move a hot spot to Rust only when the measurements say so. If we do move it, its results are tested against the Python reference. This time it was not needed. The biggest win did not come from the language anyway; it came from not doing unneeded work.
What we took away
Both pieces of work taught the same lesson. Speed usually comes from never touching data you do not need, more than from a faster language. Keeping only the links readable on the phone while the text stays encrypted, and looking only in the relevant branches when searching, are two sides of that idea.