nomeata / haskell-leb128-cereal Goto Github PK
View Code? Open in Web Editor NEWLEB128 encoding for Haskell
License: MIT License
LEB128 encoding for Haskell
License: MIT License
Thus allowing to reuse the coding functionality of cereal by providing instances for Serialize
. E.g.,
-- | Wrapper that provides instances of Serialize
-- that implement LEB128 encoding.
newtype Leb128 n = Leb128 n
instance Serialize (Leb128 Word64) where ...
instance Serialize (Leb128 Natural) where ...
The current go definitions like this:
go :: Int -> Int -> G.Get Int
go shift w = do
byte <- G.getWord8 <|> fail "short encoding"
let !byteVal = fromIntegral (clearBit byte 7)
let !hasMore = testBit byte 7
let !val = w .|. (byteVal `unsafeShiftL` shift)
let !shift' = shift+7
if hasMore
then go shift' val
else return $! val
result in intermediate Int's being allocated on each iteration.
toLazyBytestring
preallocates a buffer of 4k bytes. This is highly execessive for this kind of use.
Using toLazyByteStringWith
allows giving the size of the inital buffer and avoid this overhead. In practice I imagine something like 32 bytes will do. Numbers far larger than this are better served by a different encoding scheme so leb128 should not be optimized for this case.
You could go further and just compute the required length ahead of doing the encoding which would allow us to limit allocation to a single (strict) bytestring as well.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.