Replit database: Update value directly or work on a copy

I am not sure how replit database is implemented. I use the database to store a large python dictionary. Will the code run faster if I create a deep copy of the dictionary before using it and save it back to the db when I am finished, or is it as fast to work directly on the dictionary object returned by the call the db?

Versions 1:

 import copy
my_dict= copy.deepcopy( db["my_dict"])
...
db["my_dict"] = my_dict

Versions 2:

my_dict= db["my_dict"]
...

do this:

import json
from replit import db

my_dict = json.loads(db.get_raw("key"))

1 Like

So you suggest that working on a deep copy is better and that the best way to make a deep copy is the code you propose, correct?

Um no, this gets the raw json of it and then interprets it via the json package

1 Like

Yes, that creates a copy of the dictionary instead of a reference to an observed dictionary. That means that the copy needs to be saved back to the db otherwise, any change would be lost. If, instead, I worked directly on the object produced by the call to db, there is no need to save the dictionary back to the db but there is a lot of overhead.
Thanks

Technically? It creates a string actually that is revealed to a dict

Do you know that the underlying implementation of the observed dictionary returned by the call to db . Is the observed dictionary a wrapper around a JSON string that is updated and restored every time the object is modified?

ReplDB is open source: replit-py/database.py at master · replit/replit-py · GitHub