Builder for Data.Fixed?

Open
#689 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
haskell
Domain
data

Research direction

Start with the Data.Fixed type and the fixedDec example in the issue, then inspect the existing bytestring Builder APIs for numeric encoders. Compare the current logBase-based zero calculation with possible integer-based approaches and establish benchmarks for repeated Fixed values. Done means an efficient Fixed builder is defined and its performance is demonstrated against the supplied implementation.

Written by the indexing model from the issue text.

Description

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?

Dominant language
Haskell
Stars
302
Forks
146
Avg merge
7d 21h
Merged PRs (30d)
1

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from haskell/bytestring

All issues in haskell/bytestring

Similar issues

More Haskell issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.