0

I have a struct that has a number of fields and a mapping of StudentInfo array.

struct StudentInfo {    
    uint studentId;
    uint courseId;
    uint age;
    string firstName; 
    string lastName; 
    string gender;         
    bool hasPassed;          
}

mapping(address => StudentInfo[]) public studentInfo;

Basically, a teacher can retrieve his/her list of studentInfo.

A new student info can be saved into studentInfo state variable via the function below:

function storeStudent(uint _studentId, uint _courseId, uint age, string _firstName, string _lastName, string gender) public returns (bool) {  
    studentInfo[msg.sender].push(StudentInfo(_studentId, _courseId, age, _firstName, _lastName, gender, 0));                
}

and when the teacher needs to retrieve the list of studentInfo, I call it like below:

function getStudentInfo(address _teacher) public view returns (uint[], uint[], uint[]) {
    uint length = studentInfo[_teacher].length;
    uint[] memory studentId = new uint[](length);
    uint[] memory courseId = new uint[](length);
    uint[] memory age = new uint[](length);   

    for (uint i = 0; i < length; i++) {
        studentId[i] = studentInfo[_teacher][i].studentId;
        courseId[i] = studentInfo[_teacher][i].courseId;
        age[i] = studentInfo[_teacher][i].age;
    }

    return (studentId, courseId, age);
}

function getStudentInfo2(address _teacher) public view returns (string[], string[], string[], bool[]) {
    uint length = studentInfo[_teacher].length;  
    string[] memory firstName = new string[](length);
    string[] memory lastName = new string[](length);
    string[] memory gender = new string[](length);
    bool[] memory hasPassed = new bool[](length);

    for (uint i = 0; i < length; i++) {     
        firstName [i] = studentInfo[_teacher][i].firstName ;
        lastName [i] = studentInfo[_teacher][i].lastName ;
        gender [i] = studentInfo[_teacher][i].gender ;
        hasPassed [i] = studentInfo[_teacher][i].hasPassed ;
    }

    return (firstName, lastName, gender, hasPassed);
}

Those functions are working, but I'm just wondering if there could be any performance or gas (runs out) related issues if the size of studentInfo gets bigger and bigger.

Can the above logics be optimized?

1 Answer 1

0

You can avoid the loop by having a mapping of struct instead of a mapping of arrays of structs.

Then the struct will contain arrays for each parameter:

mapping(address => StudentInfo) public studentInfo;

struct StudentInfo {    
    uint[] studentId;
    uint[] courseId;
    uint[] age;
    string[] firstName; 
    string[] lastName; 
    string[] gender;         
    bool[] hasPassed;          
}

You can get, for instance, all the first names in one shot doing:

studentInfo['teacherAddress'].fristName

Same for the other parameters

Hope this helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.