From 4e928a937ce29acd02153c7a169f23453946e60e Mon Sep 17 00:00:00 2001 From: Maurits van Rees Date: Thu, 12 Dec 2024 14:43:16 +0100 Subject: [PATCH 1/5] Make primary field target adapter return early. When there is no primary field, do not go through all fields. First compare the field name, then the permission: comparing two strings is cheaper. If the field name matches, stop iterating over other fields: you have already found the field. --- news/1851.bugfix | 2 ++ src/plone/restapi/serializer/dxcontent.py | 15 ++++++++------- 2 files changed, 10 insertions(+), 7 deletions(-) create mode 100644 news/1851.bugfix diff --git a/news/1851.bugfix b/news/1851.bugfix new file mode 100644 index 000000000..36f1da159 --- /dev/null +++ b/news/1851.bugfix @@ -0,0 +1,2 @@ +Make primary field target adapter return early. +[maurits] diff --git a/src/plone/restapi/serializer/dxcontent.py b/src/plone/restapi/serializer/dxcontent.py index e497bb6b6..9e793dc97 100644 --- a/src/plone/restapi/serializer/dxcontent.py +++ b/src/plone/restapi/serializer/dxcontent.py @@ -220,23 +220,24 @@ def __init__(self, context, request): def __call__(self): primary_field_name = self.get_primary_field_name() + if not primary_field_name: + return for schema in iterSchemata(self.context): read_permissions = mergedTaggedValueDict(schema, READ_PERMISSIONS_KEY) for name, field in getFields(schema).items(): - if not self.check_permission(read_permissions.get(name), self.context): - continue - if name != primary_field_name: continue + if not self.check_permission(read_permissions.get(name), self.context): + return + target_adapter = queryMultiAdapter( (field, self.context, self.request), IPrimaryFieldTarget ) - if target_adapter: - target = target_adapter() - if target: - return target + if not target_adapter: + return + return target_adapter() def get_primary_field_name(self): fieldname = None From 69e2c948c1bf4f2820b8d2a72db72abdde8d0fdd Mon Sep 17 00:00:00 2001 From: David Glick Date: Thu, 12 Dec 2024 18:40:11 -0800 Subject: [PATCH 2/5] Update news/1851.bugfix --- news/1851.bugfix | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/news/1851.bugfix b/news/1851.bugfix index 36f1da159..dc2951605 100644 --- a/news/1851.bugfix +++ b/news/1851.bugfix @@ -1,2 +1 @@ -Make primary field target adapter return early. -[maurits] +Optimized performance of DexterityObjectPrimaryFieldTarget adapter. @maurits From db0c975a5c697538cdff6ea6cd173af63da390da Mon Sep 17 00:00:00 2001 From: Maurits van Rees Date: Mon, 16 Dec 2024 23:52:15 +0100 Subject: [PATCH 3/5] Primary field target: get primary_field_name directly instead of iterating over all fields. --- src/plone/restapi/serializer/dxcontent.py | 29 +++++++++++++---------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/plone/restapi/serializer/dxcontent.py b/src/plone/restapi/serializer/dxcontent.py index 9e793dc97..1130c492c 100644 --- a/src/plone/restapi/serializer/dxcontent.py +++ b/src/plone/restapi/serializer/dxcontent.py @@ -225,19 +225,22 @@ def __call__(self): for schema in iterSchemata(self.context): read_permissions = mergedTaggedValueDict(schema, READ_PERMISSIONS_KEY) - for name, field in getFields(schema).items(): - if name != primary_field_name: - continue - - if not self.check_permission(read_permissions.get(name), self.context): - return - - target_adapter = queryMultiAdapter( - (field, self.context, self.request), IPrimaryFieldTarget - ) - if not target_adapter: - return - return target_adapter() + field = getFields(schema).get(primary_field_name) + if field is None: + continue + print(primary_field_name) + if not self.check_permission( + read_permissions.get(primary_field_name), + self.context, + ): + return + + target_adapter = queryMultiAdapter( + (field, self.context, self.request), IPrimaryFieldTarget + ) + if not target_adapter: + return + return target_adapter() def get_primary_field_name(self): fieldname = None From a0ba91f932a570f8761a08c1ba33c8f4a0fb4da0 Mon Sep 17 00:00:00 2001 From: Maurits van Rees Date: Tue, 17 Dec 2024 00:18:57 +0100 Subject: [PATCH 4/5] Run tests on ubuntu-22.04. This was ubuntu-latest until last week. With ubuntu-24.04 on Python 3.12 Pillow is not installed correctly. See https://github.com/plone/plone.restapi/pull/1851#issuecomment-2547097591 --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 73447527b..72d7ada3f 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -3,7 +3,7 @@ on: [push, pull_request] jobs: build: if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name - runs-on: ubuntu-latest + runs-on: ubuntu-22.04 strategy: fail-fast: false matrix: From 995f1459a1fb7508a134d5135441b730c7aed1a5 Mon Sep 17 00:00:00 2001 From: David Glick Date: Mon, 16 Dec 2024 20:09:26 -0800 Subject: [PATCH 5/5] Update src/plone/restapi/serializer/dxcontent.py --- src/plone/restapi/serializer/dxcontent.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/plone/restapi/serializer/dxcontent.py b/src/plone/restapi/serializer/dxcontent.py index 1130c492c..eb0fc44c6 100644 --- a/src/plone/restapi/serializer/dxcontent.py +++ b/src/plone/restapi/serializer/dxcontent.py @@ -228,7 +228,6 @@ def __call__(self): field = getFields(schema).get(primary_field_name) if field is None: continue - print(primary_field_name) if not self.check_permission( read_permissions.get(primary_field_name), self.context,