Skip to content

Performance impact of Nested with many vs List of Nested. #2872

@mspiller

Description

@mspiller

We noticed that Nested is slower than List of Nested.
Not sure why this happens but should they be the same?

import timeit

import marshmallow
from marshmallow import fields
from marshmallow import validate


class PropertySchema(marshmallow.Schema):
    name = fields.String(validate=[validate.Regexp('^[0-9a-zA-Z -]{1,100}$')], required=True)
    value = fields.String(validate=[validate.Regexp('^[ -~]{0,255}$')], required=True)


class TrackInteractionActionSchema(marshmallow.Schema):
    properties = fields.Nested(PropertySchema, many=True, required=False)

class TrackInteractionActionSchemaNew(marshmallow.Schema):
    properties = fields.List(fields.Nested(PropertySchema), required=False)


def load_schema(schema: marshmallow.Schema, data: dict) -> None:
    try:
        schema.load(data)
    except validate.ValidationError as err:
        pass


if __name__ == '__main__':
    data = {
        "properties": [
            {"name": "x!0", "value": "v"} for x in range(20000)
        ]
    }

    a = TrackInteractionActionSchema()
    print(timeit.timeit(lambda: load_schema(a, data), number=10))
    b = TrackInteractionActionSchemaNew()
    print(timeit.timeit(lambda: load_schema(b, data), number=10))

It is couple of times slower:

7.725316457916051
1.1829904590267688

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions