mirror of
				https://github.com/GayPizzaSpecifications/darwin-apk.git
				synced 2025-11-04 07:59:38 +00:00 
			
		
		
		
	Tried a few things to try making memory streams faster, the answer may be in just biting the bullet and making streams classes though
This commit is contained in:
		@ -4,25 +4,52 @@
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
import Foundation
 | 
			
		||||
import System
 | 
			
		||||
 | 
			
		||||
public struct MemoryInputStream: InputStream {
 | 
			
		||||
  private var _buf: [UInt8]! = nil
 | 
			
		||||
  private var _buf: ContiguousArray<UInt8>! = nil
 | 
			
		||||
  private let _sli: ArraySlice<UInt8>
 | 
			
		||||
  private let _ptr: UnsafeBufferPointer<UInt8>
 | 
			
		||||
  private let _len: Int
 | 
			
		||||
  private var _idx = 0
 | 
			
		||||
 | 
			
		||||
  public init(buffer: Data) {
 | 
			
		||||
    self._len = buffer.count
 | 
			
		||||
    self._buf = [UInt8](repeating: 0, count: self._len)
 | 
			
		||||
    self._buf.withUnsafeMutableBytes { _ = buffer.copyBytes(to: $0) }
 | 
			
		||||
    /*
 | 
			
		||||
    self._buf = .buffer(.allocate(capacity: self._len))
 | 
			
		||||
    if case .buffer(let buf) = _buf {
 | 
			
		||||
      _ = buffer.copyBytes(to: buf)
 | 
			
		||||
      self._ptr = .init(start: buf.baseAddress, count: self._len)
 | 
			
		||||
    } else { fatalError() }
 | 
			
		||||
    */
 | 
			
		||||
    self._buf = .init(repeating: 0, count: self._len)
 | 
			
		||||
    self._buf.withUnsafeMutableBufferPointer { buf in
 | 
			
		||||
      _ = buffer.copyBytes(to: buf)
 | 
			
		||||
    }
 | 
			
		||||
    self._sli = self._buf[...]
 | 
			
		||||
    self._ptr = self._sli.withUnsafeBufferPointer(\.self)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public init(view: ArraySlice<UInt8>) {
 | 
			
		||||
    self._sli = view
 | 
			
		||||
    self._len = view.count
 | 
			
		||||
    /*
 | 
			
		||||
    self._buf = .slice(view)
 | 
			
		||||
    if case .slice(let sli) = _buf {
 | 
			
		||||
      self._ptr = sli.withUnsafeBufferPointer(\.self)
 | 
			
		||||
    } else { fatalError() }
 | 
			
		||||
    */
 | 
			
		||||
    self._sli = view
 | 
			
		||||
    self._ptr = self._sli.withUnsafeBufferPointer(\.self)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /*
 | 
			
		||||
  deinit {
 | 
			
		||||
    if case .buffer(let buf) = _buf {
 | 
			
		||||
      buf.deallocate()
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
  public mutating func seek(_ whence: StreamWhence) throws(StreamError) {
 | 
			
		||||
    let (position, overflow) = switch whence {
 | 
			
		||||
    case .set(let position):   (position, false)
 | 
			
		||||
@ -47,7 +74,7 @@ public struct MemoryInputStream: InputStream {
 | 
			
		||||
  public mutating func read(_ count: Int) throws(StreamError) -> Data {
 | 
			
		||||
    let beg = min(self._idx, self._len)
 | 
			
		||||
    let end = min(self._idx + count, self._len)
 | 
			
		||||
    let bytes = Data(self._sli[beg..<end])
 | 
			
		||||
    let bytes = Data(self._ptr[beg..<end])
 | 
			
		||||
    self._idx += beg.distance(to: end)
 | 
			
		||||
    return bytes
 | 
			
		||||
  }
 | 
			
		||||
@ -58,16 +85,25 @@ public struct MemoryInputStream: InputStream {
 | 
			
		||||
    let len = beg.distance(to: end)
 | 
			
		||||
    let buf = UnsafeMutableRawBufferPointer(start: buffer, count: len)
 | 
			
		||||
    self._idx += len
 | 
			
		||||
    return self._sli.copyBytes(to: buf, from: beg..<end)
 | 
			
		||||
    return self._ptr.copyBytes(to: buf, from: beg..<end)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public mutating func next() -> UInt8? {
 | 
			
		||||
    if self._idx < self._len {
 | 
			
		||||
      let byte = self._sli[self._idx]
 | 
			
		||||
      self._idx += 1
 | 
			
		||||
      return byte
 | 
			
		||||
    if _fastPath(self._idx < self._len) {
 | 
			
		||||
      defer { self._idx += 1 }
 | 
			
		||||
      return self._ptr[self._idx]
 | 
			
		||||
    } else {
 | 
			
		||||
      return nil
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
fileprivate extension MemoryInputStream {
 | 
			
		||||
  enum Storage {
 | 
			
		||||
    //case buffer(_: UnsafeMutableBufferPointer<UInt8>)
 | 
			
		||||
    case buffer(_: ContiguousArray<UInt8>)
 | 
			
		||||
    case slice(_: ArraySlice<UInt8>)
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user