Problem/Motivation

The function feeds_tokens() currently does quite some processing, including a db query, that can be avoided.
I don't see a good reason for calling feeds_get_feed_nid() and / or node_load() before we know if the feed-source token is required at all.
Further it doesn't seem to make sense to iterate over all tokens and then compare the array key with our provided token if we can do isset($tokens['feed-source']) on the associative token array.

Proposed resolution

Don't iterate over the tokens, just check if our token is requested.
And just if this is the case execute the required db query to get $feed_nid and the later node_load()

Yay less db queries :)

Remaining tasks

Reviews needed.

User interface changes

None.

API changes

None.

Data model changes

None.

Comments

megachriz’s picture

Status: Needs review » Needs work

T

+++ b/feeds.tokens.inc
@@ -29,21 +29,18 @@ function feeds_tokens($type, $tokens, array $data, array $options) {
+    if (isset($tokens['feed-source'])) {
...
+      if ($feed_nid && $feed_source = node_load($feed_nid)) {

This could result into a undefined variable error. If $tokens['feeds-source'] does not exist or the feed node failed to load, then $feeds_source is undefined.

But the variable is used here in the patch:

+++ b/feeds.tokens.inc
@@ -29,21 +29,18 @@ function feeds_tokens($type, $tokens, array $data, array $options) {
+    // Chained node token relationships.
+    if ($feed_source_tokens = token_find_with_prefix($tokens, 'feed-source')) {
+      $replacements += token_generate('node', $feed_source_tokens, array('node' => $feed_source), $options);

I think that this needs a test for token replacement, to ensure that tokens like [node:feed-source] and [node:feed-source:field_link] are replaced.

I wonder though if these feed-source tokens are an exact copy of the feed-node tokens. The feed-node tokens become available when the entity module is installed. But existing sites may use these tokens, so I think we can not just delete them.

maximpodorov’s picture

Status: Needs work » Needs review
StatusFileSize
new1.02 KB

Here is the updated patch which is free from these problems.

megachriz’s picture

Great! Thanks for the patch. I've created two tests for this issue: one to ensure tokens get replaced and one to check if there is a performance gain with the provided patch.

Two patches. The first is only the tests, for which FeedsTokenTest::testPerformance() should fail, but FeedsTokenTest::testFeedsTokens() should pass. The second is with the fix included.

The last submitted patch, 3: feeds-optimize-token-performance-2537926-3-test-only.patch, failed testing.

megachriz’s picture

I realized the automated test did not include token replacement for [node:feeds-source], so I added that one to FeedsTokenTest::testFeedsTokens() as well.

The last submitted patch, 5: feeds-optimize-token-performance-2537926-5-test-only.patch, failed testing.

megachriz’s picture

While I'm at it, the foreach loop could be killed as well. There is no need to go through the whole list of tokens if we are only looking for one in particular.

Status: Needs review » Needs work

The last submitted patch, 7: feeds-optimize-token-performance-2537926-7.patch, failed testing.

megachriz’s picture

Status: Needs work » Needs review
StatusFileSize
new5.28 KB
new599 bytes

Thank you tests! I indeed made a mistake in the last patch.

megachriz’s picture

Status: Needs review » Fixed

Committed #9. And thanks for creating the fix, maximpodorov!

das-peter’s picture

Awesome guys, thank you so much for continuing this!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.