essential-contributions / essential-server

Centralized implementation of the Essential declarative protocol
Apache License 2.0
0 stars 0 forks source link

Avoid encoding database data as base64 and use blobs #124

Open freesig opened 6 months ago

freesig commented 6 months ago

sqlite does support byte arrays as blobs but unfortunately it doesn't seem like rqlite does due to the json api. Rqlite suggests using inline hex which we should probably do. I'm not 100% sure but I think hex will be faster then base64? Also I'm not sure base64 gives us the correct ordering for keys.

freesig commented 6 months ago

@mitchmindtree do you know if hex might be faster then base64 and if base64 maintains the ordering of byte arrays?

mitchmindtree commented 6 months ago

I was originally under the impression that base64 does maintain ordering, but it appears it does not. Here's the original RFC where it mentions a base32hex encoding that does maintain sort order and compares it to base64 and base32 that do not.

I wonder if we should be considering using this base32hex encoding? Sounds like it preserves sort order while resulting in a more compact representation than hex.

I think hex is generally considered faster as it's basically a look-up table, whereas the base32 and 64 encodings require grouping of non-byte-aligned bits. I guess the tradeoff is bandwidth, e.g. hex encoding results in ~1.5x more characters than base64.

freesig commented 6 months ago

I should also note that using hex in the json calls results in the db storing the actual bytes as a blob and not a string. One kind of annoying thing is when reading blobs you have the choice of getting back either base64 or a json decimal array. Kinda annoying they have this inconsistency.