An optimized tinyDB storage extension
Project description
Introduction
BetterJSONStorage is a faster ‘Storage Type’ for TinyDB. It uses the faster Orjson library for parsing the JSON and BLOSC2 for compression.
Parsing, compressing, and writing to the file is done by a seperate thread so reads don’t get blocked by slow fileIO. Smaller filesizes result in faster reading and writing (less diskIO). Even Reading is all done from memory.
These optimizations result in much faster reading and writing without loss of functionality.
A goal for the BetterJSONStorage project is to provide a drop in replacement for the default JSONStorage.
An example of how to implement BetterJSONStorage can be found below. Anything else can be found in the TinyDB docs.
*Database or JSON files created using other storage types (even the default one) are incompatible.*
Installing BetterJSONStorage
Install BetterJSONStorage from PyPi.
pip install BetterJSONStorage
Usage
context Manager
from pathlib import Path
from tinydb import TinyDB
from BetterJSONStorage import BetterJSONStorage
path = Path('relative/path/to/file.db')
with TinyDB(path, access_mode="r+", storage=BetterJSONStorage) as db:
db.insert({'int': 1, 'char': 'a'})
db.insert({'int': 1, 'char': 'b'})
extra
one difference from TinyDB default JSONStorage is that BetterJSONStorage is ReadOnly by default. use access_mode=’r+’ if you want to write as well.
All arguments except for the storage and access_mode argument are forwarded to the underlying storage. You can use this to pass additional keyword arguments to orjson.dumps(…) method.
For all options see the orjson documentation.
with TinyDB('file.db', option=orjson.OPT_NAIVE_UTC, storage=BetterJSONStorage) as db:
performance
See new performance numbers on the bottom. The entire test suite will be redone to be up to date but until that happens both the old (as they are more complete) as the new (as they are more comparable to modern hardware) will be kept in the readme.
The benchmarks are done on fixtures of real data:
citm_catalog.json, 1.7MiB, concert data, containing nested dictionaries of strings and arrays of integers, indented.
canada.json, 2.2MiB, coordinates of the Canadian border in GeoJSON format, containing floats and arrays, indented.
twitter.json, 631.5KiB, results of a search on Twitter for “一”, containing CJK strings, dictionaries of strings and arrays of dictionaries, indented.
data can be found here.
The exact same code is used for both BetterJSONStorage and the default JSONStorage. BetterJSONStorage is faster in almost* all situations and uses significantly less space on disk.
citm_catalog.json
storage |
used storage in kb |
vs. BetterJSONStorage |
---|---|---|
BetterJSONStorage |
83.3 |
1x |
default JSONStorage |
540 |
6.48x |
canada.json
storage |
used storage in kb |
vs. BetterJSONStorage |
---|---|---|
BetterJSONStorage |
1572 |
1x |
default JSONStorage |
2150 |
1.36x |
twitter.json
storage |
used storage in kb |
vs. BetterJSONStorage |
---|---|---|
BetterJSONStorage |
155 |
1x |
default JSONStorage |
574 |
3.7x |
Random generated JSON
JSON has been generated on json-generator. The generated JSON contains 140 items of about 0.7kb each. (100kb total) Every test was run 10 times and the average was taken.
- init times: the time it takes to instantiate the db and storage:
- BetterJSONStorage takes a bit more time to start but this only has to happen once in the beginning.This was a tradeoff that made it possible for the fast reads and writes we see from BetterJSONStorage.
storage |
time taken in μs |
---|---|
BetterJSONStorage |
181884 |
default JSONStorage |
145234 |
- insert time: the time it took to insert 140 items of around 0.7kb each:
- Because BetterJSONStorage uses a seperate thread for writing, the main thread is not blocked.This means no waiting for fileIO between subsequent writes.BetterJSONStorage makes sure every thing is writen correctly.
storage |
time taken in μs |
---|---|
BetterJSONStorage |
41448 |
default JSONStorage |
3019673 |
- read times: the time it took to read 140 items of around 0.7kb each:
- All reading is done from memory and not from disk.This means working with very large files can be an issue,but if you’re working on extremely large datasets TinyDB might also not be the right solution for you.This also means reading is extremely fast.Data in memory and on disk is always synced in the background so there should be no slowdown even with heavy writing in between reads.
storage |
time taken in μs |
---|---|
BetterJSONStorage |
1314 |
default JSONStorage |
13075 |
Graph
This is the same data that has een used above poured into a nice excel graph.
New Performance Numbers
New tests were run on a 2021 MacBook Pro running Ventura 13.0.1 and python 3.10.9 .
Both reading and writing test are the same for both Better as default JSONStorage.
BetterJSONStorage:
writing took: 71.001375ms
reading took: 29.283583ms
Default JSONStorage:
writing took: 7825.321125ms
reading took: 19438.65975ms
Total:
BetterJsonStorage: 240.7505ms
default jsonStorage: 27264.555167ms
relative time vs BetterJSONStorage:
BetterJSONStorage: 1x
JSONStorage: 113.25x
The Benchmark shows that the default JSONStorage takes 113 times as long to finish as the BetterJSONStorage. Filesizes are also way bigger with 8.5MB for the default JSONStorage and only 373 KB for BetterJSONStorage.
To test the performance for yourself, run the tests/benchmark/citm.py file.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file BetterJSONStorage-1.3.2.tar.gz
.
File metadata
- Download URL: BetterJSONStorage-1.3.2.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b11c539ecd58696cb556f207ade73014ed2e8fba0ff60b85293f8890387c7ea |
|
MD5 | 69ab481f49ec835a3a2fb7427b8a2981 |
|
BLAKE2b-256 | 86652a1aa99779489288c054d13bf8040071578662d7145e168aa5b115e299b8 |
File details
Details for the file BetterJSONStorage-1.3.2-py3-none-any.whl
.
File metadata
- Download URL: BetterJSONStorage-1.3.2-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 14861108a0fbf51ec020865888b5a1a3f590b13c0c3ac0186ab46319887bc622 |
|
MD5 | ee527457b3fea01a0d8bfa1dc68b30c9 |
|
BLAKE2b-256 | b06411dccdfd92521358ec27314c08d6e9379729c250739cf6fab25023b3c2cf |