Skip to content
Snippets Groups Projects
Commit 07a7ac9a authored by Tim Hunt's avatar Tim Hunt
Browse files

Performance: reduce calls stack_maxima_latex_tidy > stack_string

This is a bit quick and dirty. The use of static here is dangerous. E.g.
this will break if one script tries to process things where some is in
one language and some is in another.

However, perf testing a quiz with 7 STACK questions, that caused
685 calls to stack_maxima_latex_tidy which caused ~7000 calls
to Moodle's get_string, which too about 1.5s of the page load time.
So, in the common case where everything is in one language, this is
a worthwhile performance win. If it causes bugs for some people, we
can fix that later.
parent b9701f76
No related branches found
No related tags found
No related merge requests found
...@@ -50,23 +50,42 @@ function stack_string($key, $a = null) { ...@@ -50,23 +50,42 @@ function stack_string($key, $a = null) {
return stack_maths::process_lang_string(get_string($key, 'qtype_stack', $a)); return stack_maths::process_lang_string(get_string($key, 'qtype_stack', $a));
} }
/**
* Private helper used by the next function.
*
* @return array search => replace strings.
*/
function get_stack_maxima_latex_replacements() {
static $replacements = null;
if ($replacements === null) {
$replacements = [
'QMCHAR' => '?',
'!LEFTSQ!' => '\left[',
'!LEFTR!' => '\left(',
'!RIGHTSQ!' => '\right]',
'!RIGHTR!' => '\right)',
'!ANDOR!' => stack_string('equiv_ANDOR'),
'!SAMEROOTS!' => stack_string('equiv_SAMEROOTS'),
'!MISSINGVAR!' => stack_string('equiv_MISSINGVAR'),
'!ASSUMEPOSVARS!' => stack_string('equiv_ASSUMEPOSVARS'),
'!ASSUMEPOSREALVARS!' => stack_string('equiv_ASSUMEPOSREALVARS'),
'!LET!' => stack_string('equiv_LET'),
'!AND!' => stack_string('equiv_AND'),
'!OR!' => stack_string('equiv_OR'),
'!NOT!' => stack_string('equiv_NOT'),
];
}
return $replacements;
}
/** /**
* This function tidies up LaTeX from Maxima. * This function tidies up LaTeX from Maxima.
* @param string $rawfeedback * @param string $rawfeedback
* @return string * @return string
*/ */
function stack_maxima_latex_tidy($latex) { function stack_maxima_latex_tidy($latex) {
$dispfix = array('QMCHAR' => '?', '!LEFTSQ!' => '\left[', '!LEFTR!' => '\left(', $replacements = get_stack_maxima_latex_replacements();
'!RIGHTSQ!' => '\right]', '!RIGHTR!' => '\right)'); $latex = str_replace(array_keys($replacements), array_values($replacements), $latex);
// Need to add this in here also because strings may contain question mark characters.
foreach ($dispfix as $key => $fix) {
$latex = str_replace($key, $fix, $latex);
}
$loctags = array('ANDOR', 'SAMEROOTS', 'MISSINGVAR', 'ASSUMEPOSVARS', 'ASSUMEPOSREALVARS', 'LET',
'AND', 'OR', 'NOT');
foreach ($loctags as $tag) {
$latex = str_replace('!'.$tag.'!', stack_string('equiv_'.$tag), $latex);
}
// Also previously some spaces have been eliminated and line changes dropped. // Also previously some spaces have been eliminated and line changes dropped.
// Apparently returning verbatim LaTeX was not a thing. // Apparently returning verbatim LaTeX was not a thing.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment