Skip to content

Commit

Permalink
Improve ISymbol#toString() performance
Browse files Browse the repository at this point in the history
  • Loading branch information
axkr committed Oct 16, 2024
1 parent 4814e52 commit 46372e5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1069,14 +1069,9 @@ public void setRulesData(RulesData rd) {

@Override
public String toString() {
try {
StringBuilder sb = new StringBuilder();
OutputFormFactory.get(EvalEngine.get().isRelaxedSyntax()).convertSymbol(sb, this);
return sb.toString();
} catch (Exception e1) {
Errors.rethrowsInterruptException(e1);
return fSymbolName;
}
final Context context = getContext();
final String symbolName = getSymbolName();
return ISymbol.toString(context, symbolName, EvalEngine.get());
}

/** {@inheritDoc} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1083,14 +1083,9 @@ public void setRulesData(RulesData rd) {

@Override
public String toString() {
try {
StringBuilder sb = new StringBuilder();
OutputFormFactory.get(EvalEngine.get().isRelaxedSyntax()).convertSymbol(sb, this);
return sb.toString();
} catch (Exception e1) {
Errors.rethrowsInterruptException(e1);
return fSymbolName;
}
final Context context = getContext();
final String symbolName = getSymbolName();
return ISymbol.toString(context, symbolName, EvalEngine.get());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -834,23 +834,24 @@ public void convertString(final Appendable buf, final String str) throws IOExcep
}

public void convertSymbol(final Appendable buf, final ISymbol symbol) throws IOException {
Context context = symbol.getContext();
if (context == Context.DUMMY) {
append(buf, symbol.getSymbolName());
return;
}
if (context.equals(Context.SYSTEM)) {
String str = AST2Expr.PREDEFINED_SYMBOLS_MAP.get(symbol.getSymbolName());
if (str != null) {
append(buf, str);
return;
}
}
if (EvalEngine.get().getContextPath().contains(context)) {
append(buf, symbol.getSymbolName());
} else {
append(buf, context.completeContextName() + symbol.getSymbolName());
}
append(buf, ISymbol.toString(symbol.getContext(), symbol.getSymbolName(), EvalEngine.get()));
// Context context = symbol.getContext();
// if (context == Context.DUMMY) {
// append(buf, symbol.getSymbolName());
// return;
// }
// if (context.equals(Context.SYSTEM)) {
// String str = AST2Expr.PREDEFINED_SYMBOLS_MAP.get(symbol.getSymbolName());
// if (str != null) {
// append(buf, str);
// return;
// }
// }
// if (EvalEngine.get().getContextPath().contains(context)) {
// append(buf, symbol.getSymbolName());
// } else {
// append(buf, context.completeContextName() + symbol.getSymbolName());
// }
}

public void convertPattern(final Appendable buf, final IPatternObject pattern)
Expand Down Expand Up @@ -1539,7 +1540,7 @@ private void convert(final Appendable buf, final IExpr o, final int precedence,
} else if (o instanceof IPatternObject) {
convertPattern(buf, (IPatternObject) o);
} else if (o instanceof IStringX) {
convertString(buf, ((IStringX) o).toString());
convertString(buf, o.toString());
} else {
convertString(buf, o.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.function.Predicate;
import org.hipparchus.special.elliptic.jacobi.Theta;
import org.matheclipse.core.builtin.AttributeFunctions;
import org.matheclipse.core.convert.AST2Expr;
import org.matheclipse.core.convert.Object2Expr;
import org.matheclipse.core.eval.EvalEngine;
import org.matheclipse.core.eval.exception.ArgumentTypeException;
Expand Down Expand Up @@ -276,7 +277,7 @@ default boolean hasProtectedAttribute() {
}

static IAST symbolDefinition(ISymbol symbol) {

if (symbol.equals(S.In)) {
IAST list = EvalEngine.get().getEvalHistory().definitionIn();
IASTAppendable result = F.ListAlloc(list.isNIL() ? 1 : list.size());
Expand All @@ -288,15 +289,15 @@ static IAST symbolDefinition(ISymbol symbol) {
result.appendArgs(list);
return result;
}

List<IAST> rules = null;
RulesData rulesData = symbol.getRulesData();
if (rulesData != null) {
rules = rulesData.definition();
}
IASTAppendable result = F.ListAlloc(rules == null ? 1 : rules.size() + 1);
result = F.ListAlloc(rules == null ? 1 : rules.size() + 1);

if (symbol.hasAssignedSymbolValue()) {
IExpr assignedValue = symbol.assignedValue();
if (symbol.isEvalFlagOn(SETDELAYED_FLAG_ASSIGNED_VALUE)) {
Expand Down Expand Up @@ -1092,7 +1093,24 @@ public boolean removeRule(final int setSymbol, final boolean equalRule, final IE
*
* @param stream
* @throws java.io.IOException
* @return <code>false</code> if the symbol contains no rule definion.
* @return <code>false</code> if the symbol contains no rule definition.
*/
public boolean writeRules(java.io.ObjectOutputStream stream) throws java.io.IOException;

public static String toString(final Context context, final String symbolName, EvalEngine engine) {
if (context == Context.SYSTEM) {
String str = AST2Expr.PREDEFINED_SYMBOLS_MAP.get(symbolName);
if (str != null) {
return str;
}
} else if (context == Context.DUMMY) {
return symbolName;
} else if (context == Context.RUBI) {
return context.completeContextName() + symbolName;
}
if (engine != null && engine.getContextPath().contains(context)) {
return symbolName;
}
return context.completeContextName() + symbolName;
}
}

0 comments on commit 46372e5

Please sign in to comment.