@hackage / derive-storable-plugin

GHC core plugin supporting the derive-storable package.

Latest0.2.3.7

About

Metadata

  • Last updated , by mkloczko
  • License MIT
  • Categories FFI
  • Maintained by: mateusz.p.kloczko@gmail.com

  • Lottery factor: 0

Links

Installation

Tested Compilers

  1. 9.6.1
  2. 9.4.2
  3. 9.2.2
  4. 9.0.2
  5. 8.10.7
  6. 8.8.1
  7. 8.6.5
  8. 8.4.2
  9. 8.2.2

Package Flags

Use the -f option with cabal commands to enable flags

    sumtypes (off by default)

    Use sumtypes within benchmark and tests.

Readme

Introduction

Hackage Build Status

The goal of derive-storable-plugin is to support the derive-storable package. It introduces optimisations to GStorable methods derived using GHC.Generics at core-to-core passes.

Usage

Just add a -fplugin=Foreign.Storable.Generic.Plugin flag and you're set. You might also want to pass a verbosity flag -vX, where X is either 0,1 or 2. By default the verbosity flag is set to -v1.

{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-}
{-# OPTIONS_GHC -fplugin=Foreign.Storable.Generic.Plugin #-}
{-# OPTIONS_GHC -fplugin-opt=Foreign.Storable.Generic.Plugin:-v1 #-} 
module Main where

import GHC.Generics

import Foreign.Storable.Generic
import Foreign.Ptr
import Foreign.Marshal.Alloc

data Point = Point {
    x :: Float,
    y :: Float
    } deriving (Show, Read, Generic, GStorable)

main = do
    let val = Point 0.0 10.0
    ptr <- malloc :: IO (Ptr Point)
    putStrLn "Created a ptr with value of"
    print =<< peek ptr
    poke ptr val
    putStrLn "And now the value of ptr is:"
    print =<< peek ptr

Benchmarks

The plugin was benchmarked using derive-storable-benchmark package. The benchark measures handwritten Storable instances, raw GStorable instances and optimised-by-plugin GStorable instances. The code was compiled with -O1 optimisation flag. Benchmarks