Jefferson Gonzalez wrote on 22/09/2015 20:28:
Is also worth noting that if you keep performance in mind, enums whith values represented as integers should be a much more performant/efficient implementation since it would require less cpu cycles and memory to build and read them. In the other side the RFC proposal is treating the enum values as objects which would require a bigger zval and more cpu cycles to construct them and read them.

The internal implementation can never be quite as simple as an IS_LONG zval if we want to provide anything at all beyond a C-style enum. I for one do not want Days::MONDAY === Months::JANUARY to return true, and nor should typeof(Days::MONDAY) return "long"; this requires at the very least a struct with type name (enum "class") and value.

Memory efficiency is only trivially different, since all possible instances exist in memory only once, so each zval pointing at Days::MONDAY would just be a single pointer to the single instance of that structure. Initial creation of the enum instances would be slower, but again only needs to happen once when the enum is declared, so is unlikely to be a major concern.

Every other action I can think of is either the same, slower, or impossible if you use an int-based representation:

- Comparing two enum instances can again be optimised based on the "only one instance" rule: you can directly compare the pointers. - Getting the name of a class constant based on its definition (e.g. to display an Exception's code in human-readable form) requires extremely inefficient use of reflection. An int-based enum implementation would have to do something similar, an object-based one could cache the information in the instance. - Absolutely any other additional behaviour or fields would be either impossible with an int-based representation, or require exactly the same lookups as an object-based implementation would provide anyway.

Looking at Levi's PoC branch, the actual approach taken is a hybrid anyway: the _zend_enum struct directly holds the name and an "ordinal" z_long directly, and only access the object representation if these two pieces of information are not sufficient.

Regards,
--
Rowan Collins
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to