Moodle

Why "role_assign()" && "role_unassign()" takes so much time to execute...?!

Details

  • Type: Improvement Improvement
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.9, 1.9.1
  • Fix Version/s: 2.0
  • Labels:
    None
  • Environment:
  • Database:
    MySQL
  • Affected Branches:
    MOODLE_19_STABLE
  • Fixed Branches:
    MOODLE_20_STABLE

Description

I explain my problem :
The function role_assign is really heavy.
I made a development to enrol students automatically in their course spaces and groups (it's a synchronization with LDAP).
When I call the function role_assign (or role_unassign it's the same problem), to enrol 186 students in a group and in a context of a course, it takes 213 secs!

So, I tried to understand why before I post in this forum and I think I have a possible reason:
I duplicated the function role_assign in a personalized function called role_assign_iut and I just commented the lines :

/// Do some data validation

if (empty($roleid)) { debugging('Role ID not provided'); return false; }

if (empty($userid) && empty($groupid)) { debugging('Either userid or groupid must be provided'); return false; }

if ($userid && !record_exists('user', 'id', $userid)) { debugging('User ID '.intval($userid).' does not exist!'); return false; }

if ($groupid && !groups_group_exists($groupid)) { debugging('Group ID '.intval($groupid).' does not exist!'); return false; }

if (!$context = get_context_instance_by_id($contextid)) { debugging('Context ID '.intval($contextid).' does not exist!'); return false; }

if (($timestart and $timeend) and ($timestart > $timeend)) { debugging('The end time can not be earlier than the start time'); return false; }

So now, the execution time takes only 2secs!
I just commented these lines because I know my parameters are good, I checked them before the execution.

My questions are :
Why these params verifications takes so long?
Why are these verifications here?
Is it just to be sure the developers like me don't use bad params?

Regards,
MELIX Simon

Issue Links

Activity

Hide
Dan Poltawski added a comment -

have you tried just commenting out individual validation functions?

(I would imagine the userrid/ groupid/ context checks would be the slow ones since they are likely to cause a db query per call)

Show
Dan Poltawski added a comment - have you tried just commenting out individual validation functions? (I would imagine the userrid/ groupid/ context checks would be the slow ones since they are likely to cause a db query per call)
Hide
Petr Škoda (skodak) added a comment -

Hello,
hopefully this should be significantly improved in 2.0. Please test it there and create new issue if necessary.

Thank you for the report.

Petr Škoda

Show
Petr Škoda (skodak) added a comment - Hello, hopefully this should be significantly improved in 2.0. Please test it there and create new issue if necessary. Thank you for the report. Petr Škoda

People

Vote (2)
Watch (6)

Dates

  • Created:
    Updated:
    Resolved: