Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement stimes more efficiently #301

Merged
merged 24 commits into from
Oct 28, 2020
Merged

Implement stimes more efficiently #301

merged 24 commits into from
Oct 28, 2020

Conversation

elikoga
Copy link
Contributor

@elikoga elikoga commented Oct 9, 2020

As requested in #299

I didn't write any test cases yet

Copy link
Contributor

@Bodigrim Bodigrim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stimes for a negative argument should throw an error similar to stimesDefault.

Is strict stimes correct for n = 0?

Data/ByteString/Internal.hs Outdated Show resolved Hide resolved
@elikoga
Copy link
Contributor Author

elikoga commented Oct 10, 2020

Is strict stimes correct for n = 0?

I've just taken the liberty to throw an error on any non-negative input to stimes just like stimesDefault.

@Bodigrim
Copy link
Contributor

Sorry for confusion, I should have pointed you to stimesMonoid. It is desirable to handle n = 0 when we are dealing with a Monoid.

Data/ByteString/Internal.hs Outdated Show resolved Hide resolved
Data/ByteString/Lazy/Internal.hs Outdated Show resolved Hide resolved
@Bodigrim
Copy link
Contributor

Thanks, looks reasonable to me. Please go ahead with tests. It's enough to check that stimes and mtimesDefault are observably equivalent.

@elikoga
Copy link
Contributor Author

elikoga commented Oct 10, 2020

Any Idea on how to get to stimesDefault or stimesMonoid without reimplementing them?

@Bodigrim
Copy link
Contributor

@elikoga
Copy link
Contributor Author

elikoga commented Oct 10, 2020

mtimesDefault n x
  | n == 0    = mempty
  | otherwise = unwrapMonoid (stimes n (WrapMonoid x))

I'm afraid it's implemented using stimes which wouldn't help us at all

@elikoga
Copy link
Contributor Author

elikoga commented Oct 10, 2020

Oh just realized its using stimes of the wrapped monoid nvm

@elikoga
Copy link
Contributor Author

elikoga commented Oct 10, 2020

Doesn't look like the strict version is working

@elikoga
Copy link
Contributor Author

elikoga commented Oct 10, 2020

Turns out memcpy copies to the first argument, my mistake

@Bodigrim
Copy link
Contributor

Looks very nice, thanks! Please fix tests for GHC < 8.0 by adding semigroups to build-depends of the test suite.
https://travis-ci.org/github/haskell/bytestring/jobs/734551109#L836

@Bodigrim
Copy link
Contributor

Ah, that's me being stupid. Since bytestring library cannot depend on semigroups and prior to GHC 8.0 Semigroup was not in base, there is simply no instance Semigroup ByteString for GHC < 8.0. Could you please guard new test properties by CPP?

@elikoga
Copy link
Contributor Author

elikoga commented Oct 10, 2020

Done

@Bodigrim Bodigrim requested a review from sjakobi October 12, 2020 18:40
Data/ByteString/Internal.hs Outdated Show resolved Hide resolved
Data/ByteString/Internal.hs Outdated Show resolved Hide resolved
@Bodigrim
Copy link
Contributor

@elikoga any chance to address @sjakobi's review suggestions? I'd like to get it merged soon. I can put finishing touches myself, if you are busy.

@elikoga
Copy link
Contributor Author

elikoga commented Oct 24, 2020

@Bodigrim just did, sorry for the delay

Data/ByteString/Lazy/Internal.hs Outdated Show resolved Hide resolved
Data/ByteString/Internal.hs Show resolved Hide resolved
Copy link
Member

@sjakobi sjakobi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few more wibbles – nearly done.

Data/ByteString/Lazy/Internal.hs Outdated Show resolved Hide resolved
Data/ByteString/Internal.hs Outdated Show resolved Hide resolved
Data/ByteString/Internal.hs Outdated Show resolved Hide resolved
| n < 0 = error "stimes: non-negative multiplier expected"
| n == 0 = mempty
| len == 0 = mempty
| n == 1 = BS fp len
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does returning BS fp len instead of a variable bound to the BS pattern result in additional allocations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checked with ddump-simpl and couldn't see any differences outside of different identifiers.
ddump-core-stats also returns the same amount of Terms, Types and Coercions, so I think it's optimized away. Wouldn't say no to assigning it to a name though if you believe it would be more readable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assembly generated with ddump-asm also looks essentially the same, up to renaming.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for investigating! 👍

Copy link
Member

@sjakobi sjakobi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @elikoga! :)

@Bodigrim
Copy link
Contributor

@elikoga last hurdle before merge: could you please fix tests? Seems Properties.hs lacks {-# LANGUAGE CPP #-}.
https://travis-ci.org/github/haskell/bytestring/jobs/739428587#L638

@elikoga
Copy link
Contributor Author

elikoga commented Oct 28, 2020

>  head tests/Properties.hs
{-# LANGUAGE CPP, ScopedTypeVariables, BangPatterns #-}
--
-- Must have rules off, otherwise the rewrite rules will replace the rhs
-- with the lhs, and we only end up testing lhs == lhs
--

--
-- -fhpc interferes with rewrite rules firing.
--

I don't believe that Properties.hs has changed since c39c1e7 and I'm honestly quite confused what changes to make to fix it.
Do you have any idea?

@Bodigrim
Copy link
Contributor

@elikoga Properties.hs was changed in 7968977. Try to rebase your branch atop of master please.

@elikoga
Copy link
Contributor Author

elikoga commented Oct 28, 2020

Whoops my fault

@elikoga
Copy link
Contributor Author

elikoga commented Oct 28, 2020

Looks like it works; If accepted please add the hacktoberfest-accepted label.

@Bodigrim Bodigrim added this to the 0.11.1.0 milestone Oct 28, 2020
@Bodigrim Bodigrim linked an issue Oct 28, 2020 that may be closed by this pull request
@Bodigrim Bodigrim merged commit 1f97c4c into haskell:master Oct 28, 2020
@Bodigrim
Copy link
Contributor

Thanks, merged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Efficient implementation of stimes
3 participants