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

Use swiftformat #43

Merged
merged 2 commits into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ on:

jobs:

"sanity-Tests":
runs-on: macOS-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install swiftformat
run: brew install swiftformat
- name: Run sanity
run: ./scripts/sanity.sh .

"tuxOS-Tests":
runs-on: ubuntu-latest
strategy:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ let package = Package(
products: [
.library(name: "AtomicCounter", targets: ["AtomicCounter"]),
],
dependencies: [ ],
dependencies: [],
targets: [
.target(
name: "AtomicCounter",
dependencies: []),
dependencies: []
),
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,47 @@
//
//===----------------------------------------------------------------------===//

import Foundation
import AtomicCounter
import Foundation
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
import Darwin
import Darwin
#else
import Glibc
import Glibc
#endif

func measureAll(_ fn: () -> Int) -> [[String: Int]] {
func measureOne(_ fn: () -> Int) -> [String: Int] {
AtomicCounter.reset_free_counter()
AtomicCounter.reset_malloc_counter()
AtomicCounter.reset_malloc_bytes_counter()
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
autoreleasepool {
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
autoreleasepool {
_ = fn()
}
#else
_ = fn()
}
#else
_ = fn()
#endif
#endif
usleep(100_000) // allocs/frees happen on multiple threads, allow some cool down time
let frees = AtomicCounter.read_free_counter()
let mallocs = AtomicCounter.read_malloc_counter()
let mallocedBytes = AtomicCounter.read_malloc_bytes_counter()
return [
"total_allocations": mallocs,
"total_allocated_bytes": mallocedBytes,
"remaining_allocations": mallocs - frees
"remaining_allocations": mallocs - frees,
]
}

_ = measureOne(fn) /* pre-heat and throw away */
usleep(100_000) // allocs/frees happen on multiple threads, allow some cool down time
var measurements: [[String: Int]] = []
for _ in 0..<10 {
for _ in 0 ..< 10 {
measurements.append(measureOne(fn))
}
return measurements
}

func measureAndPrint(desc: String, fn: () -> Int) -> Void {
func measureAndPrint(desc: String, fn: () -> Int) {
let measurements = measureAll(fn)
for k in measurements[0].keys {
let vs = measurements.map { $0[k]! }
Expand All @@ -63,6 +63,6 @@ func measureAndPrint(desc: String, fn: () -> Int) -> Void {

public func measure(identifier: String, _ body: () -> Int) {
measureAndPrint(desc: identifier) {
return body()
body()
}
}
106 changes: 52 additions & 54 deletions IntegrationTests/tests_04_performance/test_01_resources/shared.swift
Original file line number Diff line number Diff line change
@@ -1,63 +1,61 @@

struct SampleStructure: Codable {

struct Friend: Codable {
let id: Int
struct Friend: Codable {
let id: Int
let name: String
}

let id: String
let index: Int
let guid: String
let isActive: Bool
let balance: String
let picture: String
let age: Int
let eyeColor: String
let name: String
}

let id: String
let index: Int
let guid: String
let isActive: Bool
let balance: String
let picture: String
let age: Int
let eyeColor: String
let name: String
let gender: String
let company: String
let email: String
let phone: String
let address: String
let about: String
let registered: String
let latitude: Double
let longitude: Double
let tags: [String]
let friends: [Friend]
let greeting: String
let favoriteFruit: String

enum CodingKeys: String, CodingKey {
case id = "_id"
case index
case guid
case isActive
case balance
case picture
case age
case eyeColor
case name
case gender
case company
case email
case phone
case address
case about
case registered
case latitude
case longitude
case tags
case friends
case greeting
case favoriteFruit
}
let gender: String
let company: String
let email: String
let phone: String
let address: String
let about: String
let registered: String
let latitude: Double
let longitude: Double
let tags: [String]
let friends: [Friend]
let greeting: String
let favoriteFruit: String

enum CodingKeys: String, CodingKey {
case id = "_id"
case index
case guid
case isActive
case balance
case picture
case age
case eyeColor
case name
case gender
case company
case email
case phone
case address
case about
case registered
case latitude
case longitude
case tags
case friends
case greeting
case favoriteFruit
}
}

extension SampleStructure {

static let sampleJSON = """
static let sampleJSON = """
[
{
"_id": "5e24ed2749789afeebcaa1ca",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import PureSwiftJSONParsing

func run(identifier: String) {
let sampleString = SampleStructure.sampleJSON
let sampleBytes = [UInt8](sampleString.utf8)
let sampleJSON = try! JSONParser().parse(bytes: sampleBytes)
let sampleString = SampleStructure.sampleJSON
let sampleBytes = [UInt8](sampleString.utf8)
let sampleJSON = try! JSONParser().parse(bytes: sampleBytes)

var bytes = [UInt8]() // <-- this is the only real allocation
bytes.reserveCapacity(6000) // <-- reserve capactity so high we don't need reallocs

measure(identifier: identifier) {
for _ in 0..<1_000 {
bytes.removeAll(keepingCapacity: true)
sampleJSON.appendBytes(to: &bytes)
var bytes = [UInt8]() // <-- this is the only real allocation
bytes.reserveCapacity(6000) // <-- reserve capactity so high we don't need reallocs

measure(identifier: identifier) {
for _ in 0 ..< 1000 {
bytes.removeAll(keepingCapacity: true)
sampleJSON.appendBytes(to: &bytes)
}

return bytes.count
}

return bytes.count
}
}
65 changes: 35 additions & 30 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,39 @@
import PackageDescription

var package = Package(
name: "pure-swift-json",
products: [
.library(
name: "PureSwiftJSONCoding",
targets: ["PureSwiftJSONCoding"]),
.library(
name: "PureSwiftJSONParsing",
targets: ["PureSwiftJSONParsing"]),
],
dependencies: [

],
targets: [
.target(
name: "PureSwiftJSONCoding",
dependencies: ["PureSwiftJSONParsing"]),
.target(
name: "PureSwiftJSONParsing",
dependencies: []),
.testTarget(
name: "JSONCodingTests",
dependencies: ["PureSwiftJSONCoding"]),
.testTarget(
name: "JSONParsingTests",
dependencies: ["PureSwiftJSONParsing"]),
.testTarget(
name: "LearningTests",
dependencies: ["PureSwiftJSONParsing"]),
]
name: "pure-swift-json",
products: [
.library(
name: "PureSwiftJSONCoding",
targets: ["PureSwiftJSONCoding"]
),
.library(
name: "PureSwiftJSONParsing",
targets: ["PureSwiftJSONParsing"]
),
],
dependencies: [
],
targets: [
.target(
name: "PureSwiftJSONCoding",
dependencies: ["PureSwiftJSONParsing"]
),
.target(
name: "PureSwiftJSONParsing",
dependencies: []
),
.testTarget(
name: "JSONCodingTests",
dependencies: ["PureSwiftJSONCoding"]
),
.testTarget(
name: "JSONParsingTests",
dependencies: ["PureSwiftJSONParsing"]
),
.testTarget(
name: "LearningTests",
dependencies: ["PureSwiftJSONParsing"]
),
]
)

32 changes: 16 additions & 16 deletions PerfTests/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
import PackageDescription

var package = Package(
name: "pure-swift-json-performance",
products: [

],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "2.13.0"),
.package(url: "https://github.com/autimatisering/IkigaJSON.git", from: "2.0.0"),
.package(path: "..")
],
targets: [
.target(
name: "CodingPerfTests",
dependencies: ["PureSwiftJSONCoding", "PureSwiftJSONParsing", "NIO", "NIOFoundationCompat", "IkigaJSON"]),
]
name: "pure-swift-json-performance",
products: [
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "2.13.0"),
.package(url: "https://github.com/autimatisering/IkigaJSON.git", from: "2.0.0"),
.package(path: ".."),
],
targets: [
.target(
name: "CodingPerfTests",
dependencies: ["PureSwiftJSONCoding", "PureSwiftJSONParsing", "NIO", "NIOFoundationCompat", "IkigaJSON"]
),
]
)

#if os(macOS)
package.dependencies.append(.package(url: "https://github.com/SwiftyJSON/SwiftyJSON.git", from: "5.0.0"))
package.targets.last?.dependencies.append("SwiftyJSON")
package.dependencies.append(.package(url: "https://github.com/SwiftyJSON/SwiftyJSON.git", from: "5.0.0"))
package.targets.last?.dependencies.append("SwiftyJSON")
#endif
Loading