Haskell in Depth by Vitaly Bragilevsky
Author:Vitaly Bragilevsky [Bragilevsky, Vitaly]
Language: eng
Format: epub, pdf
Publisher: Manning Publications Co.
Published: 0101-01-01T00:00:00+00:00
9.2.2 Defining data types with unboxed values
GHC provides several tools to ask for unboxing. Letâs overview them in this section.
The UNPACK pragma and corresponding optimizations
As we discussed in the previous section, storing values like Rectangle 3 5 requires many closures for the value itself and for each of its fields. If our fields are small, it may be helpful to put them directly into the payload of the constructorâs closure. This is done with the UNPACK pragma, which can be used as follows:
data Shape = Rectangle {-# UNPACK #-} !Int {-# UNPACK #-} !Int | Circle {-# UNPACK #-} !Int
Note that it is required to have strictness flags on all the unpacked fields. The UNPACK pragma has an effect only when optimizations are turned on (compilation with the -O family of flags). Note also that this pragma has no effect on polymorphic values.
Depending on what we are doing with the fields after matching over the constructor pattern, it may be necessary to rebox them, so this optimization does not always lead to reduced memory usage. Anyway, GHC tries hard to avoid reboxing.
GHC options on unboxing In addition to the UNPACK pragma, GHC provides the -funbox-strict-fields flag, which has the effect of adding {-# UNPACK #-} to every strict constructor field. Note that using it could lead to performance drawbacks if we copy a data structure extensively because all the unboxed fields have to be copied. Remember that any change in a data structure leads to copying in Haskell. This is not a problem for small, strict fields if their size is no bigger than a pointerâthe pointer has to be copied anyway. Thatâs why GHC enables the -funbox-small-strict-fields flag by default.
Unboxed tuples and unboxed sums
We are not allowed to define primitive unboxed types, but we are able to define unboxed tuples and unboxed sums using the UnboxedTuples and UnboxedSums GHC extensions. The idea behind these extensions is to make possible declaring complex types without the need to create closures for storing their values. Both unboxed tuples and unboxed sums donât exist at run time, but their components do exist and are stored in memory, depending on their own types.
Example: Unboxing tuples and sums
ch09/unboxed.hs
unboxed
We can reduce memory allocation thanks to unboxing.
Letâs define a function that computes the sum and product of two numbers and returns them as an unboxed tuple, as shown next:
{-# LANGUAGE UnboxedTuples #-} sum_prod :: Num a => a -> a -> (# a, a #) sum_prod a b = (# a + b, a*b #)
To use this function, we pattern match on its result right after calling it as follows:
test_sum_prod = case sum_prod 5 6 of (# s, p #) -> print (s + p)
In this case, we avoid creating closures for tuples in memory. Doing so may be beneficial if we call this function many times and use its results right away.
The function returning an unboxed tuple may be recursive, as shown here:
{-# LANGUAGE UnboxedTuples #-} fib_next :: Int -> (# Integer, Integer #) -> (#
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7915)
Grails in Action by Glen Smith Peter Ledbrook(7881)
Azure Containers Explained by Wesley Haakman & Richard Hooper(7178)
Configuring Windows Server Hybrid Advanced Services Exam Ref AZ-801 by Chris Gill(7175)
Running Windows Containers on AWS by Marcio Morales(6718)
Kotlin in Action by Dmitry Jemerov(5298)
Microsoft 365 Identity and Services Exam Guide MS-100 by Aaron Guilmette(5257)
Microsoft Cybersecurity Architect Exam Ref SC-100 by Dwayne Natwick(4953)
Combating Crime on the Dark Web by Nearchos Nearchou(4841)
The Ruby Workshop by Akshat Paul Peter Philips Dániel Szabó and Cheyne Wallace(4525)
Management Strategies for the Cloud Revolution: How Cloud Computing Is Transforming Business and Why You Can't Afford to Be Left Behind by Charles Babcock(4493)
The Age of Surveillance Capitalism by Shoshana Zuboff(4115)
Python for Security and Networking - Third Edition by José Manuel Ortega(4085)
Learn Wireshark by Lisa Bock(3875)
The Ultimate Docker Container Book by Schenker Gabriel N.;(3743)
Learn Windows PowerShell in a Month of Lunches by Don Jones(3571)
Blockchain Basics by Daniel Drescher(3428)
Mastering Azure Security by Mustafa Toroman and Tom Janetscheck(3424)
Mastering Python for Networking and Security by José Manuel Ortega(3419)
