reduce flash footprint by smarter code generation #4163

This commit is contained in:
rusefillc 2022-12-03 00:53:02 -05:00
parent 5e62595d69
commit 6a8a459200
2 changed files with 44 additions and 9 deletions

View File

@ -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<Integer, AtomicInteger> hashConflicts = new HashMap<>();
for (Pair<String, String> 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<String, String> 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;
}
}

View File

@ -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());
}