zenoh icon indicating copy to clipboard operation
zenoh copied to clipboard

`session.declare_keyexpr` doesn't optimize key transmission

Open nicksay opened this issue 4 months ago • 1 comments

Describe the bug

It appears that keys are not optimized if declared via session.declare_keyexpr (unlike session.declare_publisher).

To reproduce

  1. Run RUST_LOG=trace cargo run --example z_sub -- --mode router

  2. Run cargo run --example z_put -- --mode client --payload A

    a. Client calls session.put(&key_expr, payload).await.unwrap();
    Router receives TransportMessage { body: Frame(Frame { payload: [7d, 00, 19, 64, 65, 6d, 6f, 2f, 65, 78, 61, 6d, 70, 6c, 65, 2f, 7a, 65, 6e, 6f, 68, 2d, 72, 73, 2d, 70, 75, 74, 01, 01, 41] }) }

    b. Client calls let publisher = session.declare_publisher(&key_expr).await.unwrap(); publisher.put(payload).await.unwrap();
    Router receives TransportMessage { body: Frame(Frame { payload: [5d, 01, 01, 01, 41] }) }

    c. Client calls let declared = session.declare_keyexpr(key_expr).await.unwrap(); session.put(&declared, payload).await.unwrap();
    Router receives TransportMessage { body: Frame(Frame { payload: [7d, 00, 19, 64, 65, 6d, 6f, 2f, 65, 78, 61, 6d, 70, 6c, 65, 2f, 7a, 65, 6e, 6f, 68, 2d, 72, 73, 2d, 70, 75, 74, 01, 01, 41] }) }

System info

  • Platform: macOS 15.5
  • Zenoh Version: 1.5.0

nicksay avatar Aug 04 '25 05:08 nicksay

Hello @nicksay,

There seems to be a misunderstanding regarding the use of declare_keyexpr and how it affects a put and publisher operations.

declare_keyexpr optimizes the key-expression used by your put operation between the session layer and routing layer. Routing layer to network layer optimizations for a put operation are not performed based on the declare_keyexpr, but based on whatever key_expression is declared by the matching subscriber, writing on the wire the minimum information required to express the put's key-expression with regards to the matching key-expr declared by the subscriber.

You can see that wire expression used by the publisher and put operation are exactly the same if you enable TRACE logs on the z_pub and z_put examples used to produce this issue.

Finally, in general it is not recommended to compare the sizes of Frame messages between two different Zenoh applications when using the default transport config, as these can contain multiple messages that are batched together by Zenoh depending on the message flows of each application.

oteffahi avatar Aug 07 '25 12:08 oteffahi