Skip to content

Commit 911f4e9

Browse files
committed
Micro optimization to speed up problem import.
On my machine, this does speed up a problem with hundreds of test cases and hundreds of submissions by 4%. Part of #2019
1 parent 05fc17a commit 911f4e9

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

webapp/src/Service/ImportProblemService.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ public function importZippedProblem(
6868
$messages[$type] = [];
6969
}
7070

71+
$zipEntries = [];
72+
for ($j = 0; $j < $zip->numFiles; $j++) {
73+
$zipEntries[$j] = $zip->getNameIndex($j);
74+
}
75+
7176
$propertiesFile = 'domjudge-problem.ini';
7277
$yamlFile = 'problem.yaml';
7378
$tleFile = '.timelimit';
@@ -269,7 +274,7 @@ public function importZippedProblem(
269274
if (!$this->parseYaml($problemYaml, $messages, $validationMode, $propertyAccessor, $problem)) {
270275
return null;
271276
}
272-
if (!$this->searchAndAddValidator($zip, $messages, $externalId, $validationMode, $problem)) {
277+
if (!$this->searchAndAddValidator($zip, $zipEntries, $messages, $externalId, $validationMode, $problem)) {
273278
return null;
274279
}
275280

@@ -343,8 +348,7 @@ public function importZippedProblem(
343348
foreach (['sample', 'secret'] as $type) {
344349
$numCases = 0;
345350
$dataFiles = [];
346-
for ($j = 0; $j < $zip->numFiles; $j++) {
347-
$filename = $zip->getNameIndex($j);
351+
foreach ($zipEntries as $filename) {
348352
if (Utils::startsWith($filename, sprintf('data/%s/', $type)) &&
349353
Utils::endsWith($filename, '.in')) {
350354
$fileout = preg_replace("/\.in$/", ".ans", $filename);
@@ -505,8 +509,7 @@ public function importZippedProblem(
505509
$touchedAttachments = [];
506510

507511
$numAttachments = 0;
508-
for ($j = 0; $j < $zip->numFiles; $j++) {
509-
$filename = $zip->getNameIndex($j);
512+
foreach ($zipEntries as $j => $filename) {
510513
if (!Utils::startsWith($filename, 'attachments/')) {
511514
continue;
512515
}
@@ -639,8 +642,7 @@ public function importZippedProblem(
639642
Utils::jsonDecode($submission_file_string);
640643

641644
$numJurySolutions = 0;
642-
for ($j = 0; $j < $zip->numFiles; $j++) {
643-
$path = $zip->getNameIndex($j);
645+
foreach ($zipEntries as $j => $path) {
644646
if (!Utils::startsWith($path, 'submissions/')) {
645647
// Skipping non-submission files silently.
646648
continue;
@@ -663,8 +665,7 @@ public function importZippedProblem(
663665
$indices = [];
664666
$length = mb_strrpos($path, '/') + 1;
665667
$prefix = mb_substr($path, 0, $length);
666-
for ($k = 0; $k < $zip->numFiles; $k++) {
667-
$file = $zip->getNameIndex($k);
668+
foreach ($zipEntries as $k => $file) {
668669
// Only allow multi-file submission with all files directly under the directory.
669670
if (strncmp($prefix, $file, $length) == 0 && mb_strlen($file) > $length &&
670671
mb_strrpos($file, '/') + 1 == $length) {
@@ -902,13 +903,14 @@ public function importProblemFromRequest(Request $request, ?int $contestId = nul
902903
}
903904

904905
/**
906+
*
907+
* @param array<int, string> $zipEntries
905908
* @param array{danger?: string[], info?: string[]} $messages
906909
*/
907-
private function searchAndAddValidator(ZipArchive $zip, ?array &$messages, string $externalId, string $validationMode, ?Problem $problem): bool
910+
private function searchAndAddValidator(ZipArchive $zip, array $zipEntries, ?array &$messages, string $externalId, string $validationMode, ?Problem $problem): bool
908911
{
909912
$validatorFiles = [];
910-
for ($i = 0; $i < $zip->numFiles; $i++) {
911-
$filename = $zip->getNameIndex($i);
913+
foreach ($zipEntries as $filename) {
912914
foreach (['output_validators/', 'output_validator'] as $dir) {
913915
if (Utils::startsWith($filename, $dir) &&
914916
!Utils::endsWith($filename, '/')) {

0 commit comments

Comments
 (0)