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

Feature: inline & constexpr functions #7

Open
MinekPo1 opened this issue Aug 17, 2023 · 1 comment
Open

Feature: inline & constexpr functions #7

MinekPo1 opened this issue Aug 17, 2023 · 1 comment

Comments

@MinekPo1
Copy link

Some functions, especially ones computing a simple operation, are less efficient to call, rather than copy paste the underlying code.

As an example:

function test(num a) -> num {
	return (a * 4) + (a * 4) - (a / 2)
}

To call this function, we need 3+3 instructions, (copy a, set return destination, jump to function code, jump back to return destination, copy result), while the function result is computed in five instructions. If this function is used only once, it is not only faster, and more efficient for output length, to simply insert the content inline.

While modern compilers can detect which functions can benefit from being inline automatically, an easier solution is to let the developer mark a function as inline with syntax, for example as such:

inline function test(num a) -> num {
	return (a * 4) + (a * 4) - (a / 2)
}

Some languages also allow the user to mark a function as "constexpr", to tell the compiler that it can precalculate the result, if the input is known in advance.

These optimizations are especially beneficial where copying is expensive, so it will help if you ever decide to add structs or classes.

@MinekPo1
Copy link
Author

MinekPo1 commented Aug 22, 2023

Implemented this, will make a pr once I write tests, examples and stuff.

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