@@ -81,8 +81,9 @@ function META.Equal(
8181end
8282
8383function META :GetHash (visited )--[[ #: string]]
84- if self :GetCardinality () == 1 then
85- return (self .Data [1 ]--[[ # as any]] ):GetHash ()
84+ local data = self .Data
85+ if # data == 1 then
86+ return (data [1 ]--[[ # as any]] ):GetHash ()
8687 end
8788
8889 visited = visited or {}
@@ -91,14 +92,16 @@ function META:GetHash(visited)--[[#: string]]
9192
9293 visited [self ] = " *circular*"
9394 local types = {}
95+ local len = # data
9496
95- for i , v in ipairs ( self . Data ) do
96- types [i ] = v :GetHash (visited )
97+ for i = 1 , len do
98+ types [i ] = data [ i ] :GetHash (visited )
9799 end
98100
99101 table_sort (types )
100- visited [self ] = table.concat (types , " |" )
101- return visited [self ]--[[ # as string]]
102+ local hash = table_concat (types , " |" )
103+ visited [self ] = hash
104+ return hash
102105end
103106
104107local sort = function (a --[[ #: string]] , b --[[ #: string]] )
@@ -110,9 +113,12 @@ function META:__tostring()
110113
111114 local s = {}
112115 self .suppress = true
116+
117+ local data = self .Data
118+ local len = # data
113119
114- for i , v in ipairs ( self . Data ) do
115- s [i ] = tostring (v )
120+ for i = 1 , len do
121+ s [i ] = tostring (data [ i ] )
116122 end
117123
118124 if not s [1 ] then
@@ -122,7 +128,7 @@ function META:__tostring()
122128
123129 self .suppress = false
124130
125- if # s == 1 then return (s [1 ]--[[ # as string]] ) .. " |" end
131+ if len == 1 then return (s [1 ]--[[ # as string]] ) .. " |" end
126132
127133 table_sort (s , sort )
128134 return table_concat (s , " | " )
@@ -155,11 +161,16 @@ local function remove(self--[[#: TUnion]], index--[[#: number]])
155161end
156162
157163local function find_index (self --[[ #: TUnion]] , obj --[[ #: any]] )
158- for i = 1 , # self .Data do
159- local v = self .Data [i ]--[[ # as any]]
164+ local data = self .Data
165+ local len = # data
166+ local obj_type = obj .Type
160167
161- if v :Equal (obj ) then
162- if v .Type ~= " function" or v :GetFunctionBodyNode () == obj :GetFunctionBodyNode () then
168+ for i = 1 , len do
169+ local v = data [i ]--[[ # as any]]
170+
171+ -- Early exit if types don't match
172+ if v .Type == obj_type and v :Equal (obj ) then
173+ if obj_type ~= " function" or v :GetFunctionBodyNode () == obj :GetFunctionBodyNode () then
163174 return i
164175 end
165176 end
0 commit comments