Search code examples
gomqttamqppaho

Custom Message Headers: amqp vs. paho


Is the difference between the data structures used for custom message headers in mqttv5 paho and amqp due to the protocol, or simply an implementation decision?

Background: I noticed that the most recent paho library for mqttv5 replaced the type for Publish.PublishProperties.User from map[string]string with []UserProperty. I'm assuming the reason for the change is the prevalence of use cases that require multiple values sharing a single key, but wouldn't it be more efficient to utilize the amqp style customer message header? amqp uses amqp.Table where Table is map[string]interface{}, which seems to serve the same purpose (just use []string as your interface implementation).

Retrieving a slice of values that share a custom message header in amqp requires one line of code:

slice := message.Headers["key"]

Retrieving a slice of values that share a custom message header in paho requires a loop of string comparisons:

// GetAll returns a slice of all entries in the UserProperties
// that match key, or a nil slice if none were found.
func (u UserProperties) GetAll(key string) []string {
    var ret []string
    for _, v := range u {
        if v.Key == key {
            ret = append(ret, v.Value)
        }
    }

    return ret
}

Is there a reason for this choice of implementation?


Solution

  • As per the contributors, the performance of returning UserProperty in that format is much lower.

    See the github discussion here: https://github.com/eclipse/paho.golang/issues/47

    From that discussion, with the current implementation being the second performance run:

    With 10 k/v in the user properties and 5 being the same key
    
    pkg: github.com/eclipse/paho.golang/paho
    BenchmarkUserProperties-8         703861              1476 ns/op             784 B/op         15 allocs/op
    BenchmarkUserProperty-8          6933013               158 ns/op             320 B/op          1 allocs/op