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

fix: avoid some allocations of strings #387

Merged
merged 3 commits into from Jul 3, 2021
Merged

Conversation

isqad
Copy link
Contributor

@isqad isqad commented Apr 24, 2020

Hello!
In this commit I suggest some optimizations for avoid unnecessary strings allocations

Before (our loop of mail delivery):

21747764  /localgems/addressable/lib/addressable/uri.rb:557

After:

no any conversions from nil to empty string

lib/addressable/uri.rb Outdated Show resolved Hide resolved
lib/addressable/uri.rb Outdated Show resolved Hide resolved
lib/addressable/uri.rb Outdated Show resolved Hide resolved
lib/addressable/uri.rb Outdated Show resolved Hide resolved
@isqad
Copy link
Contributor Author

isqad commented Apr 24, 2020

🍏 @dentarg

@normalized_host.force_encoding(Encoding::UTF_8) if @normalized_host
unless @normalized_host.empty?
@normalized_host.force_encoding(Encoding::UTF_8)
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it guaranteed that @normalized_host will always respond to #empty?. The previous code guarded against nil (Maybe it wasn't necessary or the test coverage we have isn't sufficient, I don't know)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah...ok, but @normalized_host can be empty string, so it is proper way?

if @normalized_host && !@normalized_host.empty?
  @normalized_host.force_encoding(Encoding::UTF_8)
end

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah

Copy link
Contributor

Choose a reason for hiding this comment

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

What if @normalized_host is an empty string but not UTF-8 encoded..?

Copy link
Contributor Author

@isqad isqad May 15, 2020

Choose a reason for hiding this comment

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

I think that it will not be have matter because url without host is wrong url

🤔

it is may be have sense to add condition on emptiness at this line:

authority << self.normalized_host

@isqad
Copy link
Contributor Author

isqad commented Apr 24, 2020

🆙

@isqad
Copy link
Contributor Author

isqad commented May 7, 2020

🤔

@sporkmonger
Copy link
Owner

Merged #383 before this one as requested.

@isqad
Copy link
Contributor Author

isqad commented May 15, 2020

Before:
https://travis-ci.org/github/sporkmonger/addressable/jobs/687158167

image
After:
https://travis-ci.org/github/sporkmonger/addressable/jobs/687287165
image

@ashmaroli
Copy link
Contributor

@dentarg Any updates on this?

@ashmaroli
Copy link
Contributor

pinging @dentarg @sporkmonger

@sporkmonger sporkmonger changed the base branch from master to main July 3, 2021 03:11
@sporkmonger sporkmonger merged commit e35926b into sporkmonger:main Jul 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants