Visitar URL original
Builder for Data.Fixed? · Issue #689 · haskell/bytestring · GitHub
Skip to content

Builder for Data.Fixed? #689

Open
Open

Description

@matthewbauer

I wonder if there is a fast ByteString Builder for numbers from Data.Fixed?

I've written one myself, but don't think it's quite as fast as it could be:

-- | Encode a 'Fixed' resolution number using ASCII digits.
fixedDec :: HasResolution a => Fixed a -> BB.Builder
fixedDec fa@(MkFixed a) | a < 0 = P.primFixed P.char8 '-' <> fixedDec (asTypeOf (MkFixed (negate a)) fa)
fixedDec fa@(MkFixed a) = integerDec i <> P.primFixed P.char8 '.' <> stimesMonoid zeros (P.primFixed P.char8 '0') <> integerDec d
  where
    (i, d) = a `quotRem` resolution fa
    zeros :: Integer = if d == 0 then 0 else ceiling (logBase 10 (fromIntegral (resolution fa) / fromIntegral d / 10) :: Double)

Calculating logBase 10 gets expensive, so it doesn't seem to perform well with lots of Fixed numbers.

Ideally if there was an efficient builder of fixed, it could be included in bytestring. Anyone have ideas of how to make it more efficient?

Activity

  1. matthewbauer commented on Aug 2, 2024

    @matthewbauer
    Author

    I was able to get some pretty good results (sub 1ms for 10,000 calls based on benchmarks) with this version:

    -- | Encode a 'Fixed' resolution number using ASCII digits.
    fixedDec :: HasResolution a => Fixed a -> Builder
    fixedDec fa@(MkFixed a) | a < 0 = P.primFixed P.char8 '-' <> fixedDec (asTypeOf (MkFixed (negate a)) fa)
    fixedDec fa@(MkFixed a) = integerDec i <> P.primFixed P.char8 '.' <> P.primMapListFixed P.char8 (List.genericReplicate zeros '0') <> integerDec d
      where
        !(i, d) = a `quotRem` resolution fa
        zeros :: Integer = if d == 0 then 0 else ceiling (logBase 10 (fromIntegral (resolution fa) / fromIntegral d / 10) :: Double)

    You can about half (like 500μs) that by approximating Fixed to "Word64" instead of Integer:

    -- | Encode a 'Fixed' resolution number using ASCII digits, approximated to Word64.
    fixedDecWord64 :: HasResolution a => Fixed a -> Builder
    fixedDecWord64 fa@(MkFixed a) | a < 0 = P.primFixed P.char8 '-' <> fixedDecWord64 (asTypeOf (MkFixed (negate a)) fa)
    fixedDecWord64 fa@(MkFixed a) = word64Dec i <> P.primFixed P.char8 '.' <> P.primMapListFixed P.char8 (List.replicate zeros '0') <> word64Dec d
      where
        !(i, d) = fromIntegral a `quotRem` fromIntegral (resolution fa)
        zeros :: Int = if d == 0 then 0 else ceiling (logBase 10 (fromIntegral (resolution fa) / fromIntegral d / 10) :: Double)

    I have a branch available here: master...matthewbauer:bytestring:fixedDec

  2. vdukhovni commented on Oct 25, 2026

    @vdukhovni
    Contributor

    I don't think it makes much sense for ByteString to bundle builders for general Data.Fixed types.
    A typical application likely uses a single or at most a handful of specific decimal precisions, and appropriate builders for just those are I think best left to the application.

    I'd also consider a suitable type class to allow specialised implementations:

    class HasResolution a => FixedBuilder a where
        buildFixed :: Fixed a -> B.Builder
    
    instance FixedBuilder E1 where
        buildFixed (MkFixed n) =
            s <> B.integerDec i <> B.char8 '.' <> B.intDec (fromIntegral f)
          where
            (s, m) | n >= 0 = (mempty, n)
                   | otherwise = (B.char8 '-', -n)
            (i, f) = m `quotRem` 10
    
    instance FixedBuilder E2 where
        buildFixed (MkFixed n) =
            s <> B.integerDec i <> B.char8 '.' <> z <> B.intDec (fromIntegral f)
          where
            (s, m) | n >= 0 = (mempty, n)
                   | otherwise = (B.char8 '-', -n)
            (i, f) = m `quotRem` 100
            z | f >= 10 = mempty
              | otherwise = B.char8 '0'
    
    instance FixedBuilder E3 where
        buildFixed (MkFixed n) =
            s <> B.integerDec i <> B.char8 '.' <> z <> B.intDec (fromIntegral f)
          where
            (s, m) | n >= 0 = (mempty, n)
                   | otherwise = (B.char8 '-', -n)
            (i, f) = m `quotRem` 1000
            z | f >= 100 = mempty
              | f >= 10 = B.char8 '0'
              | otherwise = B.char8 '0' <> B.char8 '0'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions