Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MaxLua.mod] Avoid object wrapping for exposed/returned objects #238

Open
GWRon opened this issue Jul 18, 2022 · 1 comment
Open

[MaxLua.mod] Avoid object wrapping for exposed/returned objects #238

GWRon opened this issue Jul 18, 2022 · 1 comment

Comments

@GWRon
Copy link
Contributor

GWRon commented Jul 18, 2022

When passing an object to Lua via MaxLua, then for each object passed, an wrapper object is generated - and GC_FREE'd at the end.

Returning an array of objects, will create a wrapper for each entry in the array - instead of passing the array itself.

Means if you want to iterate over a list (or array) of 1000 blitzmax objects from inside Lua, this will create 1000 new wrapper objects each time you do such an iteration over this list.

Question: is there a way to get rid of that wrapper object generation? Especially when passing around "read only" stuff (iterating over an array or list - without adding/removing the elements in it) ?
Or ... simply when returning objects as a method/function call result (... exception here might be if you return a new TMyType which needs to be retained then of course ... or not?).

Example to replicate the problem:
Open brl.mod/maxlua.mod/lua_object.c:

#include <brl.mod/blitz.mod/blitz.h>

#include <pub.mod/lua.mod/lua-5.1.4/src/lua.h>
int gccall = 0; // ADD THIS LINE

...
...

// MODIFY THIS FUNCTION
int lua_gcobject( lua_State *L ){
	void *p;
	p=lua_touserdata( L,1 );
	struct BBObjectContainer * uc = *(struct BBObjectContainer**)p;
	GC_FREE(uc);

        // ADD THIS
	gccall += 1;
	printf("LUA: gccall %i\n", gccall);
	fflush(stdout);

	return 0;
}
SuperStrict
Framework Brl.StandardIO
Import Brl.MaxLua

Type TTest
    Method GetName:String()
        Return "test"
    End Method
End Type

Type TLuaFunctions
	Field t:TTest = new TTest

	Method GetTest:TTest()
		Return t
	End Method
End Type


Local functions:TLuaFunctions = new TLuaFunctions
LuaRegisterObject(functions,"functions")

Local source:String=..
"function test()~n"+..
"  for i=0,1000 do~n"+..
"    functions.GetTest()~n"+..
"  end~n"+..
"end~n"

Local class:TLuaClass = TLuaClass.Create(source)
Local instance:TLuaObject = TLuaObject.Create(class, Null)
For local i:int = 0 until 100
	instance.Invoke("test", Null)
	delay(10)
Next
print "Done."

This will show you a count of about 100.000 like this: LUA: gccall 99994.

@GWRon
Copy link
Contributor Author

GWRon commented Jul 19, 2022

Can't maxlua avoid creating that "GC-protected" boxcontainer and kinda BBRETAIN(obj) and in lua_gcobject then BBRelease(obj) ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant