From e1ef3ed4c08d0afb2861d51ac9fd20fe323e5732 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Tue, 29 Oct 2024 16:32:36 +0100 Subject: [PATCH] feat: move z_bytes instead of copy --- include/zenoh-pico/net/sample.h | 6 ++--- include/zenoh-pico/session/subscription.h | 8 +++--- src/collections/bytes.c | 2 +- src/net/sample.c | 21 ++++++--------- src/session/subscription.c | 32 ++++++++++------------- 5 files changed, 30 insertions(+), 39 deletions(-) diff --git a/include/zenoh-pico/net/sample.h b/include/zenoh-pico/net/sample.h index fb0adb858..ab42e6985 100644 --- a/include/zenoh-pico/net/sample.h +++ b/include/zenoh-pico/net/sample.h @@ -59,8 +59,8 @@ void _z_sample_free(_z_sample_t **sample); z_result_t _z_sample_copy(_z_sample_t *dst, const _z_sample_t *src); _z_sample_t _z_sample_duplicate(const _z_sample_t *src); -z_result_t _z_sample_create(_z_sample_t *s, _z_keyexpr_t *key, const _z_bytes_t *payload, - const _z_timestamp_t *timestamp, _z_encoding_t *encoding, const z_sample_kind_t kind, - const _z_qos_t qos, const _z_bytes_t *attachment, z_reliability_t reliability); +void _z_sample_create(_z_sample_t *s, _z_keyexpr_t *key, _z_bytes_t *payload, const _z_timestamp_t *timestamp, + _z_encoding_t *encoding, const z_sample_kind_t kind, const _z_qos_t qos, _z_bytes_t *attachment, + z_reliability_t reliability); #endif /* ZENOH_PICO_SAMPLE_NETAPI_H */ diff --git a/include/zenoh-pico/session/subscription.h b/include/zenoh-pico/session/subscription.h index f28d95e96..9fa385860 100644 --- a/include/zenoh-pico/session/subscription.h +++ b/include/zenoh-pico/session/subscription.h @@ -19,16 +19,16 @@ #include "zenoh-pico/net/session.h" /*------------------ Subscription ------------------*/ -void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t *keyexpr, const _z_bytes_t *payload, +void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t *keyexpr, _z_bytes_t *payload, _z_encoding_t *encoding, const _z_n_qos_t qos, const _z_timestamp_t *timestamp, - const _z_bytes_t *attachment, z_reliability_t reliability); + _z_bytes_t *attachment, z_reliability_t reliability); #if Z_FEATURE_SUBSCRIPTION == 1 _z_subscription_rc_t *_z_get_subscription_by_id(_z_session_t *zn, uint8_t is_local, const _z_zint_t id); _z_subscription_rc_t *_z_register_subscription(_z_session_t *zn, uint8_t is_local, _z_subscription_t *sub); -z_result_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t *keyexpr, const _z_bytes_t *payload, +z_result_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t *keyexpr, _z_bytes_t *payload, _z_encoding_t *encoding, const _z_zint_t kind, const _z_timestamp_t *timestamp, - const _z_n_qos_t qos, const _z_bytes_t *attachment, z_reliability_t reliability); + const _z_n_qos_t qos, _z_bytes_t *attachment, z_reliability_t reliability); void _z_unregister_subscription(_z_session_t *zn, uint8_t is_local, _z_subscription_rc_t *sub); void _z_flush_subscriptions(_z_session_t *zn); #endif diff --git a/src/collections/bytes.c b/src/collections/bytes.c index e4328cd40..6cb7fa3fa 100644 --- a/src/collections/bytes.c +++ b/src/collections/bytes.c @@ -159,7 +159,7 @@ _z_slice_t _z_bytes_try_get_contiguous(const _z_bytes_t *bs) { } void _z_bytes_move(_z_bytes_t *dst, _z_bytes_t *src) { - dst->_slices = src->_slices; + *dst = *src; *src = _z_bytes_null(); } diff --git a/src/net/sample.c b/src/net/sample.c index 1f0b62464..d158398c6 100644 --- a/src/net/sample.c +++ b/src/net/sample.c @@ -58,26 +58,22 @@ _z_sample_t _z_sample_duplicate(const _z_sample_t *src) { } #if Z_FEATURE_SUBSCRIPTION == 1 -z_result_t _z_sample_create(_z_sample_t *s, _z_keyexpr_t *key, const _z_bytes_t *payload, - const _z_timestamp_t *timestamp, _z_encoding_t *encoding, const z_sample_kind_t kind, - const _z_qos_t qos, const _z_bytes_t *attachment, z_reliability_t reliability) { +void _z_sample_create(_z_sample_t *s, _z_keyexpr_t *key, _z_bytes_t *payload, const _z_timestamp_t *timestamp, + _z_encoding_t *encoding, const z_sample_kind_t kind, const _z_qos_t qos, _z_bytes_t *attachment, + z_reliability_t reliability) { s->kind = kind; s->qos = qos; s->reliability = reliability; s->keyexpr = _z_keyexpr_steal(key); s->timestamp = _z_timestamp_check(timestamp) ? _z_timestamp_duplicate(timestamp) : _z_timestamp_null(); _z_encoding_move(&s->encoding, encoding); - if (!_z_bytes_is_empty(attachment)) { - _Z_RETURN_IF_ERR(_z_bytes_copy(&s->attachment, attachment)); - } else { - s->attachment = _z_bytes_null(); - } - return _z_bytes_copy(&s->payload, payload); + _z_bytes_move(&s->attachment, attachment); + _z_bytes_move(&s->payload, payload); } #else -z_result_t _z_sample_create(_z_sample_t *s, _z_keyexpr_t *key, const _z_bytes_t *payload, - const _z_timestamp_t *timestamp, _z_encoding_t *encoding, const z_sample_kind_t kind, - const _z_qos_t qos, const _z_bytes_t *attachment, z_reliability_t reliability) { +void _z_sample_create(_z_sample_t *s, _z_keyexpr_t *key, _z_bytes_t *payload, const _z_timestamp_t *timestamp, + _z_encoding_t *encoding, const z_sample_kind_t kind, const _z_qos_t qos, _z_bytes_t *attachment, + z_reliability_t reliability) { _ZP_UNUSED(key); _ZP_UNUSED(payload); _ZP_UNUSED(timestamp); @@ -87,6 +83,5 @@ z_result_t _z_sample_create(_z_sample_t *s, _z_keyexpr_t *key, const _z_bytes_t _ZP_UNUSED(attachment); _ZP_UNUSED(reliability); *s = _z_sample_null(); - return _Z_RES_OK; } #endif diff --git a/src/session/subscription.c b/src/session/subscription.c index 495b1d939..e24c58b41 100644 --- a/src/session/subscription.c +++ b/src/session/subscription.c @@ -216,17 +216,17 @@ _z_subscription_rc_t *_z_register_subscription(_z_session_t *zn, uint8_t is_loca return ret; } -void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t *keyexpr, const _z_bytes_t *payload, +void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t *keyexpr, _z_bytes_t *payload, _z_encoding_t *encoding, const _z_n_qos_t qos, const _z_timestamp_t *timestamp, - const _z_bytes_t *attachment, z_reliability_t reliability) { + _z_bytes_t *attachment, z_reliability_t reliability) { z_result_t ret = _z_trigger_subscriptions(zn, keyexpr, payload, encoding, Z_SAMPLE_KIND_PUT, timestamp, qos, attachment, reliability); (void)ret; } -z_result_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t *keyexpr, const _z_bytes_t *payload, +z_result_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t *keyexpr, _z_bytes_t *payload, _z_encoding_t *encoding, const _z_zint_t kind, const _z_timestamp_t *timestamp, - const _z_n_qos_t qos, const _z_bytes_t *attachment, z_reliability_t reliability) { + const _z_n_qos_t qos, _z_bytes_t *attachment, z_reliability_t reliability) { _z_sample_t sample; _z_keyexpr_t key; z_result_t ret = _Z_RES_OK; @@ -242,13 +242,11 @@ z_result_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t *keyexp _Z_DEBUG("Triggering %ju subs", (uintmax_t)sub_cache.info_nb); // Build the sample key = _z_keyexpr_alias(sub_cache.ke_out); - ret = _z_sample_create(&sample, &key, payload, timestamp, encoding, kind, qos, attachment, reliability); - if (ret == _Z_RES_OK) { - // Parse subscription infos - for (size_t i = 0; i < sub_cache.info_nb; i++) { - _z_subscription_infos_t *sub_info = &sub_cache.infos[i]; - sub_info->callback(&sample, sub_info->arg); - } + _z_sample_create(&sample, &key, payload, timestamp, encoding, kind, qos, attachment, reliability); + // Parse subscription infos + for (size_t i = 0; i < sub_cache.info_nb; i++) { + _z_subscription_infos_t *sub_info = &sub_cache.infos[i]; + sub_info->callback(&sample, sub_info->arg); } // Clean up _z_sample_clear(&sample); @@ -275,13 +273,11 @@ z_result_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t *keyexp } _Z_DEBUG("Triggering %ju subs", (uintmax_t)sub_nb); // Build the sample - ret = _z_sample_create(&sample, &key, payload, timestamp, encoding, kind, qos, attachment, reliability); - if (ret == _Z_RES_OK) { - // Parse subscription infos svec - for (size_t i = 0; i < sub_nb; i++) { - _z_subscription_infos_t *sub_info = _z_subscription_infos_svec_get(&subs, i); - sub_info->callback(&sample, sub_info->arg); - } + _z_sample_create(&sample, &key, payload, timestamp, encoding, kind, qos, attachment, reliability); + // Parse subscription infos svec + for (size_t i = 0; i < sub_nb; i++) { + _z_subscription_infos_t *sub_info = _z_subscription_infos_svec_get(&subs, i); + sub_info->callback(&sample, sub_info->arg); } // Clean up _z_sample_clear(&sample);