Skip to content

Commit

Permalink
Merge pull request #12 from unstoppablefi/faster-json
Browse files Browse the repository at this point in the history
~20% faster json decoding
  • Loading branch information
terhechte authored Jan 16, 2023
2 parents 9ceaf05 + 4cacc28 commit 353127c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
6 changes: 5 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ let package = Package(
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0"),
.package(url: "https://github.com/jpsim/Yams.git", from: "4.0.4"),
.package(url: "https://github.com/unstoppablefi/GzipSwift", .upToNextMajor(from: "5.0.0")),
.package(url: "https://github.com/michaeleisel/ZippyJSON", .upToNextMajor(from: "1.0.0")),
],
targets: [
/* SwiftGraphQL */
.target(
name: "SwiftGraphQL",
dependencies: [.product(name: "Gzip", package: "GzipSwift")],
dependencies: [
.product(name: "Gzip", package: "GzipSwift"),
.product(name: "ZippyJSON", package: "ZippyJSON")
],
path: "Sources/SwiftGraphQL"
),
.target(
Expand Down
3 changes: 2 additions & 1 deletion Sources/SwiftGraphQL/AnyCodable.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import ZippyJSON


// Based on: https://github.com/yonaskolb/Codability/blob/master/Sources/Codability/AnyCodable.swift
Expand Down Expand Up @@ -123,7 +124,7 @@ extension AnyCodable: Equatable {
/// Calling this allows you to compare AnyCodables that were initialised with a custom Encodable type.
mutating func recode() throws {
let encoded = try JSONEncoder().encode(self)
self = try JSONDecoder().decode(AnyCodable.self, from: encoded)
self = try ZippyJSONDecoder().decode(AnyCodable.self, from: encoded)
}
}

Expand Down
5 changes: 3 additions & 2 deletions Sources/SwiftGraphQL/HTTP+WebSockets.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation
import Network
import ZippyJSON

import os.log

Expand Down Expand Up @@ -47,7 +48,7 @@ public class GraphQLSocket<S: GraphQLEnabledSocket> {
private var queue: [(GraphQLSocket) -> Void] = []
private var subscriptions: [String: (GraphQLSocketMessage) -> Void] = [:]

private var decoder = JSONDecoder()
private var decoder = ZippyJSONDecoder()
private var encoder = JSONEncoder()

// Every successful ping should be matched by a successful pong
Expand Down Expand Up @@ -101,7 +102,7 @@ public class GraphQLSocket<S: GraphQLEnabledSocket> {
// log: OSLog.subscription,
// type: .debug, (String(data: data, encoding: .utf8) ?? "Invalid .utf8")
// )
guard var message = try? JSONDecoder().decode(Message.self, from: data) else {
guard var message = try? ZippyJSONDecoder().decode(Message.self, from: data) else {
os_log("Invalid JSON Payload", log: OSLog.subscription, type: .debug)
return false
}
Expand Down
6 changes: 4 additions & 2 deletions Sources/SwiftGraphQL/Result.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Foundation
import ZippyJSON
//import zippyjson

// MARK: - GraphQL Result

Expand All @@ -11,12 +13,12 @@ public struct GraphQLResult<Type, TypeLock> {
extension GraphQLResult: Equatable where Type: Equatable, TypeLock: Decodable {}

extension GraphQLResult where TypeLock: Decodable {
init(_ response: Data, with selection: Selection<Type, TypeLock?>) throws {
public init(_ response: Data, with selection: Selection<Type, TypeLock?>) throws {
// Decodes the data using provided selection.
var errors: [GraphQLError]? = nil
var extensions: [String: AnyCodable]? = nil
do {
let decoder = JSONDecoder()
let decoder = ZippyJSONDecoder()
let response = try decoder.decode(GraphQLResponse.self, from: response)
errors = response.errors
extensions = response.extensions
Expand Down

0 comments on commit 353127c

Please sign in to comment.