mirror of
				https://github.com/GayPizzaSpecifications/darwin-apk.git
				synced 2025-11-03 23:49: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 Foundation
 | 
				
			||||||
 | 
					import System
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public struct MemoryInputStream: InputStream {
 | 
					public struct MemoryInputStream: InputStream {
 | 
				
			||||||
  private var _buf: [UInt8]! = nil
 | 
					  private var _buf: ContiguousArray<UInt8>! = nil
 | 
				
			||||||
  private let _sli: ArraySlice<UInt8>
 | 
					  private let _sli: ArraySlice<UInt8>
 | 
				
			||||||
 | 
					  private let _ptr: UnsafeBufferPointer<UInt8>
 | 
				
			||||||
  private let _len: Int
 | 
					  private let _len: Int
 | 
				
			||||||
  private var _idx = 0
 | 
					  private var _idx = 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public init(buffer: Data) {
 | 
					  public init(buffer: Data) {
 | 
				
			||||||
    self._len = buffer.count
 | 
					    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._sli = self._buf[...]
 | 
				
			||||||
 | 
					    self._ptr = self._sli.withUnsafeBufferPointer(\.self)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public init(view: ArraySlice<UInt8>) {
 | 
					  public init(view: ArraySlice<UInt8>) {
 | 
				
			||||||
    self._sli = view
 | 
					 | 
				
			||||||
    self._len = view.count
 | 
					    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) {
 | 
					  public mutating func seek(_ whence: StreamWhence) throws(StreamError) {
 | 
				
			||||||
    let (position, overflow) = switch whence {
 | 
					    let (position, overflow) = switch whence {
 | 
				
			||||||
    case .set(let position):   (position, false)
 | 
					    case .set(let position):   (position, false)
 | 
				
			||||||
@ -47,7 +74,7 @@ public struct MemoryInputStream: InputStream {
 | 
				
			|||||||
  public mutating func read(_ count: Int) throws(StreamError) -> Data {
 | 
					  public mutating func read(_ count: Int) throws(StreamError) -> Data {
 | 
				
			||||||
    let beg = min(self._idx, self._len)
 | 
					    let beg = min(self._idx, self._len)
 | 
				
			||||||
    let end = min(self._idx + count, 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)
 | 
					    self._idx += beg.distance(to: end)
 | 
				
			||||||
    return bytes
 | 
					    return bytes
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@ -58,16 +85,25 @@ public struct MemoryInputStream: InputStream {
 | 
				
			|||||||
    let len = beg.distance(to: end)
 | 
					    let len = beg.distance(to: end)
 | 
				
			||||||
    let buf = UnsafeMutableRawBufferPointer(start: buffer, count: len)
 | 
					    let buf = UnsafeMutableRawBufferPointer(start: buffer, count: len)
 | 
				
			||||||
    self._idx += 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? {
 | 
					  public mutating func next() -> UInt8? {
 | 
				
			||||||
    if self._idx < self._len {
 | 
					    if _fastPath(self._idx < self._len) {
 | 
				
			||||||
      let byte = self._sli[self._idx]
 | 
					      defer { self._idx += 1 }
 | 
				
			||||||
      self._idx += 1
 | 
					      return self._ptr[self._idx]
 | 
				
			||||||
      return byte
 | 
					 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      return nil
 | 
					      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