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

[Performance] debug@autoclosureにする #208

Closed
ensan-hcl opened this issue May 28, 2023 · 3 comments · Fixed by #211
Closed

[Performance] debug@autoclosureにする #208

ensan-hcl opened this issue May 28, 2023 · 3 comments · Fixed by #211
Labels
good first issue Good for newcomers performance Make azooKey faster
Milestone

Comments

@ensan-hcl
Copy link
Owner

Releaseビルドでは出力しないタイプのprintとしてazooKeyではdebugを多用しているが、現在の実装は呼び出し時に引数が全て評価されるため、引数の計算自体は行われてしまう。そこで、以下のように変更することで若干高速になることを期待できる。実際軽く試したところ5%程度改善しそうだった。これを厳密に評価し、良好なら導入したい。

@_disfavoredOverload
@inlinable public func debug(_ items: Any...) {
#if DEBUG
    print(items.reduce(into: "") {$0.append(contentsOf: " \($1)")})
#endif
}

@inlinable public func debug(_ item1: @autoclosure () -> some Any) {
#if DEBUG
    print(item1())
#endif
}
@inlinable public func debug(_ item1: @autoclosure () -> some Any, _ item2: @autoclosure () -> some Any) {
#if DEBUG
    print(item1(), item2())
#endif
}
@inlinable public func debug(_ item1: @autoclosure () -> some Any, _ item2: @autoclosure () -> some Any, _ item3: @autoclosure () -> some Any) {
#if DEBUG
    print(item1(), item2(), item3())
#endif
}
@inlinable public func debug(_ item1: @autoclosure () -> some Any, _ item2: @autoclosure () -> some Any, _ item3: @autoclosure () -> some Any, _ item4: @autoclosure () -> some Any) {
#if DEBUG
    print(item1(), item2(), item3(), item4())
#endif
}
@inlinable public func debug(_ item1: @autoclosure () -> some Any, _ item2: @autoclosure () -> some Any, _ item3: @autoclosure () -> some Any, _ item4: @autoclosure () -> some Any, _ item5: @autoclosure () -> some Any) {
#if DEBUG
    print(item1(), item2(), item3(), item4(), item5())
#endif
}
@inlinable public func debug(_ item1: @autoclosure () -> some Any, _ item2: @autoclosure () -> some Any, _ item3: @autoclosure () -> some Any, _ item4: @autoclosure () -> some Any, _ item5: @autoclosure () -> some Any, _ item6: @autoclosure () -> some Any) {
#if DEBUG
    print(item1(), item2(), item3(), item4(), item5(), item6())
#endif
}
@ensan-hcl ensan-hcl added the performance Make azooKey faster label May 28, 2023
@ensan-hcl ensan-hcl added this to the Version 2.2 milestone May 28, 2023
@ensan-hcl ensan-hcl self-assigned this May 28, 2023
@ensan-hcl
Copy link
Owner Author

ensan-hcl commented May 28, 2023

Swift5.9でvariadic genericsが来たらこれも簡単に書けるようになるかな?多分こんな感じで良さそう

@inlinable public func debug<each T, U>(_ items: repeat (@autoclosure () -> each T), _ lastItem: @autoclosure () -> U) {
#if DEBUG
    repeat print(each items, terminator=" ")
    print(lastItem)
#endif
}

@ensan-hcl ensan-hcl added the good first issue Good for newcomers label May 28, 2023
@ensan-hcl ensan-hcl removed their assignment May 28, 2023
@ensan-hcl
Copy link
Owner Author

  • before
    • FullConversion: 328ms, 309ms
    • GradualConversion: 973ms, 970ms
  • after
    • FullConversion: 301ms, 305ms
    • GradualConversion: 895ms, 900ms

FullConversionだとあまり効果がないが、GradualConversionでは8%くらいの改善になってる!

@ensan-hcl
Copy link
Owner Author

some Anyにする意味があるのかも合わせてチェックする。こういう感じ。

@inlinable public func debug(_ item1: @autoclosure () -> Any) {
#if DEBUG
    print(item1())
#endif
}
  • after GradualConversion: 898ms, 884ms

あんまり変わらないか僅かにいいので、some AnyではなくAnyでいく。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers performance Make azooKey faster
Projects
None yet
1 participant