Skip to content

Constant fold enum member initializers. #917

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

Closed
mbebenita opened this issue Oct 18, 2014 · 5 comments
Closed

Constant fold enum member initializers. #917

mbebenita opened this issue Oct 18, 2014 · 5 comments
Labels
Fixed A PR has been merged for this issue Help Wanted You can do this In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@mbebenita
Copy link

Consider the following enum definition:

enum Flags {
    A = 1,
    B = 2,
    C = A | B // Should be constant folded to 3.
}

In most cases, JS engines can optimize simple expressions like these. But they can't do much about accesses to Flags.C.

switch (x) {
  case Flags.A: ... break; // Compiled as case 1: ...
  case Flags.B: ... break; // Compiled as case 2: ...
  case Flags.C: ... break; // Compiled as Flags.C: ...  
}

This prevents JS engines from using a table jump and causes them to fall back on a sequential if/else if implementation for the switch which is all sorts of terrible.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Help Wanted You can do this In Discussion Not yet reached consensus labels Oct 18, 2014
@RyanCavanaugh
Copy link
Member

We've talked about this before as something we'd like to do -- the old compiler would do this under an experimental flag and it was nice for a small perf boost. This also would be a necessary first step for a "zero emit" enum that some we had interest in.

As a rough outline I'd expect this to support math over |, &, ~, +, -, integer literals, and previously-declared enum members.

@DanielRosenwasser
Copy link
Member

I think I'll take this on.

@DanielRosenwasser DanielRosenwasser self-assigned this Oct 25, 2014
@vladima
Copy link
Contributor

vladima commented Oct 25, 2014

Oops, I've already started working on this one.


From: Daniel Rosenwassermailto:[email protected]
Sent: ý10/ý25/ý2014 11:22 AM
To: Microsoft/TypeScriptmailto:[email protected]
Subject: Re: [TypeScript] Constant fold enum member initializers. (#917)

I think I'll take this on.


Reply to this email directly or view it on GitHubhttps://github.com//issues/917#issuecomment-60491992.

@DanielRosenwasser
Copy link
Member

Oops 😄 - I currently propagate arithmetic ops on constants but not previously declared enums. You're probably farther than me, right?

@vladima
Copy link
Contributor

vladima commented Nov 4, 2014

covered by #970

@vladima vladima closed this as completed Nov 4, 2014
@danquirk danquirk added the Fixed A PR has been merged for this issue label Nov 5, 2014
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Fixed A PR has been merged for this issue Help Wanted You can do this In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants