This is a pretty interesting protocol, in that it doesn't fit some
assumptions I made when writing Gloss. All in all, though, I think
it's still easier to use Gloss than to write a custom parser.
Again, the above code hasn't been tested, so there may be some issues
with it. If you have any questions about how it works (or should
work), please feel free to ask.
Zach
(defcodec simple-tuple
(header (string-integer :ascii :delimiters ["="])
(fn [key]
(compile-frame
[key (string :ascii :delimiters [0x01])]))
(fn [[key value]]
key)))
As you correctly realized, there was no way to get the key from the
decoded value. Since the bytes have already been consumed, we need to
bake it into the header->body codec. Doing compile-frame for every
frame shouldn't be too bad, but you can always memoize the function to
get around that.
I've fixed the mistake in the gist I posted yesterday. Obviously,
there may still be others.
Zach
The reason that your decoding is so much faster is because the first
decode (outside the timed loop) is consuming all the bytes in the byte
buffer. All subsequent decodes are being passed an empty buffer, and
returning nil. To prevent this from happening, you need to duplicate
the buffer before decoding it. I've added in the necessary .duplicate
calls here: https://gist.github.com/755574.
With those changes and a few type-hints added in, your solution is
still 5x faster at encoding and 20x faster at decoding. If we make
the buffer that Gloss is decoding contiguous, the difference goes down
to 16x. Obviously this is still pretty slow, so I'm going to keep
looking at it.
Zach
Zach
Zach