improved efficiency of printing out struct queue messages
diff --git a/aos/build/queues/print_field.rb b/aos/build/queues/print_field.rb
index 84cf1f4..2e70e15 100644
--- a/aos/build/queues/print_field.rb
+++ b/aos/build/queues/print_field.rb
@@ -1,8 +1,8 @@
require File.dirname(__FILE__) + '/load.rb'
-TypeNames = [8, 16, 32, 64].collect do |size|
- ["uint#{size}_t", "int#{size}_t"]
-end.flatten + ['bool', 'float', 'char', 'double', '::aos::time::Time']
+# TODO(brians): Special-case Time too and float/double if we can find a good way to do it.
+GenericTypeNames = ['float', 'double', 'char', '::aos::time::Time']
+IntegerSizes = [8, 16, 32, 64]
WriteIffChanged.open(ARGV[0]) do |output|
output.puts <<END
@@ -16,13 +16,14 @@
#include "aos/common/byteorder.h"
#include "aos/common/time.h"
+#include "aos/common/print_field_helpers.h"
namespace aos {
bool PrintField(char *output, size_t *output_bytes, const void *input,
size_t *input_bytes, uint32_t type) {
switch (type) {
-#{TypeNames.collect do |name|
+#{GenericTypeNames.collect do |name|
message_element = Target::MessageElement.new(name, 'value')
statement = MessageElementStmt.new(name, 'value')
message_element.size = statement.size
@@ -40,11 +41,45 @@
#{print_args[0]});
if (ret < 0) return false;
if (static_cast<unsigned int>(ret) >= *output_bytes) return false;
- *output_bytes -= ret + 1;
+ *output_bytes -= ret;
return true;
}
END2
end.join('')}
+#{IntegerSizes.collect do |size|
+ [true, false].collect do |signed|
+ size_bytes = size / 8
+ name = "#{signed ? '' : 'u'}int#{size}_t"
+ message_element = Target::MessageElement.new(name, 'value')
+ message_element.size = size_bytes
+ next <<END2
+ case #{message_element.getTypeID()}:
+ {
+ if (*input_bytes < #{size_bytes}) return false;
+ *input_bytes -= #{size_bytes};
+ #{name} value;
+ to_host(static_cast<const char *>(input), &value);
+ return PrintInteger<#{name}>(output, value, output_bytes);
+ }
+END2
+ end
+end.flatten.join('')}
+#{
+ message_element = Target::MessageElement.new('bool', 'value')
+ message_element.size = 1
+ r = <<END2
+ case #{message_element.getTypeID()}:
+ {
+ if (*input_bytes < 1) return false;
+ if (*output_bytes < 1) return false;
+ *input_bytes -= 1;
+ bool value = static_cast<const char *>(input)[0];
+ *output_bytes += 1;
+ *output = value ? 'T' : 'f';
+ return true;
+ }
+END2
+}
default:
return false;
}
diff --git a/aos/common/print_field_helpers.h b/aos/common/print_field_helpers.h
new file mode 100644
index 0000000..48acdf6
--- /dev/null
+++ b/aos/common/print_field_helpers.h
@@ -0,0 +1,41 @@
+#ifndef AOS_COMMON_PRINT_FIELD_HELPERS_H_
+#define AOS_COMMON_PRINT_FIELD_HELPERS_H_
+
+#include <stdint.h>
+
+#include <type_traits>
+
+namespace aos {
+
+template<typename T>
+inline bool PrintInteger(char *buf, T val, size_t *output) {
+ static const bool is_signed = ::std::is_signed<T>::value;
+
+ size_t len = 0;
+ if (is_signed && val <= 0) {
+ while (*output > len && (val != 0 || len == 0)) {
+ buf[len++] = '0' - (val % 10);
+ val /= 10;
+ }
+ buf[len++] = '-';
+ } else {
+ while (*output > len && (val != 0 || len == 0)) {
+ buf[len++] = '0' + (val % 10);
+ val /= 10;
+ }
+ }
+ // If we have enough space.
+ if (*output >= len) {
+ for (size_t i = 0; i < (len >> 1); i++) {
+ std::swap(buf[len - 1 - i], buf[i]);
+ }
+ *output -= len;
+ return true;
+ } else {
+ return false;
+ }
+}
+
+} // namespace aos
+
+#endif // AOS_COMMON_PRINT_FIELD_HELPERS_H_
diff --git a/aos/common/queue_types.cc b/aos/common/queue_types.cc
index 93cf135..8c22d90 100644
--- a/aos/common/queue_types.cc
+++ b/aos/common/queue_types.cc
@@ -148,12 +148,12 @@
type_cache::Get(type.fields[i]->type))) {
return false;
}
+ // Ignore the trailing '\0' that the subcall put on.
+ output -= 1;
}
- // Update the input and output pointers, ignoring the trailing '\0' that the
- // subcall put on.
- output += output_bytes_before - *output_bytes - 1;
- *output_bytes += 1;
+ // Update the input and output pointers.
+ output += output_bytes_before - *output_bytes;
input =
static_cast<const char *>(input) + input_bytes_before - *input_bytes;
}
@@ -208,10 +208,8 @@
return false;
}
CHECK_EQ(0u, input_bytes);
- // Update the output pointer, ignoring the trailing '\0' that
- // the subcall put on.
- output += output_bytes_before - *output_bytes - 1;
- *output_bytes += 1;
+ // Update the output pointer.
+ output += output_bytes_before - *output_bytes;
}
if (*output_bytes < 1) return false;
diff --git a/aos/common/queue_types.h b/aos/common/queue_types.h
index b95c946..eb0527e 100644
--- a/aos/common/queue_types.h
+++ b/aos/common/queue_types.h
@@ -102,6 +102,7 @@
//
// Prints the value from 1 primitive message field into output.
// The implementation of this is generated by the ruby code.
+// Does not include a trailing '\0' in what it subtracts from *output_bytes.
bool PrintField(char *output, size_t *output_bytes, const void *input,
size_t *input_bytes, uint32_t type)
__attribute__((warn_unused_result));