Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

Use bounded cache for converted R variable names

AuthorDaveJarvis <email>
Date2022-11-27 10:59:30 GMT-0800
Commit0a077eff8cc3d4c7a7e22d1e95b1cfc4ac6a966a
Parenta5bf143
Delta12 lines added, 2 lines removed, 10-line increase
src/main/java/com/keenwrite/sigils/RKeyOperator.java
package com.keenwrite.sigils;
+import com.keenwrite.collections.BoundedCache;
+
import java.util.function.UnaryOperator;
private static final char KEY_SEPARATOR_R = '$';
+ /** Minor optimization to avoid recreating an object. */
private final StringBuilder mVarName = new StringBuilder( 128 );
+
+ /** Optimization to avoid re-converting variable names into R format. */
+ private final BoundedCache<String, String> mVariables = new BoundedCache<>(
+ 2048
+ );
/**
assert !key.isBlank();
- mVarName.setLength( 0 );
+ return mVariables.computeIfAbsent( key, this::convert );
+ }
- //final var rVarName = new StringBuilder( key.length() + 3 );
+ private String convert( final String key ) {
+ mVarName.setLength( 0 );
mVarName.append( "v" );
mVarName.append( KEY_SEPARATOR_R );