Skip to content

Optimize replacement logic#2

Merged
cxntered merged 4 commits intomainfrom
feat/improve-performance
May 11, 2025
Merged

Optimize replacement logic#2
cxntered merged 4 commits intomainfrom
feat/improve-performance

Conversation

@cxntered
Copy link
Owner

Optimizes replacement logic to greatly improve performance. Closes #1.
Went from dropping ~90 FPS to just ~5 FPS with 10 replacements of differing complexity (rough estimate, jesus christ though).

Changes

  • Cached the expanded text after replacing variables to prevent unnecessary recalculations.
  • Introduced variable caching, ensuring they are only updated when values are changed.
  • Heavily refactor code to leverage Kotlin features (fold, let, takeIf).

@cxntered cxntered requested a review from Copilot May 11, 2025 10:47
@cxntered cxntered self-assigned this May 11, 2025
@cxntered cxntered added the enhancement New feature or request label May 11, 2025
@cxntered cxntered linked an issue May 11, 2025 that may be closed by this pull request
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request optimizes the replacement logic to drastically improve performance by caching expanded text values and refactoring existing functionality using Kotlin’s functional constructs.

  • Cached expanded text values to avoid repeated recalculations
  • Updated UI components to support the new caching logic
  • Consolidated command handling within the main TextReplacer object

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/main/kotlin/dev/cxntered/textreplacer/elements/WrappedReplacer.kt Updated text field constructors to pass expanded text properties
src/main/kotlin/dev/cxntered/textreplacer/elements/ReplacerTextField.kt Modified text field drawing to update the cached expanded text on key input
src/main/kotlin/dev/cxntered/textreplacer/config/Replacer.kt Added transient properties for caching expanded text values
src/main/kotlin/dev/cxntered/textreplacer/TextReplacer.kt Refactored replacement logic using fold and caching, and updated command handling
src/main/java/dev/cxntered/textreplacer/mixin/FontRendererMixin.java Adjusted getString calls to the new static method format

return ReplacerListOption.wrappedReplacers.fold(input) { string, wrapper ->
with(wrapper.replacer) {
if (!enabled || text.isEmpty() || replacementText.isEmpty()) return@with string

Copy link

Copilot AI May 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a comment to clarify that the expandedText update only happens when a server or username change triggers shouldExpand; this helps future maintainers understand that additional changes in replacer.text are expected to be handled elsewhere.

Suggested change
// Update expandedText and expandedReplacementText only when shouldExpand is true
// (triggered by server or username changes) or when they are empty.
// Changes to replacer.text are expected to be handled elsewhere.

Copilot uses AI. Check for mistakes.
Comment on lines +61 to 70
with(wrapper.replacer) {
if (!enabled || text.isEmpty() || replacementText.isEmpty()) return@with string

string = string.replace("¶serverIp", serverIp)
if (shouldExpand || expandedText.isEmpty())
expandedText = expandText(text)
if (shouldExpand || expandedReplacementText.isEmpty())
expandedReplacementText = expandText(replacementText)

if (!InetAddressUtils.isIPv4Address(serverIp) && !InetAddressUtils.isIPv6Address(serverIp)) {
val parts = serverIp.split(".")
val serverDomain = parts[parts.size - 2]

string = string.replace("¶serverDomain", serverDomain)
string.replace(expandedText, expandedReplacementText)
}
Copy link

Copilot AI May 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Although the functional style with fold is correct, assigning the result of replace explicitly or refactoring this lambda to be more self-explanatory could improve clarity.

Copilot uses AI. Check for mistakes.
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please explain what you mean by this GitHub Copilot!
thanks, cxntered

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it even reply to me or do i have to request another review

if (!isToggled) return false
keyTyped(key, keyCode)
textProperty.set(input)
expandedTextProperty.set(TextReplacer.expandText(input))
Copy link

Copilot AI May 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this expansion is called on every key input, consider debouncing or throttling this operation to reduce unnecessary recalculations during rapid typing.

Copilot uses AI. Check for mistakes.
@cxntered
Copy link
Owner Author

gee whiz thanks copilot!

@cxntered cxntered force-pushed the feat/improve-performance branch from adabda3 to af7d850 Compare May 11, 2025 11:10
@cxntered cxntered merged commit a0e52db into main May 11, 2025
4 checks passed
@cxntered cxntered deleted the feat/improve-performance branch May 11, 2025 11:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve performance

2 participants