Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce string allocations from generating doc URLs #8392

Merged

Conversation

ashmaroli
Copy link
Member

  • This is a 🔨 code refactoring change.

Summary

Generating URLs from a drop (current route for all documents of a collection) results in a lot of string allocations because of use of non-mutating methods to manipulate strings.

This pull request replaces those non-mutating methods that mutate a duplicated version of long-term strings and mutate temporary strings.

@@ -94,7 +94,8 @@ def possible_keys(key)

def generate_url_from_drop(template)
template.gsub(%r!:([a-z_]+)!) do |match|
pool = possible_keys(match.sub(":", ""))
name = Regexp.last_match(1)
pool = name.end_with?("_") ? [name, name.chomp!("_")] : [name]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notes:

  • Regexp.last_match(1) here is equivalent to match.sub(":", "") but without the extra allocation.
  • The #possible_keys public method does not mutate the argument key but this inlined version modifies the string in-place.

Copy link
Member

@DirtyF DirtyF Sep 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given possible_keys is not used anywhere else, should rewrite or remove it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly, It is not a private method. So we'll have to bear the tech debt until v5.0

match.sub(":#{winner}", replacement)
end.squeeze("/")
match.sub!(":#{winner}", replacement)
end
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notes:

  • Calling String#squeeze here is redundant with respect to the bigger picture (Jekyll::URL.new.to_s) because the result from this method is passed onto Jekyll::URL#sanitize_url that removes extra "/" from input strings.
  • The local match variable value is not further required. So it can be mutated.

@ashmaroli ashmaroli requested a review from a team September 16, 2020 10:16
@DirtyF
Copy link
Member

DirtyF commented Sep 16, 2020

- Total allocated: 300.38 MB (3084063 objects)
- Total retained:  20.75 MB (113527 objects)
+ Total allocated: 300.31 MB (3082539 objects)
+ Total retained:  20.75 MB (113529 objects)

This seems to have an impact on build time:

- Docs site build:  6.03 seconds
+ Docs site build: 3.969 seconds

@ashmaroli
Copy link
Member Author

Its at times like these that I wish we could revive Utterson from its ashes..

@ashmaroli
Copy link
Member Author

ashmaroli commented Sep 17, 2020

Third Party Repo Profile Summary

--- master
+++ PR

- Total allocated: 282.43 MB (2467827 objects)
- Total retained:  37.81 MB (112069 objects)
+ Total allocated: 281.22 MB (2441051 objects)
+ Total retained:  37.81 MB (112070 objects)

@ashmaroli ashmaroli added the memory-optimization ⚡ Reduced memory usage for the work done label Sep 18, 2020
@ashmaroli
Copy link
Member Author

@jekyllbot: merge +fix

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
fix frozen-due-to-age memory-optimization ⚡ Reduced memory usage for the work done
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants