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

Convert constants to values at compile time #222

Open
h4kuna opened this issue Nov 8, 2019 · 7 comments
Open

Convert constants to values at compile time #222

h4kuna opened this issue Nov 8, 2019 · 7 comments

Comments

@h4kuna
Copy link
Contributor

h4kuna commented Nov 8, 2019

In this moment if you write constant to neon like this

myExtension: 
	languageId: ::constant(MyLanguageEnum::CZECH)

class

class MyLanguageEnum
{

	public const CZECH = 1;
}

In compile time I get Statement object

arguments => 
  0 => "MyLanguageEnum::CZECH"
entity private => "::constant"

In my use case I need value of constant, here is not problem do it.

I convert constant to value in compile time like this:

class NeonConstantToValue
{
	use StaticClass;

	public static function run(Statement $statement)
	{
		if ($statement->getEntity() !== '::constant') {
			return $statement;
		}

		return constant(reset($statement->arguments));
	}

}

If I thinking about it, the constants can be convertet to value by nette in compile time everytime.

If i made benchmark of this solution that is faster, because it has raw value instead of call function constant with parameter string like this 'languageId' => constant('MyLanguageEnum::CZECH'). The goal is 'languageId' => 1.

What do you think?

@dg
Copy link
Member

dg commented Nov 8, 2019

It does not seem to me that it is measurably faster.

@h4kuna
Copy link
Contributor Author

h4kuna commented Nov 8, 2019

It is secondary litle reason, i try 10k cycle where is cca 4x faster.

0.0012 -> function constant
0.00026 -> raw value

First idea was: Is posible in compile time use raw value insteand of Statement for constant?

@dg
Copy link
Member

dg commented Nov 8, 2019

It can be done, but it's a BC break. Because someone can rely on the current way.

@mabar
Copy link
Contributor

mabar commented Nov 8, 2019

It's probably slower because function call is used. So what if we instead of constant('MyLanguageEnum::CZECH') use directly MyLanguageEnum::CZECH? Uses actual value of constant and reference to source is not lost.

@h4kuna
Copy link
Contributor Author

h4kuna commented Nov 8, 2019

I need value of constant in compile time. This MyLanguageEnum::CZECH is not solution for my use case, still i must keep external method what convert constant to value.

@h4kuna
Copy link
Contributor Author

h4kuna commented Nov 8, 2019

What about keep Statement and add new property with value? Is not ideal, because is only for ::constant.

What about new class StatementConstant extend Statement and ovewrite behavior? This couldn't be BC break?

@h4kuna
Copy link
Contributor Author

h4kuna commented Nov 8, 2019

One point

If you use direct constant like describe @mabar, that is safer if value changed. Because if set raw value and change constant. In this moment nette does not rebuild container.

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

3 participants