From 6a8a4592009b90c4572db798e52ad9532d4b225e Mon Sep 17 00:00:00 2001 From: rusefillc Date: Sat, 3 Dec 2022 00:53:02 -0500 Subject: [PATCH] reduce flash footprint by smarter code generation #4163 --- .../rusefi/output/GetOutputValueConsumer.java | 38 +++++++++++++++++-- .../java/com/rusefi/test/OutputsTest.java | 15 +++++--- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/java_tools/configuration_definition/src/main/java/com/rusefi/output/GetOutputValueConsumer.java b/java_tools/configuration_definition/src/main/java/com/rusefi/output/GetOutputValueConsumer.java index 474acad173..7d1611eafe 100644 --- a/java_tools/configuration_definition/src/main/java/com/rusefi/output/GetOutputValueConsumer.java +++ b/java_tools/configuration_definition/src/main/java/com/rusefi/output/GetOutputValueConsumer.java @@ -7,7 +7,10 @@ import com.rusefi.core.Pair; import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Function; import static com.rusefi.output.ConfigStructure.ALIGNMENT_FILL_AT; import static com.rusefi.output.DataLogConsumer.UNUSED; @@ -58,13 +61,42 @@ public class GetOutputValueConsumer implements ConfigurationConsumer { public String getContent() { StringBuilder getterBody = new StringBuilder(); + + StringBuilder switchBody = new StringBuilder(); + + HashMap hashConflicts = new HashMap<>(); for (Pair pair : getterPairs) { - getterBody.append(getCompareName(pair.first)); - getterBody.append("\t\treturn " + pair.second + ";\n"); + hashConflicts.computeIfAbsent(HashUtil.hash(pair.first), integer -> new AtomicInteger(0)).incrementAndGet(); } + + for (Pair pair : getterPairs) { + String returnLine = "\t\treturn " + pair.second + ";\n"; + + int hash = HashUtil.hash(pair.first); + if (hashConflicts.get(hash).get() == 1) { + switchBody.append("\t\tcase " + hash + ":\n"); + switchBody.append("\t" + returnLine); + + } else { + + getterBody.append(getCompareName(pair.first)); + + getterBody.append(returnLine); + } + } + + String fullSwitch = switchBody.length() == 0 ? "" : + ("\tint hash = djb2lowerCase(name);\n" + + + "\tswitch(hash) {\n" + switchBody + "\t}\n"); + return FILE_HEADER + - "float getOutputValueByName(const char *name) {\n" + getterBody + GetConfigValueConsumer.GET_METHOD_FOOTER; + "float getOutputValueByName(const char *name) {\n" + + fullSwitch + + + + getterBody + GetConfigValueConsumer.GET_METHOD_FOOTER; } } diff --git a/java_tools/configuration_definition/src/test/java/com/rusefi/test/OutputsTest.java b/java_tools/configuration_definition/src/test/java/com/rusefi/test/OutputsTest.java index 2129ece5a6..f07e83621f 100644 --- a/java_tools/configuration_definition/src/test/java/com/rusefi/test/OutputsTest.java +++ b/java_tools/configuration_definition/src/test/java/com/rusefi/test/OutputsTest.java @@ -147,12 +147,15 @@ public class OutputsTest { "#include \"pch.h\"\n" + "#include \"value_lookup.h\"\n" + "float getOutputValueByName(const char *name) {\n" + - "\tif (strEqualCaseInsensitive(name, \"issue_294_31\"))\n" + - "\t\treturn engine->outputChannels.issue_294_31;\n" + - "\tif (strEqualCaseInsensitive(name, \"enableFan1WithAc\"))\n" + - "\t\treturn engine->outputChannels.enableFan1WithAc;\n" + - "\tif (strEqualCaseInsensitive(name, \"hwChannel\"))\n" + - "\t\treturn engine->outputChannels.hwChannel;\n" + + "\tint hash = djb2lowerCase(name);\n" + + "\tswitch(hash) {\n" + + "\t\tcase -1571463185:\n" + + "\t\t\treturn engine->outputChannels.issue_294_31;\n" + + "\t\tcase -298185774:\n" + + "\t\t\treturn engine->outputChannels.enableFan1WithAc;\n" + + "\t\tcase -709106787:\n" + + "\t\t\treturn engine->outputChannels.hwChannel;\n" + + "\t}\n" + "\treturn EFI_ERROR_CODE;\n" + "}\n", outputValueConsumer.getContent()); }