Skip to content

Commit 1c1268a

Browse files
xipeng-wang-skydioaaron-skydio
authored andcommitted
Turn off sort_by_offset for keys when get LCM message
This significantly speed up lcm message retrieval. topic: turn_off_sort_by_offset_when_get_lcm_message GitOrigin-RevId: 0248bf9fd83cffa273c20272e06ce5145094e3e2
1 parent 24eaf20 commit 1c1268a

File tree

5 files changed

+10
-9
lines changed

5 files changed

+10
-9
lines changed

symforce/opt/values.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,16 @@ index_entry_t Values<Scalar>::IndexEntryAt(const Key& key) const {
180180
}
181181

182182
template <typename Scalar>
183-
void Values<Scalar>::FillLcmType(LcmType* msg) const {
183+
void Values<Scalar>::FillLcmType(LcmType* msg, bool sort_keys) const {
184184
SYM_ASSERT(msg != nullptr);
185-
msg->index = CreateIndex(Keys());
185+
msg->index = CreateIndex(Keys(sort_keys));
186186
msg->data = data_;
187187
}
188188

189189
template <typename Scalar>
190-
typename Values<Scalar>::LcmType Values<Scalar>::GetLcmType() const {
190+
typename Values<Scalar>::LcmType Values<Scalar>::GetLcmType(bool sort_keys) const {
191191
LcmType msg;
192-
FillLcmType(&msg);
192+
FillLcmType(&msg, sort_keys);
193193
return msg;
194194
}
195195

symforce/opt/values.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ class Values {
239239
/**
240240
* Serialize to LCM.
241241
*/
242-
void FillLcmType(LcmType* msg) const;
243-
LcmType GetLcmType() const;
242+
void FillLcmType(LcmType* msg, bool sort_keys = false) const;
243+
LcmType GetLcmType(bool sort_keys = false) const;
244244

245245
/**
246246
* Unique ID of this Values instance, incremented whenever the structure of the Values is

symforce/pybind/cc_sym.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ class Values:
534534
"""
535535
Has zero keys.
536536
"""
537-
def get_lcm_type(self) -> values_t:
537+
def get_lcm_type(self, sort_keys: bool = False) -> values_t:
538538
"""
539539
Serialize to LCM.
540540
"""

symforce/pybind/cc_values.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,8 @@ void AddValuesWrapper(pybind11::module_ module) {
246246
index: Ordered list of keys to include (MUST be valid for both this and others Values)
247247
epsilon: Small constant to avoid singularities (do not use zero)
248248
)")
249-
.def("get_lcm_type", &sym::Valuesd::GetLcmType, "Serialize to LCM.")
249+
.def("get_lcm_type", &sym::Valuesd::GetLcmType, py::arg("sort_keys") = false,
250+
"Serialize to LCM.")
250251
.def("__repr__", [](const sym::Valuesd& values) { return fmt::format("{}", values); })
251252
.def(py::pickle(
252253
[](const sym::Valuesd& values) { // __getstate__

test/symforce_values_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ TEMPLATE_TEST_CASE("Test values", "[values]", double, float) {
109109
CHECK(v.template At<Scalar>(sym::Key('f', 2)) == Scalar(15.6));
110110

111111
// Pack to LCM
112-
const typename sym::Values<Scalar>::LcmType msg = v.GetLcmType();
112+
const typename sym::Values<Scalar>::LcmType msg = v.GetLcmType(true /*sort keys*/);
113113
CHECK(msg.index == index_2);
114114
CHECK(msg.data.size() == v.Data().size());
115115

0 commit comments

Comments
 (0)