Skip to content

use UnsafeBufferPointer instead of Array #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Sources/Runtime/Metadata/FuntionMetadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ struct FunctionMetadata: MetadataType {

private func argumentInfo() -> (Int, [Any.Type], Any.Type) {
let n = numberArguments()
var argTypes = pointer.pointee.argumentVector.vector(n: n + 1)
let argTypeBuffer = pointer.pointee.argumentVector.vector(n: n + 1)

let resultType = argTypes[0]
argTypes.removeFirst()
let resultType = argTypeBuffer[0]
let argTypes = Array(argTypeBuffer.dropFirst())

return (n, argTypes, resultType)
}
Expand Down
6 changes: 2 additions & 4 deletions Sources/Runtime/Metadata/TupleMetadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ struct TupleMetadata: MetadataType, TypeInfoConvertible {
return labels
}

func elements() -> [TupleElementLayout] {
let n = numberOfElements()
guard n > 0 else { return [] }
return pointer.pointee.elementVector.vector(n: n)
func elements() -> UnsafeBufferPointer<TupleElementLayout> {
return pointer.pointee.elementVector.vector(n: numberOfElements())
}

func properies() -> [PropertyInfo] {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Runtime/Pointers/RelativeVectorPointer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import Foundation

struct RelativeVectorPointer<Offset: FixedWidthInteger, Pointee> {
var offset: Offset
mutating func vector(metadata: UnsafePointer<Int>, n: Int) -> [Pointee] {
mutating func vector(metadata: UnsafePointer<Int>, n: Int) -> UnsafeBufferPointer<Pointee> {
return metadata.advanced(by: numericCast(offset))
.raw.assumingMemoryBound(to: Pointee.self)
.vector(n: n)
.buffer(n: n)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Runtime/Pointers/Vector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ struct Vector<Element> {

var element: Element

mutating func vector(n: Int) -> [Element] {
mutating func vector(n: Int) -> UnsafeBufferPointer<Element> {
return withUnsafePointer(to: &self) {
$0.withMemoryRebound(to: Element.self, capacity: 1) { start in
return start.vector(n: n)
return start.buffer(n: n)
}
}
}
Expand Down
16 changes: 4 additions & 12 deletions Sources/Runtime/Utilities/Pointer+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,8 @@ extension UnsafePointer {
return UnsafeMutablePointer<Pointee>(mutating: self)
}

func vector(n: Int) -> [Pointee] {
var result = [Pointee]()
for i in 0..<n {
result.append(advanced(by: i).pointee)
}
return result
func buffer(n: Int) -> UnsafeBufferPointer<Pointee> {
return UnsafeBufferPointer(start: self, count: n)
}
}

Expand All @@ -57,12 +53,8 @@ extension UnsafeMutablePointer {
return UnsafeMutableRawPointer(self)
}

func vector(n: Int) -> [Pointee] {
var result = [Pointee]()
for i in 0..<n {
result.append(advanced(by: i).pointee)
}
return result
func buffer(n: Int) -> UnsafeMutableBufferPointer<Pointee> {
return UnsafeMutableBufferPointer(start: self, count: n)
}

func advanced(by n: Int, wordSize: Int) -> UnsafeMutableRawPointer {
Expand Down