reduce flash footprint by smarter code generation #4163

This commit is contained in:
rusefillc 2022-12-03 01:06:41 -05:00
parent a2216dc485
commit 8f07285034
3 changed files with 45 additions and 34 deletions

View File

@ -140,12 +140,15 @@ public class GetConfigValueConsumer implements ConfigurationConsumer {
@NotNull @NotNull
public String getComleteGetterBody() { public String getComleteGetterBody() {
StringBuilder getterBody = new StringBuilder(); StringBuilder switchBody = new StringBuilder();
for (Pair<String, String> pair : getterPairs) {
getterBody.append(getCompareName(pair.first)); StringBuilder getterBody = GetOutputValueConsumer.getGetters(switchBody, getterPairs);
getterBody.append("\t\treturn " + pair.second + ";\n");
} String fullSwitch = GetOutputValueConsumer.wrapSwitchStatement(switchBody);
return GET_METHOD_HEADER + getterBody + GET_METHOD_FOOTER;
return GET_METHOD_HEADER +
fullSwitch +
getterBody + GET_METHOD_FOOTER;
} }
@NotNull @NotNull

View File

@ -64,19 +64,23 @@ public class GetOutputValueConsumer implements ConfigurationConsumer {
StringBuilder getterBody = getGetters(switchBody, getterPairs); StringBuilder getterBody = getGetters(switchBody, getterPairs);
String fullSwitch = switchBody.length() == 0 ? "" : String fullSwitch = wrapSwitchStatement(switchBody);
("\tint hash = djb2lowerCase(name);\n" +
"\tswitch(hash) {\n" + switchBody + "\t}\n");
return FILE_HEADER + return FILE_HEADER +
"float getOutputValueByName(const char *name) {\n" + "float getOutputValueByName(const char *name) {\n" +
fullSwitch + fullSwitch +
getterBody + GetConfigValueConsumer.GET_METHOD_FOOTER; getterBody + GetConfigValueConsumer.GET_METHOD_FOOTER;
} }
@NotNull
static String wrapSwitchStatement(StringBuilder switchBody) {
String fullSwitch = switchBody.length() == 0 ? "" :
("\tint hash = djb2lowerCase(name);\n" +
"\tswitch(hash) {\n" + switchBody + "\t}\n");
return fullSwitch;
}
@NotNull @NotNull
static StringBuilder getGetters(StringBuilder switchBody, List<Pair<String, String>> getterPairs1) { static StringBuilder getGetters(StringBuilder switchBody, List<Pair<String, String>> getterPairs1) {
HashMap<Integer, AtomicInteger> hashConflicts = getHashConflicts(getterPairs1); HashMap<Integer, AtomicInteger> hashConflicts = getHashConflicts(getterPairs1);

View File

@ -5,8 +5,6 @@ import com.rusefi.ReaderState;
import com.rusefi.output.GetConfigValueConsumer; import com.rusefi.output.GetConfigValueConsumer;
import org.junit.Test; import org.junit.Test;
import java.io.IOException;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class GetConfigValueConsumerTest { public class GetConfigValueConsumerTest {
@ -78,8 +76,11 @@ public class GetConfigValueConsumerTest {
"\t\t\treturn *(float*)hackEngineConfigurationPointer(known->value);\n" + "\t\t\treturn *(float*)hackEngineConfigurationPointer(known->value);\n" +
"\t\t}\n" + "\t\t}\n" +
"\t}\n" + "\t}\n" +
"\tif (strEqualCaseInsensitive(name, \"iat.adcChannel\"))\n" + "\tint hash = djb2lowerCase(name);\n" +
"\t\treturn config->iat.adcChannel;\n" + "\tswitch(hash) {\n" +
"\t\tcase -1237776078:\n" +
"\t\t\treturn config->iat.adcChannel;\n" +
"\t}\n" +
"\treturn EFI_ERROR_CODE;\n" + "\treturn EFI_ERROR_CODE;\n" +
"}\n", getConfigValueConsumer.getComleteGetterBody()); "}\n", getConfigValueConsumer.getComleteGetterBody());
} }
@ -166,24 +167,27 @@ public class GetConfigValueConsumerTest {
"\t\t\treturn *(float*)hackEngineConfigurationPointer(known->value);\n" + "\t\t\treturn *(float*)hackEngineConfigurationPointer(known->value);\n" +
"\t\t}\n" + "\t\t}\n" +
"\t}\n" + "\t}\n" +
"\tif (strEqualCaseInsensitive(name, \"clt.config.map.sensor.hwChannel\"))\n" + "\tint hash = djb2lowerCase(name);\n" +
"\t\treturn config->clt.config.map.sensor.hwChannel;\n" + "\tswitch(hash) {\n" +
"\tif (strEqualCaseInsensitive(name, \"clt.adcChannel\"))\n" + "\t\tcase 581685574:\n" +
"\t\treturn config->clt.adcChannel;\n" + "\t\t\treturn config->clt.config.map.sensor.hwChannel;\n" +
"\tif (strEqualCaseInsensitive(name, \"issue_294_31\"))\n" + "\t\tcase -1144186889:\n" +
"\t\treturn config->issue_294_31;\n" + "\t\t\treturn config->clt.adcChannel;\n" +
"\tif (strEqualCaseInsensitive(name, \"baseFuel\"))\n" + "\t\tcase -1571463185:\n" +
"\t\treturn config->baseFuel;\n" + "\t\t\treturn config->issue_294_31;\n" +
"\tif (strEqualCaseInsensitive(name, \"speedToRpmRatio\"))\n" + "\t\tcase 727098956:\n" +
"\t\treturn config->speedToRpmRatio;\n" + "\t\t\treturn config->baseFuel;\n" +
"\tif (strEqualCaseInsensitive(name, \"afr_typet\"))\n" + "\t\tcase -685727673:\n" +
"\t\treturn config->afr_typet;\n" + "\t\t\treturn config->speedToRpmRatio;\n" +
"\tif (strEqualCaseInsensitive(name, \"vehicleSpeedKph\"))\n" + "\t\tcase 1694412179:\n" +
"\t\treturn config->vehicleSpeedKph;\n" + "\t\t\treturn config->afr_typet;\n" +
"\tif (strEqualCaseInsensitive(name, \"isForcedInduction\"))\n" + "\t\tcase -1925174695:\n" +
"\t\treturn config->isForcedInduction;\n" + "\t\t\treturn config->vehicleSpeedKph;\n" +
"\tif (strEqualCaseInsensitive(name, \"enableFan1WithAc\"))\n" + "\t\tcase -617915487:\n" +
"\t\treturn config->enableFan1WithAc;\n" + "\t\t\treturn config->isForcedInduction;\n" +
"\t\tcase -298185774:\n" +
"\t\t\treturn config->enableFan1WithAc;\n" +
"\t}\n" +
"\treturn EFI_ERROR_CODE;\n" + "\treturn EFI_ERROR_CODE;\n" +
"}\n", getConfigValueConsumer.getHeaderAndGetter()); "}\n", getConfigValueConsumer.getHeaderAndGetter());
} }