def encode(*args): # args: (msgid, flags, name, args) # (We used to have a global pickler, but that's not thread-safe. :-( ) # It's not thread safe if, in the couse of pickling, we call the # Python interpeter, which releases the GIL. # Note that args may contain very large binary pickles already; for # this reason, it's important to use proto 1 (or higher) pickles here # too. For a long time, this used proto 0 pickles, and that can # bloat our pickle to 4x the size (due to high-bit and control bytes # being represented by \xij escapes in proto 0). # Undocumented: cPickle.Pickler accepts a lone protocol argument; # pickle.py does not. if PY3: # XXX: Py3: Needs optimization. f = BytesIO() pickler = Pickler(f, 3) pickler.fast = 1 pickler.dump(args) res = f.getvalue() return res else: pickler = Pickler(1) pickler.fast = 1 # Only CPython's cPickle supports dumping # and returning in one operation: # return pickler.dump(args, 1) # For PyPy we must return the value; fortunately this # works the same on CPython and is no more expensive pickler.dump(args) return pickler.getvalue()
def encode(*args): # args: (msgid, flags, name, args) # (We used to have a global pickler, but that's not thread-safe. :-( ) # It's not thread safe if, in the couse of pickling, we call the # Python interpeter, which releases the GIL. # Note that args may contain very large binary pickles already; for # this reason, it's important to use proto 1 (or higher) pickles here # too. For a long time, this used proto 0 pickles, and that can # bloat our pickle to 4x the size (due to high-bit and control bytes # being represented by \xij escapes in proto 0). # Undocumented: cPickle.Pickler accepts a lone protocol argument; # pickle.py does not. if PY3: # XXX: Py3: Needs optimization. f = BytesIO() pickler = Pickler(f, 3) pickler.fast = 1 pickler.dump(args) res = f.getvalue() return res else: pickler = Pickler(1) pickler.fast = 1 return pickler.dump(args, 1)
def fast_encode(): # Only use in cases where you *know* the data contains only basic # Python objects pickler = Pickler(1) pickler.fast = 1 dump = pickler.dump def fast_encode(*args): return dump(args, 1) return fast_encode
def encode(*args): # args: (msgid, flags, name, args) # (We used to have a global pickler, but that's not thread-safe. :-( ) # It's not thread safe if, in the couse of pickling, we call the # Python interpeter, which releases the GIL. # Note that args may contain very large binary pickles already; for # this reason, it's important to use proto 1 (or higher) pickles here # too. For a long time, this used proto 0 pickles, and that can # bloat our pickle to 4x the size (due to high-bit and control bytes # being represented by \xij escapes in proto 0). # Undocumented: cPickle.Pickler accepts a lone protocol argument; # pickle.py does not. # XXX: Py3: Needs optimization. f = BytesIO() pickler = Pickler(f, 3) pickler.fast = 1 pickler.dump(args) res = f.getvalue() return res