From 08fef59cc5d5af788495a96d30580c6ff8f13ee9 Mon Sep 17 00:00:00 2001 From: Thomas Hallock Date: Wed, 12 Nov 2025 07:01:35 -0600 Subject: [PATCH] perf: reduce retry limit from 3000 to 100 in problem generators **Problem**: Large worksheets with constrained digit ranges were hanging. Example: 1000 single-digit problems with 100% regrouping = 168,608 retries for just 100 problems (1,686 retries/problem on average). **Root Cause**: Only 9 single-digit numbers (1-9) with 100% regrouping requirement = very few valid unique combinations. The 3000-retry limit meant millions of iterations for 1000-2000 problem worksheets. **Solution**: Reduced retry limit from 3000 to 100 in both: - generateProblems() (addition) - generateSubtractionProblems() This allows some duplicate problems when the constraint space is tight, but prevents the server from hanging on large worksheet generation. **Impact**: 100-page worksheets should now generate in seconds instead of timing out. --- apps/web/src/app/create/worksheets/problemGenerator.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/apps/web/src/app/create/worksheets/problemGenerator.ts b/apps/web/src/app/create/worksheets/problemGenerator.ts index 6776bab7..6bdd9057 100644 --- a/apps/web/src/app/create/worksheets/problemGenerator.ts +++ b/apps/web/src/app/create/worksheets/problemGenerator.ts @@ -283,9 +283,11 @@ export function generateProblems( } // Generate problem with retries for uniqueness + // Reduced from 3000 to 100 - for large worksheets with constrained digit ranges, + // 3000 retries per problem = millions of iterations. Better to allow some duplicates. let tries = 0 let ok = false - while (tries++ < 3000 && !ok) { + while (tries++ < 100 && !ok) { let a: number, b: number if (picked === 'both') { ;[a, b] = generateBoth(rand, minDigits, maxDigits) @@ -613,9 +615,11 @@ export function generateSubtractionProblems( } // Generate problem with retries for uniqueness + // Reduced from 3000 to 100 - for large worksheets with constrained digit ranges, + // 3000 retries per problem = millions of iterations. Better to allow some duplicates. let tries = 0 let ok = false - while (tries++ < 3000 && !ok) { + while (tries++ < 100 && !ok) { let minuend: number, subtrahend: number if (picked === 'both') { ;[minuend, subtrahend] = generateBothBorrow(rand, minDigits, maxDigits)