Skip to content

Inefficient output stream operator implementation #38

Open

Description

@herolover

Now output stream operator includes a set of if statements, which might be inefficient if there are many different enum values. It could be much better if it's replaced by switch statement.

Also it would be very useful to have a separate free function to get const char * of enum. Output stream operator could use the function.

Activity

  1. chronoxor commented on Dec 15, 2020

    @chronoxor
    Owner

    FBE protocol spec allows to declare enums with duplicate values:

    // Byte enum declaration
    enum EnumByte : byte
    {
        ENUM_VALUE_0;
        ENUM_VALUE_1 = 0;
        ENUM_VALUE_2;
        ENUM_VALUE_3 = 254;
        ENUM_VALUE_4;
        ENUM_VALUE_5 = ENUM_VALUE_3;
    }
    

    In this case switch statement will generate:
    error C2196: case value 'enums::EnumByte::ENUM_VALUE_0' already used
    error C2196: case value 'enums::EnumByte::ENUM_VALUE_3' already used

  2. chronoxor commented on Dec 15, 2020

    @chronoxor
    Owner

    Enum value string is rarely used outside struct's dumps in logs. So << operator is more suitable. In case you need string equivalent try:

    std::string to_string(EnumByte value) const { std::stringstream ss; ss << value; return ss.str(); }
    
  3. herolover commented on Dec 15, 2020

    @herolover
    Author

    FBE protocol spec allows to declare enums with duplicate values:

    // Byte enum declaration
    enum EnumByte : byte
    {
        ENUM_VALUE_0;
        ENUM_VALUE_1 = 0;
        ENUM_VALUE_2;
        ENUM_VALUE_3 = 254;
        ENUM_VALUE_4;
        ENUM_VALUE_5 = ENUM_VALUE_3;
    }
    

    In this case switch statement will generate:
    error C2196: case value 'enums::EnumByte::ENUM_VALUE_0' already used
    error C2196: case value 'enums::EnumByte::ENUM_VALUE_3' already used

    So it means only an alias. And it could be handled, couldn't it? Ok, I just wanted to point it out.

    Enum value string is rarely used outside struct's dumps in logs. So << operator is more suitable. In case you need string equivalent try:

    std::string to_string(EnumByte value) const { std::stringstream ss; ss << value; return ss.str(); }
    

    Yes, such cases are rare, but they are. This way(using std::stringstream) has a huge overhead(comparing to getting const char * from switch statement).

  4. bitbugprime commented on Feb 8, 2021

    @bitbugprime

    Yes, such cases are rare, but they are. This way(using std::stringstream) has a huge overhead(comparing to getting const char * from switch statement).

    Since this is a C++17 library, an std::string_view would be a more appropriate return type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions