Repository: google/flatbuffers
File: swift/Sources/FlatBuffers/ByteBuffer.swift (Swift)

Code before:
  }

  /// Adds an object of type NativeStruct into the buffer
  /// - Parameters:
  ///   - value: Object  that will be written to the buffer
  ///   - size: size to subtract from the WriterIndex
  @usableFromInline
  @inline(__always)
  mutating func push<T: NativeStruct>(struct value: T, size: Int) {
    ensureSpace(size: size)
    var v = value
    withUnsafeBytes(of: &v) {
      memcpy(_storage.memory.advanced(by: writerIndex &- size), $0.baseAddress!, size)
      self._writerSize = self._writerSize &+ size
    }
  }

  /// Adds an object of type Scalar into the buffer
  /// - Parameters:
  ///   - value: Object  that will be written to the buffer
  ///   - len: Offset to subtract from the WriterIndex
  @inline(__always)
  @usableFromInline
  mutating func push<T: Scalar>(value: T, len: Int) {
    ensureSpace(size: len)
    var v = value
    withUnsafeBytes(of: &v) {
      memcpy(_storage.memory.advanced(by: writerIndex &- len), $0.baseAddress!, len)
      self._writerSize = self._writerSize &+ len
    }
  }

  /// Adds a string to the buffer using swift.utf8 object
  /// - Parameter str: String that will be added to the buffer
  /// - Parameter len: length of the string
  @inline(__always)
  @usableFromInline
  mutating func push(string str: String, len: Int) {
    ensureSpace(size: len)
    if str.utf8
      .withContiguousStorageIfAvailable({ self.push(bytes: $0, len: len) }) !=
      nil
    {
    } else {
      let utf8View = str.utf8
      for c in utf8View.reversed() {
        push(value: c, len: 1)
      }
    }
  }


Review comment (suggestion):
The following works faster than all the new methods that were tested, which simply returns an `UnsafeRawBufferPointer` which then allows us to use the base address to simply copy the data using memcpy. Fast and effective.

Code after:
  @inline(__always)
  @usableFromInline
  mutating func push<T: Scalar>(elements: [T]) {
    let size = elements.count &* MemoryLayout<T>.size
    ensureSpace(size: size)
    elements.reversed().forEach { s in
      push(value: s, len: MemoryLayout.size(ofValue: s))
    }
  }

  /// Adds an object of type NativeStruct into the buffer
  /// - Parameters:
  ///   - value: Object  that will be written to the buffer
  ///   - size: size to subtract from the WriterIndex
  @usableFromInline
  @inline(__always)
  mutating func push<T: NativeStruct>(struct value: T, size: Int) {
    ensureSpace(size: size)
    var v = value
    withUnsafeBytes(of: &v) {
      memcpy(
        _storage.memory.advanced(by: writerIndex &- size),
        $0.baseAddress!,
        size)
      self._writerSize = self._writerSize &+ size
    }
  }

  /// Adds an object of type Scalar into the buffer
  /// - Parameters:
  ///   - value: Object  that will be written to the buffer
  ///   - len: Offset to subtract from the WriterIndex
  @inline(__always)
  @usableFromInline
  mutating func push<T: Scalar>(value: T, len: Int) {
    ensureSpace(size: len)
    var v = value
    withUnsafeBytes(of: &v) {
      memcpy(
        _storage.memory.advanced(by: writerIndex &- len),
        $0.baseAddress!,
        len)
      self._writerSize = self._writerSize &+ len
    }
  }

  /// Adds a string to the buffer using swift.utf8 object
  /// - Parameter str: String that will be added to the buffer
  /// - Parameter len: length of the string
  @inline(__always)
  @usableFromInline