From 7bd70a3ab22669ab07a06ee6b582d4865e91fdb0 Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Mon, 14 Oct 2024 12:30:45 +0200 Subject: [PATCH] Deferred heavy `permittedAttributes` call unless needed - this code is on the hotpath for the URL service and has shown to be slow for sites with a lot of posts - this is due to the overhead of the lodash functions we use here - we can take advantage of how JS executes if-statements and move the variable into the if-statement, which lazy evaluates it (for the URL service, this branch is not hit, so it's a big win) - this cuts about 2% from CPU time --- ghost/core/core/server/models/post.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ghost/core/core/server/models/post.js b/ghost/core/core/server/models/post.js index 8490048cef..25111588cc 100644 --- a/ghost/core/core/server/models/post.js +++ b/ghost/core/core/server/models/post.js @@ -1290,11 +1290,13 @@ Post = ghostBookshelf.Model.extend({ options.withRelated = _.union(['authors', 'tags', 'post_revisions', 'post_revisions.author'], options.withRelated || []); } - const META_ATTRIBUTES = _.without(ghostBookshelf.model('PostsMeta').prototype.permittedAttributes(), 'id', 'post_id'); - // NOTE: only include post_meta relation when requested in 'columns' or by default // optimization is needed to be able to perform .findAll on large SQLite datasets - if (!options.columns || (options.columns && _.intersection(META_ATTRIBUTES, options.columns).length)) { + if (!options.columns + || ( + options.columns + && _.intersection(_.without(ghostBookshelf.model('PostsMeta').prototype.permittedAttributes(), 'id', 'post_id'), options.columns).length) + ) { options.withRelated = _.union(['posts_meta'], options.withRelated || []); }