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

Possibly improved code-gen for "typeswitch" #31515

Closed
gafter opened this issue Dec 4, 2018 · 4 comments
Closed

Possibly improved code-gen for "typeswitch" #31515

gafter opened this issue Dec 4, 2018 · 4 comments

Comments

@gafter
Copy link
Member

gafter commented Dec 4, 2018

We occasionally use a "typeswitch-style" switch in Roslyn. For example

        public static bool FormattingHelpers.IsCloseParenInStatement(this SyntaxToken token)
        {
            var statement = token.Parent as StatementSyntax;
            if (statement == null)
            {
                return false;
            }

            switch (statement)
            {
                case IfStatementSyntax ifStatement:
                    return ifStatement.CloseParenToken.Equals(token);
                case SwitchStatementSyntax switchStatement:
                    return switchStatement.CloseParenToken.Equals(token);
                case WhileStatementSyntax whileStatement:
                    return whileStatement.CloseParenToken.Equals(token);
                case DoStatementSyntax doStatement:
                    return doStatement.CloseParenToken.Equals(token);
                case ForStatementSyntax forStatement:
                    return forStatement.CloseParenToken.Equals(token);
                case CommonForEachStatementSyntax foreachStatement:
                    return foreachStatement.CloseParenToken.Equals(token);
                case LockStatementSyntax lockStatement:
                    return lockStatement.CloseParenToken.Equals(token);
                case UsingStatementSyntax usingStatement:
                    return usingStatement.CloseParenToken.Equals(token);
                case FixedStatementSyntax fixedStatement:
                    return fixedStatement.CloseParenToken.Equals(token);
            }

            return false;
        }

The generated code might possibly be improved by calling GetType() on the input and including a sequence of tests against the type desired (when consecutive tests are for sealed types).

We would have to measure whether we actually get any improvement before committing to such an optimization.

@gafter
Copy link
Member Author

gafter commented Mar 17, 2019

I agree that the jit does a pretty good job. For more than about 20 types in a type switch, we can use a hashtable to implement it more efficiently. See https://github.com/gafter/TypeSwitch/blob/master/TypeSwitch/TypeSwitchDispatch.cs for one approach.

@gafter gafter modified the milestones: Backlog, 16.2 Mar 17, 2019
@gafter
Copy link
Member Author

gafter commented Mar 17, 2019

Improved the prototype library so that the break-even point for using a hashtable is about 10 types in a switch. See https://github.com/gafter/TypeSwitch/blob/master/TypeSwitch/TypeSwitchDispatch.cs

@gafter gafter modified the milestones: 16.2, Backlog Jun 3, 2019
@gafter
Copy link
Member Author

gafter commented Nov 22, 2019

Instead of the optimization discussed, we are planning to provide an API along the lines of https://github.com/dotnet/corefx/issues/36132 and use that in the compiler for efficient dispatch on type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Development

No branches or pull requests

2 participants