-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Use Optional's built-in map #193
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
Conversation
@nadavrot This is a readability win, and should keep performance unchanged. |
I'm running tests and will merge if tests pass. |
That makes a lot of sense. How do you want the tests to be delivered? I have an XCTestCase here, it's not very high-tech. Just for future reference, how should we do this? I ran the test case, and the numbers are very close to each other. Sometimes the one is slightly faster, sometimes the other. I'm not a performance expert at all, but I'm pretty sure this is because of other processes on my computer. |
@nadavrot The SIL looks almost exactly the same to me, except for extra arguments in one of the basic blocks, and an extra retain/release for the closure. I think your team should look at this. There is no reason for the closure to be subject to any retains/releases here, I think. public struct LazyMapGeneratorTest<
Base : GeneratorType, Element
> : GeneratorType, SequenceType {
public mutating func next() -> Element? {
return _base.next().map(_transform)
}
public mutating func next_old() -> Element? {
let x = _base.next()
if x != nil {
return _transform(x!)
}
return nil
}
public var base: Base { return _base }
internal var _base: Base
internal var _transform: (Base.Element)->Element
}
public func callNextNew(xs: LazyMapGeneratorTest<Array<Int>.Generator, Int>) -> Int? {
var xs = xs
return xs.next()
}
public func callNextOld(xs: LazyMapGeneratorTest<Array<Int>.Generator, Int>) -> Int? {
var xs = xs
return xs.next_old()
} Old:
New:
The functional tests passed, by the way ( |
@aschwaighofer @gottesmm Do you have an opinion here? |
I looked at the code example and the generated code looks better for the map case. @gribozavr probably used a configuration which builds the stdlib with asserts. I am good with this going in. |
Use Optional's built-in map
Thank you! |
Don't remove 'get' statements with modifiers or attributes
Add an example on how to depend on SwiftSyntax with SwiftPM 5.2
Add 4.1 configuration to project_precommit_check
I was reading through the source and saw this usage of force-casting an optional. I think the code reads cleaner using the built-in
map
on Optional. I can also imagine anif let
would be a good alternative, in case using the built-inmap
has a performance overhead (not sure if this is the case for generic/rethrows functions, or if that gets compiled away).