di icon indicating copy to clipboard operation
di copied to clipboard

Convert constants to values at compile time

Open h4kuna opened this issue 5 years ago • 7 comments

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?

h4kuna avatar Nov 08 '19 10:11 h4kuna

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

dg avatar Nov 08 '19 12:11 dg

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?

h4kuna avatar Nov 08 '19 12:11 h4kuna

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

dg avatar Nov 08 '19 12:11 dg

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.

mabar avatar Nov 08 '19 12:11 mabar

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 avatar Nov 08 '19 12:11 h4kuna

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 avatar Nov 08 '19 12:11 h4kuna

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.

h4kuna avatar Nov 08 '19 14:11 h4kuna