Skip to content

Commit

Permalink
feat: move z_bytes instead of copy
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-roland committed Oct 29, 2024
1 parent 0ea825e commit e1ef3ed
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 39 deletions.
6 changes: 3 additions & 3 deletions include/zenoh-pico/net/sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
8 changes: 4 additions & 4 deletions include/zenoh-pico/session/subscription.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/collections/bytes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
21 changes: 8 additions & 13 deletions src/net/sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
32 changes: 14 additions & 18 deletions src/session/subscription.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit e1ef3ed

Please sign in to comment.