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

[cpp] functions get wrapped in Dynamic #6074

Open
nadako opened this issue Mar 4, 2017 · 2 comments
Open

[cpp] functions get wrapped in Dynamic #6074

nadako opened this issue Mar 4, 2017 · 2 comments
Labels
platform-cpp Everything related to CPP / C++
Milestone

Comments

@nadako
Copy link
Member

nadako commented Mar 4, 2017

One of the intermediate issues that lead to the discovery of HaxeFoundation/hxcpp#575 is the following:

Source:

class C {
    static var _f:cpp.Function<Int->Void,cpp.abi.StdCall>;

    static function f(n) {
        _f.call(n + 1);
    }
}

Generated C++

void C_obj::f(int n){
            	HX_STACKFRAME(&_hx_pos_3831bbadf076dfac_5_f)
HXLINE(   5)		 ::Dynamic _hx_tmp = ::C_obj::_f;
HXDLIN(   5)		_hx_tmp((n + (int)1));
            	}

The function is wrapped in hxcpp's Dynamic which I believe quite costly performance-wise because of all the wrapping and even array allocation for arguments, so this is something we might care about.

The cause is probably that the cpp.Function ends up being typed as a simple function in the end, as we can see in the AST. I'm not sure why though.

AST:

static function f[Function:n : Int -> Void]
	[Arg:Int] [Local n(176):Int]
	[Block:Void]
		[Var _hx_tmp(194):Int -> Void]
			[Cast:cpp.FunctionData<Int -> Void, cpp.abi.StdCall>]
				[Cast:cpp.FunctionData<Int -> Void, cpp.abi.StdCall>]
					[Field:cpp.Function<Int -> Void, cpp.abi.StdCall>]
						[TypeExpr C:Class<C>]
						[FStatic:cpp.Function<Int -> Void, cpp.abi.StdCall>]
							C
							_f
		[Call:Void]
			[Local _hx_tmp(194):Int -> Void]
			[Binop:Int]
				[Local n(176):Int]
				+
				[Const:Int] 1
@nadako nadako added the platform-cpp Everything related to CPP / C++ label Mar 4, 2017
@nadako
Copy link
Member Author

nadako commented Mar 4, 2017

Hmm, right after submitting this issue, I've found the // Legacy Api comment on the cpp.Function.call property. And indeed if we remove the .call from the original example and call _f directly, we'll get much better output:

void C_obj::f(int n){
            	HX_STACKFRAME(&_hx_pos_3831bbadf076dfac_5_f)
HXLINE(   5)		 ::Dynamic _hx_tmp = hx::ClassOf< ::C >();
HXDLIN(   5)		::C_obj::_f((n + (int)1));
            	}

This is something @Gama11 and/or @jgranick might be interested in.

I'll leave this issue open for now because chances are @hughsando can do something to make .call generate the same code somehow :)

@jgranick
Copy link

jgranick commented Mar 5, 2017

@nadako We may be able to use conditional compilation on this part of the code. At what version can we call the function directly, without the .call method? Does it require 3.4.0, does it work on 3.2.1...?

@Simn Simn modified the milestone: 4.0 Mar 20, 2017
@Simn Simn modified the milestones: Release 4.0, Backlog Apr 17, 2018
@Simn Simn modified the milestones: Backlog, Later Mar 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform-cpp Everything related to CPP / C++
Projects
None yet
Development

No branches or pull requests

3 participants