Skip to content

Commit

Permalink
feat: improve sample create perf
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-roland committed Oct 28, 2024
1 parent 6b88f91 commit 77de095
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 30 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_sample_t _z_sample_create(_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);
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);

#endif /* ZENOH_PICO_SAMPLE_NETAPI_H */
36 changes: 17 additions & 19 deletions src/net/sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,26 @@ _z_sample_t _z_sample_duplicate(const _z_sample_t *src) {
}

#if Z_FEATURE_SUBSCRIPTION == 1
_z_sample_t _z_sample_create(_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) {
_z_sample_t s = _z_sample_null();
s.keyexpr = _z_keyexpr_steal(key);
s.kind = kind;
if (_z_timestamp_check(timestamp)) {
s.timestamp = _z_timestamp_duplicate(timestamp);
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) {
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();
}
s.qos = qos;
s.reliability = reliability;
_z_bytes_copy(&s.payload, payload);
_z_bytes_copy(&s.attachment, attachment);
if (encoding != NULL) {
_z_encoding_move(&s.encoding, encoding);
}
return s;
return _z_bytes_copy(&s->payload, payload);
}
#else
_z_sample_t _z_sample_create(_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) {
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) {
_ZP_UNUSED(key);
_ZP_UNUSED(payload);
_ZP_UNUSED(timestamp);
Expand Down
19 changes: 11 additions & 8 deletions src/session/subscription.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,18 @@ z_result_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t *keyexp
if (sub_nb == 0) {
return _Z_RES_OK;
}
// Build the sample
_z_sample_t sample = _z_sample_create(&key, payload, timestamp, encoding, kind, qos, attachment, reliability);
// Parse subscription list
_z_subscription_rc_list_t *xs = subs;
_Z_DEBUG("Triggering %ju subs", (uintmax_t)sub_nb);
while (xs != NULL) {
_z_subscription_rc_t *sub = _z_subscription_rc_list_head(xs);
_Z_RC_IN_VAL(sub)->_callback(&sample, _Z_RC_IN_VAL(sub)->_arg);
xs = _z_subscription_rc_list_tail(xs);
// Build the sample
_z_sample_t sample;
ret = _z_sample_create(&sample, &key, payload, timestamp, encoding, kind, qos, attachment, reliability);
if (ret == _Z_RES_OK) {
// Parse subscription list
_z_subscription_rc_list_t *xs = subs;
while (xs != NULL) {
_z_subscription_rc_t *sub = _z_subscription_rc_list_head(xs);
_Z_RC_IN_VAL(sub)->_callback(&sample, _Z_RC_IN_VAL(sub)->_arg);
xs = _z_subscription_rc_list_tail(xs);
}
}
// Clean up
_z_sample_clear(&sample);
Expand Down

0 comments on commit 77de095

Please sign in to comment.