From 71784065cb5c2d70bc1b4c23b4573db3d6290855 Mon Sep 17 00:00:00 2001 From: Micah Benn Date: Sun, 1 Nov 2020 08:39:51 -0600 Subject: [PATCH 1/9] Add IndexPath benchmarks Add benchmarks for subscripts, max, min --- benchmark/CMakeLists.txt | 1 + benchmark/single-source/IndexPathTest.swift | 167 ++++++++++++++++++++ benchmark/utils/TestsUtils.swift | 2 +- benchmark/utils/main.swift | 2 + 4 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 benchmark/single-source/IndexPathTest.swift diff --git a/benchmark/CMakeLists.txt b/benchmark/CMakeLists.txt index f63860100e928..8f6ca9158bf60 100644 --- a/benchmark/CMakeLists.txt +++ b/benchmark/CMakeLists.txt @@ -98,6 +98,7 @@ set(SWIFT_BENCH_MODULES single-source/Hash single-source/Histogram single-source/HTTP2StateMachine + single-source/IndexPathTest single-source/InsertCharacter single-source/IntegerParsing single-source/Integrate diff --git a/benchmark/single-source/IndexPathTest.swift b/benchmark/single-source/IndexPathTest.swift new file mode 100644 index 0000000000000..f454b273cd793 --- /dev/null +++ b/benchmark/single-source/IndexPathTest.swift @@ -0,0 +1,167 @@ +//===--- IndexPathTest.swift -------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2020 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +import TestsUtils +import Foundation + +let size = 200 +let tags: [BenchmarkCategory] = [.validation, .api, .IndexPath] + +public let IndexPathTest = [ + BenchmarkInfo(name: "IndexPathSubscriptMutation", + runFunction: { n in run_IndexPathSubscriptMutation(n * 1000, size)}, + tags: tags), + BenchmarkInfo(name: "IndexPathSubscriptRangeMutation", + runFunction: { n in run_IndexPathSubscriptRangeMutation(n * 1000, size)}, + tags: tags), + + BenchmarkInfo(name: "IndexPathMaxBeginning", + runFunction: { n in run_IndexPathMaxBeginning(n * 1000)}, + tags: tags), + BenchmarkInfo(name: "IndexPathMaxMiddle", + runFunction: { n in run_IndexPathMaxMiddle(n * 1000)}, + tags: tags), + BenchmarkInfo(name: "IndexPathMaxEnd", + runFunction: { n in run_IndexPathMaxEnd(n * 1000)}, + tags: tags), + + BenchmarkInfo(name: "IndexPathMinBeginning", + runFunction: { n in run_IndexPathMinBeginning(n * 1000)}, + tags: tags), + BenchmarkInfo(name: "IndexPathMinMiddle", + runFunction: { n in run_IndexPathMinMiddle(n * 1000)}, + tags: tags), + BenchmarkInfo(name: "IndexPathMinEnd", + runFunction: { n in run_IndexPathMinEnd(n * 1000)}, + tags: tags), +] + +@inline(__always) +func indexPath(_ size: Int, + reversed: Bool = false) -> IndexPath { + let indexes = Array(0.. IndexPath { + var indexes = Array(0.. ()) { + for _ in 0.. IndexPath, + maxMinFunc: (inout IndexPath) -> Int?) { + for _ in 0.. Date: Sun, 1 Nov 2020 09:07:27 -0600 Subject: [PATCH 2/9] Fix formatting --- benchmark/single-source/IndexPathTest.swift | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/benchmark/single-source/IndexPathTest.swift b/benchmark/single-source/IndexPathTest.swift index f454b273cd793..e4d2e239bc60c 100644 --- a/benchmark/single-source/IndexPathTest.swift +++ b/benchmark/single-source/IndexPathTest.swift @@ -63,18 +63,18 @@ func indexPath(_ size: Int, // Subscript Mutations @inline(__always) -func subscriptMutation(n: Int, - mutations: Int, - mutate: (inout IndexPath, Int) -> ()) { +func subscriptMutation(n: Int, + mutations: Int, + mutate: (inout IndexPath, Int) -> ()) { for _ in 0.. Date: Sun, 1 Nov 2020 15:07:24 -0600 Subject: [PATCH 3/9] Update per style guildelines --- benchmark/single-source/IndexPathTest.swift | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/benchmark/single-source/IndexPathTest.swift b/benchmark/single-source/IndexPathTest.swift index e4d2e239bc60c..0a864bea22b57 100644 --- a/benchmark/single-source/IndexPathTest.swift +++ b/benchmark/single-source/IndexPathTest.swift @@ -1,4 +1,4 @@ -//===--- IndexPathTest.swift -------------------------------------------===// +//===--- IndexPathTest.swift ----------------------------------------------===// // // This source file is part of the Swift.org open source project // @@ -46,15 +46,13 @@ public let IndexPathTest = [ ] @inline(__always) -func indexPath(_ size: Int, - reversed: Bool = false) -> IndexPath { +func indexPath(_ size: Int, reversed: Bool = false) -> IndexPath { let indexes = Array(0.. IndexPath { +func indexPath(_ size: Int, middle: Int) -> IndexPath { var indexes = Array(0.. Date: Sun, 1 Nov 2020 15:25:30 -0600 Subject: [PATCH 4/9] Fix indentation, move params to new line --- benchmark/single-source/IndexPathTest.swift | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/benchmark/single-source/IndexPathTest.swift b/benchmark/single-source/IndexPathTest.swift index 0a864bea22b57..d467288215508 100644 --- a/benchmark/single-source/IndexPathTest.swift +++ b/benchmark/single-source/IndexPathTest.swift @@ -61,7 +61,8 @@ func indexPath(_ size: Int, middle: Int) -> IndexPath { // Subscript Mutations @inline(__always) -func subscriptMutation(n: Int, +func subscriptMutation( + n: Int, mutations: Int, mutate: (inout IndexPath, Int) -> ()) { for _ in 0.. IndexPath, maxMinFunc: (inout IndexPath) -> Int?) { for _ in 0.. Date: Sun, 1 Nov 2020 19:20:08 -0600 Subject: [PATCH 5/9] Apply swift-format --- benchmark/single-source/IndexPathTest.swift | 202 +++++++++++--------- 1 file changed, 116 insertions(+), 86 deletions(-) diff --git a/benchmark/single-source/IndexPathTest.swift b/benchmark/single-source/IndexPathTest.swift index d467288215508..d2b4c50a98aed 100644 --- a/benchmark/single-source/IndexPathTest.swift +++ b/benchmark/single-source/IndexPathTest.swift @@ -10,52 +10,60 @@ // //===----------------------------------------------------------------------===// -import TestsUtils import Foundation +import TestsUtils let size = 200 let tags: [BenchmarkCategory] = [.validation, .api, .IndexPath] public let IndexPathTest = [ - BenchmarkInfo(name: "IndexPathSubscriptMutation", - runFunction: { n in run_IndexPathSubscriptMutation(n * 1000, size)}, - tags: tags), - BenchmarkInfo(name: "IndexPathSubscriptRangeMutation", - runFunction: { n in run_IndexPathSubscriptRangeMutation(n * 1000, size)}, - tags: tags), - - BenchmarkInfo(name: "IndexPathMaxBeginning", - runFunction: { n in run_IndexPathMaxBeginning(n * 1000)}, - tags: tags), - BenchmarkInfo(name: "IndexPathMaxMiddle", - runFunction: { n in run_IndexPathMaxMiddle(n * 1000)}, - tags: tags), - BenchmarkInfo(name: "IndexPathMaxEnd", - runFunction: { n in run_IndexPathMaxEnd(n * 1000)}, - tags: tags), - - BenchmarkInfo(name: "IndexPathMinBeginning", - runFunction: { n in run_IndexPathMinBeginning(n * 1000)}, - tags: tags), - BenchmarkInfo(name: "IndexPathMinMiddle", - runFunction: { n in run_IndexPathMinMiddle(n * 1000)}, - tags: tags), - BenchmarkInfo(name: "IndexPathMinEnd", - runFunction: { n in run_IndexPathMinEnd(n * 1000)}, - tags: tags), + BenchmarkInfo( + name: "IndexPathSubscriptMutation", + runFunction: { n in run_IndexPathSubscriptMutation(n * 1000, size) }, + tags: tags), + BenchmarkInfo( + name: "IndexPathSubscriptRangeMutation", + runFunction: { n in run_IndexPathSubscriptRangeMutation(n * 1000, size) }, + tags: tags), + + BenchmarkInfo( + name: "IndexPathMaxBeginning", + runFunction: { n in run_IndexPathMaxBeginning(n * 1000) }, + tags: tags), + BenchmarkInfo( + name: "IndexPathMaxMiddle", + runFunction: { n in run_IndexPathMaxMiddle(n * 1000) }, + tags: tags), + BenchmarkInfo( + name: "IndexPathMaxEnd", + runFunction: { n in run_IndexPathMaxEnd(n * 1000) }, + tags: tags), + + BenchmarkInfo( + name: "IndexPathMinBeginning", + runFunction: { n in run_IndexPathMinBeginning(n * 1000) }, + tags: tags), + BenchmarkInfo( + name: "IndexPathMinMiddle", + runFunction: { n in run_IndexPathMinMiddle(n * 1000) }, + tags: tags), + BenchmarkInfo( + name: "IndexPathMinEnd", + runFunction: { n in run_IndexPathMinEnd(n * 1000) }, + tags: tags), ] @inline(__always) func indexPath(_ size: Int, reversed: Bool = false) -> IndexPath { - let indexes = Array(0.. IndexPath { - var indexes = Array(0.. IndexPath { func subscriptMutation( n: Int, mutations: Int, - mutate: (inout IndexPath, Int) -> ()) { - for _ in 0.. Void +) { + for _ in 0.. IndexPath, - maxMinFunc: (inout IndexPath) -> Int?) { - for _ in 0.. IndexPath, + maxMinFunc: (inout IndexPath) -> Int? +) { + for _ in 0.. Date: Sun, 1 Nov 2020 23:05:00 -0600 Subject: [PATCH 6/9] Decrease load 16x, fix naming, fix mem usage Fixes mem usage of run_IndexPathSubscriptRangeMutation --- benchmark/single-source/IndexPathTest.swift | 34 ++++++++++----------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/benchmark/single-source/IndexPathTest.swift b/benchmark/single-source/IndexPathTest.swift index d2b4c50a98aed..815031404df2e 100644 --- a/benchmark/single-source/IndexPathTest.swift +++ b/benchmark/single-source/IndexPathTest.swift @@ -18,38 +18,38 @@ let tags: [BenchmarkCategory] = [.validation, .api, .IndexPath] public let IndexPathTest = [ BenchmarkInfo( - name: "IndexPathSubscriptMutation", - runFunction: { n in run_IndexPathSubscriptMutation(n * 1000, size) }, + name: "IndexPath.Subscript.Mutation", + runFunction: { n in run_IndexPathSubscriptMutation(n * 60, size) }, tags: tags), BenchmarkInfo( - name: "IndexPathSubscriptRangeMutation", - runFunction: { n in run_IndexPathSubscriptRangeMutation(n * 1000, size) }, + name: "IndexPath.Subscript.Range.Mutation", + runFunction: { n in run_IndexPathSubscriptRangeMutation(n * 60, size) }, tags: tags), BenchmarkInfo( - name: "IndexPathMaxBeginning", - runFunction: { n in run_IndexPathMaxBeginning(n * 1000) }, + name: "IndexPath.Max.Beginning", + runFunction: { n in run_IndexPathMaxBeginning(n * 60) }, tags: tags), BenchmarkInfo( - name: "IndexPathMaxMiddle", - runFunction: { n in run_IndexPathMaxMiddle(n * 1000) }, + name: "IndexPath.Max.Middle", + runFunction: { n in run_IndexPathMaxMiddle(n * 60) }, tags: tags), BenchmarkInfo( - name: "IndexPathMaxEnd", - runFunction: { n in run_IndexPathMaxEnd(n * 1000) }, + name: "IndexPath.Max.End", + runFunction: { n in run_IndexPathMaxEnd(n * 60) }, tags: tags), BenchmarkInfo( - name: "IndexPathMinBeginning", - runFunction: { n in run_IndexPathMinBeginning(n * 1000) }, + name: "IndexPath.Min.Beginning", + runFunction: { n in run_IndexPathMinBeginning(n * 60) }, tags: tags), BenchmarkInfo( - name: "IndexPathMinMiddle", - runFunction: { n in run_IndexPathMinMiddle(n * 1000) }, + name: "IndexPath.Min.Middle", + runFunction: { n in run_IndexPathMinMiddle(n * 60) }, tags: tags), BenchmarkInfo( - name: "IndexPathMinEnd", - runFunction: { n in run_IndexPathMinEnd(n * 1000) }, + name: "IndexPath.Min.End", + runFunction: { n in run_IndexPathMinEnd(n * 60) }, tags: tags), ] @@ -95,7 +95,7 @@ public func run_IndexPathSubscriptMutation(_ n: Int, _ count: Int) { @inline(never) public func run_IndexPathSubscriptRangeMutation(_ n: Int, _ count: Int) { subscriptMutation( - n: count, mutations: count, + n: n, mutations: count, mutate: { ip, i in ip[0.. Date: Sun, 1 Nov 2020 23:46:43 -0600 Subject: [PATCH 7/9] Pre-construct indexPath, use setUpFunction --- benchmark/single-source/IndexPathTest.swift | 116 ++++++++++++-------- 1 file changed, 68 insertions(+), 48 deletions(-) diff --git a/benchmark/single-source/IndexPathTest.swift b/benchmark/single-source/IndexPathTest.swift index 815031404df2e..645dcab355773 100644 --- a/benchmark/single-source/IndexPathTest.swift +++ b/benchmark/single-source/IndexPathTest.swift @@ -14,43 +14,71 @@ import Foundation import TestsUtils let size = 200 +let increasingIndexPath = indexPath(size) +let decreasingIndexPath = indexPath(size, reversed: true) +let increasingMaxMiddleIndexPath = indexPath(size, middle: size + 1) +let increasingMinMiddleIndexPath = indexPath(size, middle: -1) let tags: [BenchmarkCategory] = [.validation, .api, .IndexPath] public let IndexPathTest = [ BenchmarkInfo( name: "IndexPath.Subscript.Mutation", - runFunction: { n in run_IndexPathSubscriptMutation(n * 60, size) }, - tags: tags), + runFunction: { n in + run_IndexPathSubscriptMutation(n * 10, size, increasingIndexPath) + }, + tags: tags, + setUpFunction: { blackHole(increasingIndexPath) }), BenchmarkInfo( name: "IndexPath.Subscript.Range.Mutation", - runFunction: { n in run_IndexPathSubscriptRangeMutation(n * 60, size) }, - tags: tags), + runFunction: { n in + run_IndexPathSubscriptRangeMutation(n, size, increasingIndexPath) + }, + tags: tags, + setUpFunction: { blackHole(increasingIndexPath) }), BenchmarkInfo( name: "IndexPath.Max.Beginning", - runFunction: { n in run_IndexPathMaxBeginning(n * 60) }, - tags: tags), + runFunction: { n in + run_IndexPathMaxBeginning(n * 25, decreasingIndexPath) + }, + tags: tags, + setUpFunction: { blackHole(decreasingIndexPath) }), BenchmarkInfo( name: "IndexPath.Max.Middle", - runFunction: { n in run_IndexPathMaxMiddle(n * 60) }, - tags: tags), + runFunction: { n in + run_IndexPathMaxMiddle(n * 25, increasingMaxMiddleIndexPath) + }, + tags: tags, + setUpFunction: { blackHole(increasingMaxMiddleIndexPath) }), BenchmarkInfo( name: "IndexPath.Max.End", - runFunction: { n in run_IndexPathMaxEnd(n * 60) }, - tags: tags), + runFunction: { n in + run_IndexPathMaxEnd(n * 25, increasingIndexPath) + }, + tags: tags, + setUpFunction: { blackHole(increasingIndexPath) }), BenchmarkInfo( name: "IndexPath.Min.Beginning", - runFunction: { n in run_IndexPathMinBeginning(n * 60) }, - tags: tags), + runFunction: { n in + run_IndexPathMinBeginning(n * 25, increasingIndexPath) + }, + tags: tags, + setUpFunction: { blackHole(increasingIndexPath) }), BenchmarkInfo( name: "IndexPath.Min.Middle", - runFunction: { n in run_IndexPathMinMiddle(n * 60) }, - tags: tags), + runFunction: { n in + run_IndexPathMinMiddle(n * 25, increasingMinMiddleIndexPath) + }, + tags: tags, + setUpFunction: { blackHole(increasingMinMiddleIndexPath) }), BenchmarkInfo( name: "IndexPath.Min.End", - runFunction: { n in run_IndexPathMinEnd(n * 60) }, - tags: tags), + runFunction: { n in + run_IndexPathMinEnd(n * 25, decreasingIndexPath) + }, + tags: tags, + setUpFunction: { blackHole(decreasingIndexPath) }), ] @inline(__always) @@ -72,30 +100,34 @@ func indexPath(_ size: Int, middle: Int) -> IndexPath { func subscriptMutation( n: Int, mutations: Int, + indexPath: IndexPath, mutate: (inout IndexPath, Int) -> Void ) { for _ in 0.. IndexPath, + indexPath: IndexPath, maxMinFunc: (inout IndexPath) -> Int? ) { for _ in 0.. Date: Fri, 6 Nov 2020 20:11:16 -0600 Subject: [PATCH 8/9] Condense benchmarks --- benchmark/single-source/IndexPathTest.swift | 139 ++++---------------- 1 file changed, 28 insertions(+), 111 deletions(-) diff --git a/benchmark/single-source/IndexPathTest.swift b/benchmark/single-source/IndexPathTest.swift index 645dcab355773..290886ac9fbd2 100644 --- a/benchmark/single-source/IndexPathTest.swift +++ b/benchmark/single-source/IndexPathTest.swift @@ -35,50 +35,14 @@ public let IndexPathTest = [ }, tags: tags, setUpFunction: { blackHole(increasingIndexPath) }), - - BenchmarkInfo( - name: "IndexPath.Max.Beginning", - runFunction: { n in - run_IndexPathMaxBeginning(n * 25, decreasingIndexPath) - }, - tags: tags, - setUpFunction: { blackHole(decreasingIndexPath) }), - BenchmarkInfo( - name: "IndexPath.Max.Middle", - runFunction: { n in - run_IndexPathMaxMiddle(n * 25, increasingMaxMiddleIndexPath) - }, - tags: tags, - setUpFunction: { blackHole(increasingMaxMiddleIndexPath) }), BenchmarkInfo( - name: "IndexPath.Max.End", - runFunction: { n in - run_IndexPathMaxEnd(n * 25, increasingIndexPath) - }, - tags: tags, - setUpFunction: { blackHole(increasingIndexPath) }), - + name: "IndexPath.Max", + runFunction: { n in run_IndexPathMax(n * 10) }, + tags: tags), BenchmarkInfo( - name: "IndexPath.Min.Beginning", - runFunction: { n in - run_IndexPathMinBeginning(n * 25, increasingIndexPath) - }, - tags: tags, - setUpFunction: { blackHole(increasingIndexPath) }), - BenchmarkInfo( - name: "IndexPath.Min.Middle", - runFunction: { n in - run_IndexPathMinMiddle(n * 25, increasingMinMiddleIndexPath) - }, - tags: tags, - setUpFunction: { blackHole(increasingMinMiddleIndexPath) }), - BenchmarkInfo( - name: "IndexPath.Min.End", - runFunction: { n in - run_IndexPathMinEnd(n * 25, decreasingIndexPath) - }, - tags: tags, - setUpFunction: { blackHole(decreasingIndexPath) }), + name: "IndexPath.Min", + runFunction: { n in run_IndexPathMin(n * 10) }, + tags: tags), ] @inline(__always) @@ -133,81 +97,34 @@ public func run_IndexPathSubscriptRangeMutation( }) } -// Max, Min - -@inline(__always) -func maxMin( - n: Int, - indexPath: IndexPath, - maxMinFunc: (inout IndexPath) -> Int? -) { - for _ in 0.. Date: Mon, 9 Nov 2020 17:04:59 -0600 Subject: [PATCH 9/9] Fix styling, optimize --- benchmark/single-source/IndexPathTest.swift | 48 +++++++++++---------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/benchmark/single-source/IndexPathTest.swift b/benchmark/single-source/IndexPathTest.swift index 290886ac9fbd2..c85d5392d3c80 100644 --- a/benchmark/single-source/IndexPathTest.swift +++ b/benchmark/single-source/IndexPathTest.swift @@ -23,35 +23,39 @@ let tags: [BenchmarkCategory] = [.validation, .api, .IndexPath] public let IndexPathTest = [ BenchmarkInfo( name: "IndexPath.Subscript.Mutation", - runFunction: { n in - run_IndexPathSubscriptMutation(n * 10, size, increasingIndexPath) - }, + runFunction: run_IndexPathSubscriptMutation, tags: tags, setUpFunction: { blackHole(increasingIndexPath) }), BenchmarkInfo( name: "IndexPath.Subscript.Range.Mutation", - runFunction: { n in - run_IndexPathSubscriptRangeMutation(n, size, increasingIndexPath) - }, + runFunction: run_IndexPathSubscriptRangeMutation, tags: tags, setUpFunction: { blackHole(increasingIndexPath) }), BenchmarkInfo( name: "IndexPath.Max", - runFunction: { n in run_IndexPathMax(n * 10) }, - tags: tags), + runFunction: run_IndexPathMax, + tags: tags, + setUpFunction: { + blackHole(decreasingIndexPath) + blackHole(increasingMaxMiddleIndexPath) + blackHole(increasingIndexPath) + }), BenchmarkInfo( name: "IndexPath.Min", - runFunction: { n in run_IndexPathMin(n * 10) }, - tags: tags), + runFunction: run_IndexPathMin, + tags: tags, + setUpFunction: { + blackHole(increasingIndexPath) + blackHole(increasingMinMiddleIndexPath) + blackHole(decreasingIndexPath) + }), ] -@inline(__always) func indexPath(_ size: Int, reversed: Bool = false) -> IndexPath { let indexes = Array(0.. IndexPath { var indexes = Array(0..