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

Lazily initialise the properties of URLParser #146

Merged
merged 1 commit into from
Jan 18, 2019

Conversation

nethraravindran
Copy link
Contributor

@nethraravindran nethraravindran commented Jan 8, 2019

This increases the performance by 5-7% (approx)

@pushkarnk
Copy link
Contributor

@nethraravindran I think all of the properties of URLParser are read-write. Can you write setters for each of them here (except description) ?

Copy link
Contributor

@pushkarnk pushkarnk left a comment

Choose a reason for hiding this comment

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

Setters need for every property except description

@codecov-io
Copy link

codecov-io commented Jan 9, 2019

Codecov Report

Merging #146 into master will decrease coverage by 0.39%.
The diff coverage is 61.53%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master     #146     +/-   ##
=========================================
- Coverage   67.11%   66.71%   -0.4%     
=========================================
  Files          20       20             
  Lines        1271     1298     +27     
=========================================
+ Hits          853      866     +13     
- Misses        418      432     +14
Flag Coverage Δ
#KituraNet 66.71% <61.53%> (-0.4%) ⬇️
Impacted Files Coverage Δ
Sources/KituraNet/URLParser.swift 75% <61.53%> (-21.97%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 242c40a...17d720e. Read the comment docs.

@nethraravindran
Copy link
Contributor Author

@pushkarnk Thank you! I have addressed the review comments.

@nethraravindran nethraravindran self-assigned this Jan 9, 2019
@nethraravindran nethraravindran modified the milestones: 2018.26, 2019.01 Jan 9, 2019
Copy link
Contributor

@ianpartridge ianpartridge left a comment

Choose a reason for hiding this comment

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

Did you benchmark just delegating to each property to urlComponents? In other words, this:

- private lazy var _query: String? = urlComponents?.query

public var query: String? {
        get {
            return self.urlComponents?.query
        }
        set (newValue) {
-            _query = newValue
+            self.urlComponents?.query = newValue
        }
    }

Also, lazy in Swift is not thread-safe (see the "Note" at https://docs.swift.org/swift-book/LanguageGuide/Properties.html). Have you confirmed that instances of URLParser are never shared between threads?

self.queryParameters[$0.name] = $0.value
}
}
self.urlComponents = URLComponents(string: String(data: url, encoding: .utf8)!)
Copy link
Contributor

Choose a reason for hiding this comment

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

We shouldn't force-unwrap. If we cannot create the String then just set URLComponents to nil?

public var userinfo: String?
public var userinfo: String? {
get {
print("inside get")
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove this.

@nethraravindran nethraravindran force-pushed the urlParser branch 4 times, most recently from 2559683 to 2d644d4 Compare January 14, 2019 08:31
@nethraravindran
Copy link
Contributor Author

@ianpartridge Thanks for the review! I've addressed the changes. Butuserinfo and queryParameters can be get only properties since they depend on other parameters

@nethraravindran nethraravindran modified the milestones: 2019.01, 2019.02 Jan 17, 2019

/// The userid and password if specified in the URL.
public var userinfo: String?
public var userinfo: String? {
var _userInfo: String? = nil
Copy link
Contributor

Choose a reason for hiding this comment

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

You can save a line here:

if let username = urlComponents?.user, let password = urlComponents?.password {
    return "\(username):\(password)"
}
return nil

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure


/// The query parameters broken out.
public var queryParameters: [String: String] = [:]
public var queryParameters: [String: String] {
var _query: [String: String] = [:]
Copy link
Contributor

Choose a reason for hiding this comment

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

This will calculate the queryParameters dictionary every time the property is accessed. For this property we should cache the dictionary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ianpartridge I agree! Thanks for pointing this out :)

@pushkarnk pushkarnk merged commit 6655471 into Kitura:master Jan 18, 2019
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.

4 participants