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

unsafeTextWithoutNewlines might be slower than necessary #231

Open
sjakobi opened this issue Jul 19, 2022 · 1 comment
Open

unsafeTextWithoutNewlines might be slower than necessary #231

sjakobi opened this issue Jul 19, 2022 · 1 comment

Comments

@sjakobi
Copy link
Collaborator

sjakobi commented Jul 19, 2022

-- | @(unsafeTextWithoutNewlines s)@ contains the literal string @s@.
--
-- The string must not contain any newline characters, since this is an
-- invariant of the 'Text' constructor.
unsafeTextWithoutNewlines :: Text -> Doc ann
unsafeTextWithoutNewlines text = case T.uncons text of
Nothing -> Empty
Just (t,ext)
| T.null ext -> Char t
| otherwise -> Text (T.length text) text

It seems that this function would allocate a new Text value in order to perform the T.null check on it. GHC may be able to optimize this allocation away, but it seems fragile.

@sjakobi
Copy link
Collaborator Author

sjakobi commented Jul 19, 2022

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

No branches or pull requests

1 participant