Skip to content

Commit

Permalink
Switch from mock Vector3 to native vector lib
Browse files Browse the repository at this point in the history
  • Loading branch information
centau committed Nov 13, 2024
1 parent 5abd5ee commit 49cc551
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 95 deletions.
4 changes: 3 additions & 1 deletion src/apply.luau
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@ for name, class in {
UDim = UDim,
UDim2 = UDim2,
Vector2 = Vector2,
Vector3 = Vector3,
Rect = Rect
} :: Map<string, { [string]: any }> do
aggregates[name] = class.new
end

aggregates.Vector3 = vector.create
aggregates.vector = vector.create

-- applies table of nested properties to an instance using full vide semantics
local function apply<T>(instance: T & Instance, properties: { [unknown]: unknown }): T
if not properties then
Expand Down
3 changes: 1 addition & 2 deletions src/defaults.luau
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
local Enum = game and Enum or require "../test/mock".Enum :: never
local Color3 = game and Color3 or require "../test/mock".Color3 :: never
local Vector3 = game and Vector3 or require "../test/mock".Vector3 :: never

return {
Part = {
Material = Enum.Material.SmoothPlastic,
Size = Vector3.new(1, 1, 1),
Size = vector.create(1, 1, 1),
Anchored = true
},

Expand Down
8 changes: 3 additions & 5 deletions src/spring.luau
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
local Vector3 = game and Vector3 or require "../test/mock".Vector3 :: never

--[[
Supported datatypes:
Expand Down Expand Up @@ -36,8 +34,8 @@ local TOLERANCE = 0.0001

type Vec3 = Vector3

local function Vec3(x: number?, y: number?, z: number?)
return Vector3.new(x, y, z)
local function Vec3(x: number?, y: number?, z: number?): Vec3
return vector.create(x, y, z)
end

local ZERO = Vec3(0, 0, 0)
Expand Down Expand Up @@ -276,7 +274,7 @@ local function update_spring_sources()
x0_456 - x1_456

-- todo: can this false positive?
if (v_123 + v_456 + dx_123 + dx_456).Magnitude < TOLERANCE then
if vector.magnitude(v_123 + v_456 + dx_123 + dx_456) < TOLERANCE then
-- close enough to target, unshedule spring and set value to target
table.insert(remove_queue, data)
output.cache = data.source_value
Expand Down
97 changes: 45 additions & 52 deletions test/benchmark.luau
Original file line number Diff line number Diff line change
Expand Up @@ -504,85 +504,78 @@ ROOT_BENCH(`get context (depth={depth})`, function()
end)
end)

N *= 1024
TITLE "spring()"

ROOT_BENCH("spring update", function()
local root, source, spring = vide.root, vide.source, vide.spring

local src = source(0)

root(function()
for i = 1, N do
spring(src)
end

START(N)

src(1)

return nil
end)
end)

ROOT_BENCH("spring step", function()
local root, source, spring = vide.root, vide.source, vide.spring

local src = source(0)

root(function()
for i = 1, N do
spring(src)
end

src(1)

START(N)

vide.step(1/60)

return nil
end)
end)

TITLE "aggregate"

do
-- the purpose of the two following benchmarks is to measure the overhead of
-- aggregate construction
ROOT_BENCH("set explicit mock vector2", function()
ROOT_BENCH("set explicit vector", function()
local apply = require "../src/apply"
local Vector2 = require "../test/mock".Vector2

local label = create "TextLabel" {
AnchorPoint = Vector2.new(1, 1)
AnchorPoint = vector.create(1, 1, 1)
}

for i = 1, START(N) do
apply(label, {
AnchorPoint = Vector2.new(i, i)
AnchorPoint = vector.create(i, i, i)
})
end
end)

ROOT_BENCH("set aggregate mock vector2", function()
ROOT_BENCH("set aggregate vector", function()
local apply = require "../src/apply"
local Vector2 = require "../test/mock".Vector2

local label = create "TextLabel" {
AnchorPoint = Vector2.new(1, 1)
AnchorPoint = vector.create(1, 1, 1)
}

for i = 1, START(N) do
apply(label, {
AnchorPoint = { i, i }
AnchorPoint = { i, i, i }
})
end
end)
end

-- innacurate due to no Vector3 in vanilla Luau
-- mock vector is 200x slower than native vector

-- ROOT_BENCH("spring update", function()
-- local root, source, spring = vide.root, vide.source, vide.spring

-- local src = source(0)

-- root(function()
-- for i = 1, N do
-- spring(src)
-- end

-- START(N)

-- src(1)

-- return nil
-- end)
-- end)

-- N /= 1024

-- ROOT_BENCH("spring step", function()
-- local root, source, spring = vide.root, vide.source, vide.spring

-- local src = source(0)

-- root(function()
-- for i = 1, N do
-- spring(src)
-- end

-- src(1)

-- START(N)

-- vide.step(1/60)

-- return nil
-- end)
-- end)

return nil
35 changes: 0 additions & 35 deletions test/mock.luau
Original file line number Diff line number Diff line change
Expand Up @@ -257,40 +257,6 @@ local Vector2 = { __type = "Vector2" } :: any do
end
end

local Vector3 = { __type = "Vector3" } :: any do
local function new(x, y, z)
return setmetatable({ X = x, Y = y, Z = z }, Vector3)
end

function Vector3.new(x, y, z)
return new(x or 0, y or 0, z or 0)
end

function Vector3.__add(a, b)
return new(a.X + b.X, a.Y + b.Y, a.Z + b.Z)
end

function Vector3.__sub(a, b)
return new(a.X - b.X, a.Y - b.Y, a.Z - b.Z)
end

function Vector3.__mul(a, b)
return new(a.X * b, a.Y * b, a.Z * b)
end

function Vector3.__unm(v)
return new(-v.X, -v.Y, -v.Z)
end

function Vector3.__eq(a, b)
return a.X == b.X and a.Y == b.Y
end

function Vector3.__index(v)
return (v.X^2 + v.Y^2 + v.Z^2)^0.5
end
end

local UDim2 = { __type = "UDim2" } :: any do
function UDim2.new(sx, ox, sy, oy)
return table_to_proxy(setmetatable({ x = { scale = sx, offset = ox }, y = { scale = sy, offset = oy } }, UDim2))
Expand Down Expand Up @@ -330,7 +296,6 @@ return {
Instance = Instance :: typeof(Instance),
Color3 = Color3 :: typeof(Color3),
Vector2 = Vector2 :: typeof(Vector2),
Vector3 = Vector3 :: typeof(Vector3),
UDim2 = UDim2 :: typeof(UDim2),
Enum = Enum :: typeof(Enum),
typeof = typeof :: typeof(typeof)
Expand Down

0 comments on commit 49cc551

Please sign in to comment.