Skip to content

Commit d48e60c

Browse files
committed
Reduced memory usage
1 parent b62ad88 commit d48e60c

File tree

3 files changed

+55
-44
lines changed

3 files changed

+55
-44
lines changed

CHANGELOG.MD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# CHANGELOG
2+
## 3.0.2 - 2021-06-15
3+
### Changed
4+
- Class definitions. Memory usage reduced
25
## 3.0.1 - 2021-06-15
36
### Added
47
- Memory usage test

src/class.lua

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -199,55 +199,59 @@ local function resolve_type_extend_list(entityType, name, extendList)
199199
return parentList
200200
end
201201

202+
local function type_constructor(self, ...)
203+
if not self.__meta.__proto then
204+
self.__meta.__proto = {
205+
__index = self,
206+
__newindex = self["[]"] or self.__newindex,
207+
__call = self["()"] or self.__call,
208+
__tostring = self.__tostring,
209+
__concat = self[".."] or self.__concat,
210+
__metatable = self.__metatable,
211+
__mode = self.__mode,
212+
__gc = self.__gc,
213+
__len = self["#"] or self.__len,
214+
__pairs = self.__pairs,
215+
__ipairs = self.__ipairs,
216+
__add = self["+"] or self.__add,
217+
__sub = self["-"] or self.__sub,
218+
__mul = self["*"] or self.__mul,
219+
__div = self["/"] or self.__div,
220+
__pow = self["^"] or self.__pow,
221+
__mod = self["%"] or self.__mod,
222+
__idiv = self["//"] or self.__idiv,
223+
__eq = self["=="] or self.__eq,
224+
__lt = self["<"] or self.__lt,
225+
__le = self["<="] or self.__le,
226+
__band = self["&"] or self.__band,
227+
__bor = self["|"] or self.__bor,
228+
__bxor = self["~"] or self.__bxor,
229+
__bnot = self["not"] or self.__bnot,
230+
__shl = self["<<"] or self.__shl,
231+
__shr = self[">>"] or self.__shr
232+
}
233+
end
234+
local object = setmetatable({}, self.__meta.__proto)
235+
if self.constructor then
236+
self.constructor(object, table.unpack({...}))
237+
end
238+
object.__meta = {
239+
type = Type.INSTANCE,
240+
class = self
241+
}
242+
return object
243+
end
244+
202245
local function type_descriptor_handler(descriptor)
203246
local meta = __meta.lastType.__meta
247+
__meta.lastTypeDescriptor = descriptor
204248
check_type_field_absence(meta.type, meta.name, descriptor, "__meta")
205249
check_type_field_absence(meta.type, meta.name, descriptor, "__index")
206250
setmetatable(descriptor, {
207251
__index = __meta.lastType;
208-
__call = function (self, ...)
209-
if not self.__meta.__proto then
210-
self.__meta.__proto = {
211-
__index = self,
212-
__newindex = self["[]"] or self.__newindex,
213-
__call = self["()"] or self.__call,
214-
__tostring = self.__tostring,
215-
__concat = self[".."] or self.__concat,
216-
__metatable = self.__metatable,
217-
__mode = self.__mode,
218-
__gc = self.__gc,
219-
__len = self["#"] or self.__len,
220-
__pairs = self.__pairs,
221-
__ipairs = self.__ipairs,
222-
__add = self["+"] or self.__add,
223-
__sub = self["-"] or self.__sub,
224-
__mul = self["*"] or self.__mul,
225-
__div = self["/"] or self.__div,
226-
__pow = self["^"] or self.__pow,
227-
__mod = self["%"] or self.__mod,
228-
__idiv = self["//"] or self.__idiv,
229-
__eq = self["=="] or self.__eq,
230-
__lt = self["<"] or self.__lt,
231-
__le = self["<="] or self.__le,
232-
__band = self["&"] or self.__band,
233-
__bor = self["|"] or self.__bor,
234-
__bxor = self["~"] or self.__bxor,
235-
__bnot = self["not"] or self.__bnot,
236-
__shl = self["<<"] or self.__shl,
237-
__shr = self[">>"] or self.__shr
238-
}
239-
end
240-
local object = setmetatable({}, self.__meta.__proto)
241-
if descriptor.constructor then
242-
descriptor.constructor(object, table.unpack({...}))
243-
end
244-
object.__meta = {
245-
type = Type.INSTANCE,
246-
class = descriptor
247-
}
248-
return object
249-
end
252+
__call = type_constructor
250253
})
254+
__meta.lastTypeDescriptor = nil
251255
for parentName, parent in pairs(__meta.lastType.__meta.parents) do
252256
parent.__meta.children[meta.name] = descriptor
253257
end

test/memory.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,9 @@ local iterationsCount = tonumber(arg[1]) or 1000
3333
for i = 0, iterationsCount do
3434
table.insert(cache, TestingMemoryUsage())
3535
end
36-
print("Memory test iterations count: "..iterationsCount)
37-
print("Memory usage: "..(collectgarbage("count") - initialSize).."KB")
36+
print("Instantiation memory usage for "..iterationsCount.." iterations: "..(collectgarbage("count") - initialSize).."KB")
37+
local initialSize = collectgarbage("count")
38+
for i = 0, iterationsCount do
39+
class ('MemoryTest'..tostring(i)) {}
40+
end
41+
print("Class definition memory usage for "..iterationsCount.." iterations: "..(collectgarbage("count") - initialSize).."KB")

0 commit comments

Comments
 (0)