Simplify titlecase() to unique + match (drop mutable cache) - #164
Merged
Merged
Conversation
titlecase() used a local() closure holding two lists that were grown on every cache miss and lived for the whole session. The speedup it was meant to provide (#129) comes entirely from converting only distinct values, which unique()/match() already does within a call, without the mutable global state. Replace the memoized implementation with a plain unique + toTitleCase + match. Output is identical, and a benchmark on 200k repeated values shows the simple version is marginally faster than the warm-cache memoized version (~1.95s vs ~2.26s over 20 runs). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
yihui
marked this pull request as ready for review
September 23, 2026 19:06
LittleBeannie
approved these changes
Sep 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
titlecase()previously used alocal({})closure with a mutable cache (list keyed by input value) to memoizetools::toTitleCase()calls across invocations (issue #129). This replaces it with a plain unique + match:Why
toTitleCase()was already only ever called on distinct values within a single call; the cross-call cache only helped if the same strings recurred across separatetitlecase()invocations, which is not the hot path.lower = TRUE/FALSEpaths.Testing
tests/testthat/test-ae_listing.R(coverstitlecase()/propercase()): 5/5 pass.This PR is independent of the bug-fix PR (#162) and the vectorization PR — it touches only
R/ae_listing.R.