From 7f353e925b7dbd6d0c16962e0e822aea0093308b Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Mon, 11 May 2026 17:17:54 -0500 Subject: perf(lorem-optimum): speed up the Markov generation path `cj/markov-join-tokens' collects tokens in a list and `mapconcat's once instead of repeated string concatenation. `cj/markov-generate' uses `push'/`nreverse' instead of repeated `append'. The Markov keys are cached as a vector so random key selection is O(1). Re-enabled the benchmark tests (the `:slow' tags were stale) and added a `cj/lipsum-title' test after byte-compilation flagged a malformed form there. `assets/liber-primus.txt' is left as-is (36 KB / 5,374 words, small enough not to need trimming). 100K-word learning now measures about 196 ms. --- tests/test-lorem-optimum-benchmark.el | 15 ++++----------- tests/test-lorem-optimum.el | 9 +++++++++ 2 files changed, 13 insertions(+), 11 deletions(-) (limited to 'tests') diff --git a/tests/test-lorem-optimum-benchmark.el b/tests/test-lorem-optimum-benchmark.el index 57d5ae5f..6b2f0163 100644 --- a/tests/test-lorem-optimum-benchmark.el +++ b/tests/test-lorem-optimum-benchmark.el @@ -63,10 +63,7 @@ (should (< time 50.0)))) ; Should be < 50ms (ert-deftest benchmark-learn-10k-words () - "Benchmark learning 10,000 words. -DISABLED: Takes too long (minutes instead of seconds). -Needs lorem-optimum performance optimization before re-enabling." - :tags '(:slow) + "Benchmark learning 10,000 words." (let* ((text (generate-test-text 10000)) (chain (cj/markov-chain-create)) (time (benchmark-time @@ -76,7 +73,6 @@ Needs lorem-optimum performance optimization before re-enabling." (ert-deftest benchmark-learn-100k-words () "Benchmark learning 100,000 words (stress test)." - :tags '(:slow) (let* ((text (generate-test-text 100000)) (chain (cj/markov-chain-create)) (time (benchmark-time @@ -84,7 +80,8 @@ Needs lorem-optimum performance optimization before re-enabling." (benchmark-report "Learn 100K words" time) ;; This may be slow due to key rebuild (message "Hash table size: %d bigrams" - (hash-table-count (cj/markov-chain-map chain))))) + (hash-table-count (cj/markov-chain-map chain))) + (should (< time 5000.0)))) ;;; Multiple Learning Operations (Exposes Quadratic Behavior) @@ -109,7 +106,6 @@ Needs lorem-optimum performance optimization before re-enabling." (ert-deftest benchmark-multiple-learns-100x100 () "Benchmark 100 learn operations of 100 words each (key rebuild overhead)." - :tags '(:slow) (let ((chain (cj/markov-chain-create)) (times '()) (measurements '())) @@ -154,10 +150,7 @@ Needs lorem-optimum performance optimization before re-enabling." ;;; Tokenization Performance Tests (ert-deftest benchmark-tokenize-10k-words () - "Benchmark tokenizing 10,000 words. -DISABLED: Takes too long (minutes instead of seconds). -Needs lorem-optimum performance optimization before re-enabling." - :tags '(:slow) + "Benchmark tokenizing 10,000 words." (let* ((text (generate-test-text 10000)) (time (benchmark-time (lambda () (cj/markov-tokenize text))))) diff --git a/tests/test-lorem-optimum.el b/tests/test-lorem-optimum.el index ca2e52f4..40bdc684 100644 --- a/tests/test-lorem-optimum.el +++ b/tests/test-lorem-optimum.el @@ -238,5 +238,14 @@ (let ((result (cj/markov-generate chain 2))) (should (stringp result))))) +(ert-deftest test-title-generation-produces-title () + "Should generate a non-empty title from the global chain." + (let ((cj/lipsum-chain + (test-learn "lorem ipsum dolor sit amet consectetur adipiscing elit"))) + (let ((result (cj/lipsum-title))) + (should (stringp result)) + (should (> (length result) 0)) + (should (string-match-p "^[[:upper:]]" result))))) + (provide 'test-lorem-optimum) ;;; test-lorem-optimum.el ends here -- cgit v1.2.3