Skip to content

Commit

Permalink
[ios] Using UnsafeBufferPointer to skip data copying and boost perf…
Browse files Browse the repository at this point in the history
…ormance. (#2686)

* [ios] Using UnsafeBufferPointer to skip data copying and boost performance.

* [ios] Fix the extra data copying caused by using  and move the guard let condition to be earlier
  • Loading branch information
kikiloveswift authored Nov 20, 2024
1 parent 227adda commit b2061f1
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 143 deletions.
42 changes: 31 additions & 11 deletions spine-cpp/spine-cpp-lite/spine-cpp-lite-codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,6 @@ def write(self):

if swift_return_type_is_array:
body += self.write_array_call(num_function_name, function_call)
body += inset + inset
body += "}"
else:
if not self.spine_function.return_type == "void":
body += "return "
Expand Down Expand Up @@ -305,24 +303,46 @@ def write_c_function_call(self, spine_params):
function_call += " != 0"

return function_call
def write_array_call(self, num_function_name, function_call):
array_call = f"let num = Int({num_function_name}({self.spine_object.var_name}))"

def write_array_spine_class(self, num_function_name, function_call):
array_call = f"let ptr = {function_call}"
array_call += "\n"
array_call += inset + inset
array_call += f"let ptr = {function_call}"
array_call += "guard let validPtr = ptr else { return [] }"
array_call += "\n"
array_call += inset + inset
array_call += "return (0..<num).compactMap {"
array_call += f"let num = Int({num_function_name}({self.spine_object.var_name}))"
array_call += "\n"
array_call += inset + inset
array_call += "let buffer = UnsafeBufferPointer(start: validPtr, count: num)"
array_call += "\n"
array_call += inset + inset
array_call += "return buffer.compactMap {"
array_call += "\n"
array_call += inset + inset + inset

array_call += "$0.flatMap { .init($0) }"
array_call += "\n"
array_call += inset + inset
array_call += "}"
return array_call

def write_array_call(self, num_function_name, function_call):
if self.spine_function.isReturningSpineClass():
array_call += "ptr?[$0].flatMap { .init($0) }"
else:
array_call += "ptr?[$0]"
return self.write_array_spine_class(num_function_name, function_call)
array_call = f"let ptr = {function_call}"
array_call += "\n"
array_call += inset + inset
array_call += "guard let validPtr = ptr else { return [] }"
array_call += "\n"
array_call += inset + inset
array_call += f"let num = Int({num_function_name}({self.spine_object.var_name}))"
array_call += "\n"
array_call += inset + inset

array_call += "let buffer = UnsafeBufferPointer(start: validPtr, count: num)"
array_call += "\n"
array_call += inset + inset
array_call += "return Array(buffer)"
return array_call

def write_dispose_call(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extension RenderCommand {
let positions = positions(numVertices: numVertices)
let uvs = uvs(numVertices: numVertices)
let colors = colors(numVertices: numVertices)

vertices.reserveCapacity(indices.count)
for i in 0..<indices.count {
let index = Int(indices[i])

Expand Down
12 changes: 9 additions & 3 deletions spine-ios/Sources/Spine/Spine.Generated+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,19 +215,25 @@ internal extension RenderCommand {
func positions(numVertices: Int) -> [Float] {
let num = numVertices * 2
let ptr = spine_render_command_get_positions(wrappee)
return (0..<num).compactMap { ptr?[$0] }
guard let validPtr = ptr else { return [] }
let buffer = UnsafeBufferPointer(start: validPtr, count: num)
return Array(buffer)
}

func uvs(numVertices: Int) -> [Float] {
let num = numVertices * 2
let ptr = spine_render_command_get_uvs(wrappee)
return (0..<num).compactMap { ptr?[$0] }
guard let validPtr = ptr else { return [] }
let buffer = UnsafeBufferPointer(start: validPtr, count: num)
return Array(buffer)
}

func colors(numVertices: Int) ->[Int32] {
let num = numVertices
let ptr = spine_render_command_get_colors(wrappee)
return (0..<num).compactMap { ptr?[$0] }
guard let validPtr = ptr else { return [] }
let buffer = UnsafeBufferPointer(start: validPtr, count: num)
return Array(buffer)
}
}

Expand Down
Loading

0 comments on commit b2061f1

Please sign in to comment.