ballerina-platform / ballerina-lang

The Ballerina Programming Language
https://ballerina.io/
Apache License 2.0
3.66k stars 750 forks source link

[Improvement]: Improve record field access time #41868

Open heshanpadmasiri opened 10 months ago

heshanpadmasiri commented 10 months ago

Description

Current field access for record types happens via a virtual function call fallowed by a static function call and a switch (compiled to a jump table) based on the field name as a string. However in most common circumstances (such as when we know the type of the record value is exactly the same as the variable) we can simplify this into just a getfield / putfield instruction (probably still invoked via a function call, but without the jump table).

For this we may have to port the exactness tracking optimization from nBallerina.

Describe your problem(s)

No response

Describe your solution(s)

No response

Related area

-> Compilation

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

heshanpadmasiri commented 10 months ago

For this we have planned to implement/experiment with few ideas,

  1. When accessing record fields use the "typed" versions of getters where possible instead of generic get method. This will require record types to override these methods with direct access to record fields.
  2. Introduce unboxed get methods for basic types such as int, float and boolean. Currently get method box the value (record holds unboxed values) and we add additional code to unbox values after the function call.
  3. Experiment with using exactness tracking (maybe we can use instanceof instruction to do this without having to port nBallerina's exactness tracking) to cast the record type to exact value and using direct field access instructions (putField and getField)

1 and 2 should improve runtime performance (with the downside of increasing the file size since all record types will now implement additional functions). At this point 3 will be trading the cost of virtual function call and switch statement for instanceOf. Note we can eliminate the cost of instanceOf as well if we implement compile time exactness tracking.