From 6f3dba17f69550790c0bbfb5291c77901d3a987b Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Sat, 4 Jan 2020 21:12:38 +0300 Subject: [PATCH] perf(core): Don't waste time instantiating unused intermediaries --- core/length.lua | 9 ++++----- core/measurement.lua | 8 ++++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/core/length.lua b/core/length.lua index d3da18ae3..1032e8a1d 100644 --- a/core/length.lua +++ b/core/length.lua @@ -87,12 +87,11 @@ return pl.class({ end, __add = function (self, other) - local result = SILE.length(self) + if type(self) == "number" then self, other = other, self end other = SU.cast("length", other) - result.length = result.length + other.length - result.stretch = result.stretch + other.stretch - result.shrink = result.shrink + other.shrink - return result + return SILE.length(self.length + other.length, + self.stretch + other.stretch, + self.shrink + other.shrink) end, __sub = function (self, other) diff --git a/core/measurement.lua b/core/measurement.lua index acf998114..21ff7e2c3 100644 --- a/core/measurement.lua +++ b/core/measurement.lua @@ -58,9 +58,7 @@ local measurement = pl.class({ end, absolute = function (self) - local def = SILE.units[self.unit] - local amount = def.converter and def.converter(self.amount) or self.amount * def.value - return SILE.measurement(amount) + return SILE.measurement(self:tonumber()) end, tostring = function (self) @@ -68,7 +66,9 @@ local measurement = pl.class({ end, tonumber = function (self) - return self:absolute().amount + local def = SILE.units[self.unit] + local amount = def.converter and def.converter(self.amount) or self.amount * def.value + return amount end, __tostring = function (self)