Skip to content

Conversation

@alexbiehl
Copy link
Contributor

@alexbiehl alexbiehl commented Jul 30, 2025

This PR changes how we generate the code for Hamlet templates.

Currently we generate

             (asWidgetT . toWidget)
               ((preEscapedText . txt-2.1.1-d07d1290:Data.Text.Internal.pack)
                  " class="")

Note how we apply Data.Text.pack to the string literal. It works but we can be a little more direct at this point.

With this patch we are now generating leaner code that doesn't have to run pack during runtime and that with inlining can statically allocate the bytestring value (and possibly the Markup value):

       (asWidgetT . toWidget)
         (Text.Blaze.Internal.unsafeByteString
            (bytestring-0.12.2.0-5b16:Data.ByteString.Internal.Type.unsafePackLenLiteral
               51 "\"><p class=\"text-sm/6 font-semibold text-gray-900\">"#))

@alexbiehl alexbiehl changed the title Use unsafeByteString to instead of preEscapedText for ContentRaw, if … Use unsafeByteString to instead of preEscapedText for ContentRaw Jul 30, 2025
@alexbiehl alexbiehl force-pushed the alex/unsafe-bytestring branch from 8d7e344 to 692170d Compare July 30, 2025 17:27
@alexbiehl alexbiehl changed the title Use unsafeByteString to instead of preEscapedText for ContentRaw Use unsafeByteString instead of preEscapedText for ContentRaw Jul 30, 2025
@alexbiehl
Copy link
Contributor Author

alexbiehl commented Oct 5, 2025

Do you feel hesitant to go via some unsafeByteString? I am happy to make it use the lift instance of text values instead. Either way this is a minor but clear win.

Copy link
Collaborator

@parsonsmatt parsonsmatt left a comment

Choose a reason for hiding this comment

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

This looks good to me - would you mind posting the before generated code too, just so we can see the difference?

Also please make a changelog note

Thanks!

@alexbiehl alexbiehl force-pushed the alex/unsafe-bytestring branch from 692170d to ee3bd3d Compare October 7, 2025 08:04
@parsonsmatt
Copy link
Collaborator

Awesome, thank you!

I think usually pack gets inlined pretty well with string literals, but I do expect this form to require less compile-time work from the inliner, so it's definitely a performance win all around

@parsonsmatt parsonsmatt merged commit 1abd742 into yesodweb:master Oct 7, 2025
9 checks passed
@parsonsmatt
Copy link
Collaborator

2.1.7 uploaded!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants