Commit 68512da6 authored by Mickaël Desfrênes's avatar Mickaël Desfrênes
Browse files

add optimizations comments

parent ea834de3
Loading
Loading
Loading
Loading
+8
Changes for src/rpc/methods.py: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -186,6 +186,9 @@ def _prevent_exiftool_metadata_modification(


def _collections_with_serialization_data(query_set: QuerySet) -> QuerySet:
    # Collection serialization includes counts, tags, metadata, and representatives.
    # Load them with the page query so list/search endpoints do not run those
    # lookups once per collection.
    return (
        query_set.select_related("parent", "representative", "representative__file")
        .prefetch_related(
@@ -212,6 +215,8 @@ def _collections_with_serialization_data(query_set: QuerySet) -> QuerySet:
def _resources_with_serialization_data(
    query_set: QuerySet, include_metas: bool
) -> QuerySet:
    # File serialization needs the subtype row, file type, project, and tags.
    # Metadata is only prefetched for callers that include it in the response.
    query_set = query_set.select_related(
        "file",
        "file__project",
@@ -229,6 +234,9 @@ def _resources_with_serialization_data(
def _attach_collection_ancestors(collections: List[Collection], project: Project):
    if not collections:
        return
    # Ancestors are serialized as a compact list, not model instances. Hydrate the
    # parent chain for the current page in batches and attach the serialized value,
    # avoiding a database walk from each collection back to the root.
    collection_by_id = {collection.pk: collection for collection in collections}
    missing_parent_ids = {
        collection.parent_id
+6
Changes for src/rpc/serializers.py: 6 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -97,6 +97,8 @@ def collection(
    tags = []
    for tag_instance in collection_instance.tags.all():
        tags.append(tag(tag_instance, cache=cache))
    # List/search endpoints annotate these counts before serialization. Keep the
    # fallback for direct calls that serialize a single collection.
    resources_count = getattr(
        collection_instance,
        "rpc_resources_count",
@@ -113,6 +115,8 @@ def collection(
    )
    if children_count is None:
        children_count = collection_instance.children().count()
    # Ancestors may be attached by the caller after batch hydration; otherwise
    # use the model method for standalone serialization.
    ancestors = getattr(collection_instance, "_rpc_ancestors", None)
    if ancestors is None:
        ancestors = [
@@ -237,6 +241,8 @@ def file(
                props.append(metadata_resource_value(prop, cache=cache))
        props = sorted(props, key=lambda d: d["meta"]["id"])

    # Hot list paths select_related these fields; the cache check avoids a
    # SerializerCache lookup or project relation fetch when Django already has them.
    if _related_field_is_cached(file_instance, "file_type"):
        file_type_instance = file_instance.file_type
    else: