Skip to content

Commit b62ad88

Browse files
committed
Added memory test
1 parent 87a2ed0 commit b62ad88

File tree

7 files changed

+78
-33
lines changed

7 files changed

+78
-33
lines changed

CHANGELOG.MD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
# CHANGELOG
2+
## 3.0.1 - 2021-06-15
3+
### Added
4+
- Memory usage test
5+
### Changed
6+
- Object instantiation. Now object prototypes are saved in `__meta.__proto` entry which reduces the amount of memory usage
27
## 3.0.0 - 2021-06-13
38
### Added
49
- Namespace support

README.MD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,4 @@ There is also special Class API. This package defines own class `Class` which ca
286286

287287
## Tests
288288
To run tests, call from terminal `.\test.bat` for Windows and `./test.sh` for Linux
289+
You can pass an argument to tests to define memory test iterations count like this - `test 1024`

TODO.MD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
- [x] Add ability to call methods from inside constructor
1313
- [x] Add operator overloading methods
1414
- [ ] Add `__newindex`, `__gc`, `__mode`, `__ipairs` overloading
15-
- [ ] Important - save instance prototype to parent.__meta.__proto - it'll decrease the number of tables in memory
15+
- [x] Important - save instance prototype to parent.__meta.__proto - it'll decrease the number of tables in memory
1616
- [ ] Merge `Object():clone()` with `table_clone()` (to remove duplicate code)

src/class.lua

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -206,36 +206,38 @@ local function type_descriptor_handler(descriptor)
206206
setmetatable(descriptor, {
207207
__index = __meta.lastType;
208208
__call = function (self, ...)
209-
local parent = self
210-
local object = setmetatable({}, {
211-
__index = parent,
212-
__newindex = parent["[]"] or parent.__newindex,
213-
__call = parent["()"] or parent.__call,
214-
__tostring = parent.__tostring,
215-
__concat = parent[".."] or parent.__concat,
216-
__metatable = parent.__metatable,
217-
__mode = parent.__mode,
218-
__gc = parent.__gc,
219-
__len = parent["#"] or parent.__len,
220-
__pairs = parent.__pairs,
221-
__ipairs = parent.__ipairs,
222-
__add = parent["+"] or parent.__add,
223-
__sub = parent["-"] or parent.__sub,
224-
__mul = parent["*"] or parent.__mul,
225-
__div = parent["/"] or parent.__div,
226-
__pow = parent["^"] or parent.__pow,
227-
__mod = parent["%"] or parent.__mod,
228-
__idiv = parent["//"] or parent.__idiv,
229-
__eq = parent["=="] or parent.__eq,
230-
__lt = parent["<"] or parent.__lt,
231-
__le = parent["<="] or parent.__le,
232-
__band = parent["&"] or parent.__band,
233-
__bor = parent["|"] or parent.__bor,
234-
__bxor = parent["~"] or parent.__bxor,
235-
__bnot = parent["not"] or parent.__bnot,
236-
__shl = parent["<<"] or parent.__shl,
237-
__shr = parent[">>"] or parent.__shr
238-
})
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)
239241
if descriptor.constructor then
240242
descriptor.constructor(object, table.unpack({...}))
241243
end

test.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ if errorlevel 1 (
44
echo Lua interpreter is not installed
55
exit /b
66
)
7-
cls & lua test\class.lua --verbose
7+
cls & lua test\class.lua --verbose & lua test\memory.lua %1

test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ then
33
echo "Lua is not installed"
44
exit
55
fi
6-
clear & lua test/class.lua --verbose
6+
clear & lua test/class.lua --verbose & lua test/memory.lua $1

test/memory.lua

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
dofile 'src/class.lua'
2+
local initialSize = collectgarbage("count")
3+
local cache = {}
4+
class 'TestingMemoryUsage' {
5+
__newindex = function () end;
6+
__call = function () end;
7+
__tostring = function () end;
8+
__concat = function () end;
9+
__metatable = function () end;
10+
__mode = function () end;
11+
__gc = function () end;
12+
__len = function () end;
13+
__pairs = function () end;
14+
__ipairs = function () end;
15+
__add = function () end;
16+
__sub = function () end;
17+
__mul = function () end;
18+
__div = function () end;
19+
__pow = function () end;
20+
__mod = function () end;
21+
__idiv = function () end;
22+
__eq = function () end;
23+
__lt = function () end;
24+
__le = function () end;
25+
__band = function () end;
26+
__bor = function () end;
27+
__bxor = function () end;
28+
__bnot = function () end;
29+
__shl = function () end;
30+
__shr = function () end;
31+
}
32+
local iterationsCount = tonumber(arg[1]) or 1000
33+
for i = 0, iterationsCount do
34+
table.insert(cache, TestingMemoryUsage())
35+
end
36+
print("Memory test iterations count: "..iterationsCount)
37+
print("Memory usage: "..(collectgarbage("count") - initialSize).."KB")

0 commit comments

Comments
 (0)