Package: toastedmarshmallow
Toasted Marshmallow implements a JIT for marshmallow that speeds up dumping
objects 10-25X (depending on your schema).  Toasted Marshmallow allows you to
have the great API thatMarshmallowprovides
without having to sacrifice performance!Benchmark Result:
  Original Time: 2682.61 usec/dump
  Optimized Time: 176.38 usec/dump
  Speed up: 15.21xEvenPyPybenefits fromtoastedmarshmallow!Benchmark Result:
    Original Time: 189.78 usec/dump
    Optimized Time: 20.03 usec/dump
    Speed up: 9.48xInstalling toastedmarshmallowpipinstalltoastedmarshmallowThis willalsoinstall a slightly-forkedmarshmallowthat includes some
hooks Toastedmarshmallow needs enable the JIT to run before falling back
to the original marshmallow code.  These changes are minimal making it easier
to track upstream.  You can find the changesHere.This means you shouldremovemarshmallowfrom your requirements and
replace it withtoastedmarshmallow.  By default there is no
difference unless you explicitly enable Toasted Marshmallow.Enabling Toasted MarshmallowEnabling Toasted Marshmallow on an existing Schema is just one line of code,
set thejitproperty on anySchemainstance totoastedmarshmallow.Jit.  For example:fromdatetimeimportdateimporttoastedmarshmallowfrommarshmallowimportSchema,fields,pprintclassArtistSchema(Schema):name=fields.Str()classAlbumSchema(Schema):title=fields.Str()release_date=fields.Date()artist=fields.Nested(ArtistSchema())schema=AlbumSchema()# Specify the jit method as toastedmarshmallow's jitschema.jit=toastedmarshmallow.Jit# And that's it!  Your dump methods are 15x faster!It’s also possible to use theMetaclass on theMarshmallowschema
to specify all instances of a givenSchemashould be optimized:importtoastedmarshmallowfrommarshmallowimportSchema,fields,pprintclassArtistSchema(Schema):classMeta:jit=toastedMarshmallow.Jitname=fields.Str()You can also enable Toasted Marshmallow globally by setting the environment
variableMARSHMALLOW_SCHEMA_DEFAULT_JITtotoastedmarshmallow.Jit.
Future versions of Toasted Marshmallow may make this the default.How it worksToasted Marshmallow works by generating code at runtime to optimize dumping
objects without going through layers and layers of reflection.  The generated
code optimistically assumes the objects being passed in are schematically valid,
falling back to the original marshmallow code on failure.For example, takingAlbumSchemafrom above, Toastedmarshmallow will
generate the following 3 methods:defInstanceSerializer(obj):res={}value=obj.release_date;value=value()ifcallable(value)elsevalue;res["release_date"]=_field_release_date__serialize(value,"release_date",obj)value=obj.artist;value=value()ifcallable(value)elsevalue;res["artist"]=_field_artist__serialize(value,"artist",obj)value=obj.title;value=value()ifcallable(value)elsevalue;value=str(value)ifvalueisnotNoneelseNone;res["title"]=valuereturnresdefDictSerializer(obj):res={}if"release_date"inobj:value=obj["release_date"];value=value()ifcallable(value)elsevalue;res["release_date"]=_field_release_date__serialize(value,"release_date",obj)if"artist"inobj:value=obj["artist"];value=value()ifcallable(value)elsevalue;res["artist"]=_field_artist__serialize(value,"artist",obj)if"title"inobj:value=obj["title"];value=value()ifcallable(value)elsevalue;value=str(value)ifvalueisnotNoneelseNone;res["title"]=valuereturnresdefHybridSerializer(obj):res={}try:value=obj["release_date"]except(KeyError,AttributeError,IndexError,TypeError):value=obj.release_datevalue=value;value=value()ifcallable(value)elsevalue;res["release_date"]=_field_release_date__serialize(value,"release_date",obj)try:value=obj["artist"]except(KeyError,AttributeError,IndexError,TypeError):value=obj.artistvalue=value;value=value()ifcallable(value)elsevalue;res["artist"]=_field_artist__serialize(value,"artist",obj)try:value=obj["title"]except(KeyError,AttributeError,IndexError,TypeError):value=obj.titlevalue=value;value=value()ifcallable(value)elsevalue;value=str(value)ifvalueisnotNoneelseNone;res["title"]=valuereturnresToastedmarshmallow will invoke the proper serializer based upon the input.Since Toastedmarshmallow is generating code at runtime, it’s critical you
re-use Schema objects.  If you’re creating a new Schema object every time you
serialize/deserialize an object you’ll likely have much worse performance.