Skip to content

Follow up question about interfacing with the client from the service #5

@stevebegin

Description

@stevebegin

Thanks so much for your previous answer and example code, it helped me a ton and I now have a good prototype for the service I'm aiming for.

I have run however into a major issue.

Here's the setup. My service needs to send some data to the client really fast. I do this using the sendOnewayMessage method on the XPCConnection created from an XPCEndpoint. The message that is sent is a struct conforming to Codable with a single property of type NSBitmapImageRep.

public struct Frame: Codable {
  public let bitmap: NSBitmapImageRep

  public enum CodingKeys: String, CodingKey {
    case bitmap
  }

  public init(bitmap: NSBitmapImageRep) {
    self.bitmap = bitmap
  }

  public init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    let data = try container.decode(Data.self, forKey: CodingKeys.bitmap)
    guard let bitmap = NSBitmapImageRep(data: data) else {
      throw FrameGraberError.decodingFailed
    }

    self.bitmap = bitmap
  }

  public func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
      let data = bitmap.tiffRepresentation
      guard let data else {
        throw FrameGraberError.encodingFailed
    }

    try container.encode(data, forKey: CodingKeys.bitmap)
  }
}

In a Unit test, I have used a 2000 x 2000 8 bits bitmap (4 MB) to measure the performance of those 2 blocks of code:

This runs with a Time: 0.006 sec

let data = bitmap.tiffRepresentation
let bitmap = NSBitmapImageRep(data: data)

And this runs with a Time: 0.467 sec

let encoded = try! XPCEncoder().encode(frame)
let decoded = try! XPCDecoder().decode(type: Frame.self, from: encoded)

In the end, I'm not sure what my question is, but maybe you could enlighten me on what is going on in both the Decoder and the Encoder and why they are this slow.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions