Skip to main content

Ralsina.Me — Roberto Alsina's website

Pickling can be expensive

When try­ing to se­ri­al­ize python data, of­ten the first thing we look at is pick­le. Well, some­times pick­le can be very ex­pen­sive!

Here Post is an elixir class, which means it's an ob­ject mapped to a re­la­tion­al database, and com­par­ing its prop­er­ties to some­thing is pret­ty much a piece of a SQL WHERE state­men­t.

>>> print Post.important==True
posts.important = ?

What hap­pens when we pick­le it?

>>> print len(pickle.dumps(Post.important==True))
27287
>>> print pickle.loads(s)
posts.important = :important_1

Yikes. What's a bet­ter so­lu­tion?

>>> print eval('Post.important==True')
posts.important = ?

As long as we are sure no weird da­ta will get in­to the eval...


Contents © 2000-2024 Roberto Alsina