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

Improve code for get*() series of methods for data constructors #505

Open
rahulmutt opened this issue Sep 9, 2017 · 3 comments
Open

Improve code for get*() series of methods for data constructors #505

rahulmutt opened this issue Sep 9, 2017 · 3 comments

Comments

@rahulmutt
Copy link
Member

The get*() series of methods are used to access the fields of a data constructor by index, used by selector thunks. getP corresponds to normal lazy fields, getN corresponds to strict integer fields, and so on for all the primitive types. A list of all of them can be found in eta.runtime.stg.DataCon.

This is the generated code for (,) or ghc_prim.ghc.Types$Z2TD.

  public eta.runtime.stg.Closure getP(int);
    descriptor: (I)Leta/runtime/stg/Closure;
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=2, args_size=2
         0: iload_1
         1: lookupswitch  { // 2
                       1: 41
                       2: 48
                 default: 28
            }
        28: ldc           #25                 // String getP: invalid field index!
        30: iconst_0
        31: anewarray     #27                 // class java/lang/Object
        34: invokestatic  #29                 // Method eta/runtime/RuntimeLogging.barf:(Ljava/lang/String;[Ljava/lang/Object;)V
        37: aconst_null
        38: goto          55
        41: aload_0
        42: getfield      #16                 // Field x1:Leta/runtime/stg/Closure;
        45: goto          55
        48: aload_0
        49: getfield      #20                 // Field x2:Leta/runtime/stg/Closure;
        52: goto          55
        55: return

The improved code should:

  1. Create a local variable to store the result of each case of the switch, actually store the result in each case into that local variable, and return it at the very end. The above style of bytecode while efficient, is not Java-like, and unlikely to be optimised by the JIT.

  2. Call super.getP() for the default case to save some space storing the invalid field index! string which is anyways present.

The improvements should be made in ETA.CodeGen.Main.cgDataCon.

@kpadmasola
Copy link

@rahulmutt New contributor here -- I'd be happy to give this one a try. Can you point me to the relevant resources?

@rahulmutt
Copy link
Member Author

@kpadmasola Great! I can point you to the parts of the codebase that will need to be modified:

https://github.com/typelead/eta/blob/master/compiler/ETA/CodeGen/Main.hs#L356-L361

So basically you need to modify defineGetRep to generate slightly better code. You'll need some basic idea of JVM bytecode this - feel free to ask questions on generating any particular instructions.

@kpadmasola
Copy link

@rahulmutt , Ok, will go through the code and references, Thanks.

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

No branches or pull requests

2 participants