Hi,

I am just starting to using javanano (from 3.0.0-alpha-3), and was 
wondering if there was any reason that the clear() method is not defined 
(abstract) on MessageNano.

I was trying to minimise garbage collection by always re-using the same 
MessageNano subclass instance, using a ThreadLocal to hold the instance and 
then clearing it each time before it is used.  (Is this a reasonable 
pattern of usage?)
If clear is defined on MessageNano it makes it easier to implement a 
general mechanism.
e.g.

public abstract class SerDes<T, M extends MessageNano> {
    private final ThreadLocal<M> threadLocal;

    public SerDes(final Supplier<M> supplier) {
        threadLocal = ThreadLocal.withInitial(supplier);
    }

    public byte[] toBytes(final T value) throws IOException {
        final M proto = getMessage();
        populateMessage(proto, value);
        return MessageNano.toByteArray(proto);
    }

    public T fromBytes(final byte[] bytes) throws IOException {
        final M proto = MessageNano.mergeFrom(getMessage(), bytes);
        return fromMessage(proto);
    }

    private M getMessage() throws IOException {
        final M proto = threadLocal.get();
        proto.clear();                                             // <=== 
can't do this now
        return proto;
    }

    protected abstract void populateMessage(M proto, T fromValue) throws 
IOException;
    protected abstract T fromMessage(M message) throws IOException;
}

Thanks,
Ross

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to