ballerina-platform / ballerina-lang

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

[Improvement]: Improve type conversion logic by remembering repeated checks of non-convertible types #42609

Open gabilang opened 3 weeks ago

gabilang commented 3 weeks ago

Description

$title

example case 1: When members of union have records and those records have same inconvertible record fields, currently the checks are carried out on both records. If we keep the non-convertible info somewhere, we can use it for early return. i.e.:

type Basic record {|
    int id?;
|};

type Person record {|
    string name;
    int age?;
    Basic ...;
|};

type Student record {|
    *Person;
    int studentId;
    string code?;
|};

type PartTimeStudent record {|
    *Person;
    string:Char code?;
    Student s;
|};

type StudentResearcher record {|
    *Person;
    string code?;
|};

type StudentTutor record {|
    *Person;
|};

type S StudentTutor|StudentResearcher|PartTimeStudent;

function testCloneWithTypeWithUnionOfRecordsWithTypeInclusion() returns error? {
    json student = {
        name: "Anne",
        code: "A",
        s: {
            name: "Anne",
            studentId: 1001
        }
    };
    S s = check student.cloneWithType();
}

Here the convertible type check on s: { name: "Anne", studentId: 1001 } to the record Basic is happening in each iterations.

example case 2:

Describe your problem(s)

No response

Describe your solution(s)

No response

Related area

-> Runtime

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response