[Haskell-cafe] Linking in Large ByteStrings

Svein Ove Aas svein.ove at aas.no
Fri Jan 1 11:49:50 EST 2010


On Fri, Jan 1, 2010 at 4:09 PM, Tom Hawkins <tomahawkins at gmail.com> wrote:
> I have a large tarball I want to link into an executable as a
> ByteString.  What is the best way to do this?  I can convert the
> tarball into a haskell file, but I'm afraid ghc would take a long time
> to compile it.  Is there any way to link constant data directly with
> ghc?  If not, what's the most efficient way to code large ByteStrings
> for fast compilation?
>
In the limit, you can convert it to an assembly file.

Something like this, though I've done very little checking indeed of
the syntax. Consider this to be pseudocode.

foo.s
====

.global bytestring, bytestring_end
.label bytestring
.db 0x0c 0xdf 0xwhatever
.label bytestring_end

foo.hs
===
import Foreign
import Data.ByteString.Internal

foreign import ptr bytestring :: Ptr Word8
foreign import ptr bytestring_end :: Ptr Word8

yourString :: ByteString
yourString = unsafePerformIO $ do
  fptr <- newForeignPtr_ bytestring
  return $ fromForeignPtr (fptr, bytestring_end `minusPtr` bytestring,
0) -- ^ If I got the foreignPtr parameter order right


Unfortunately Data.ByteString.Internal, though still exported, is no
longer haddocked; this makes it hard to check the parameters. You
should go look up the 6.10.1 version's documentation, which is still
correct.

-- 
Svein Ove Aas


More information about the Haskell-Cafe mailing list