Skip to content

Use Slice.literal for fast_float when supported, part 2#16261

Merged
straight-shoota merged 1 commit intocrystal-lang:masterfrom
HertzDevil:refactor/slice-literal-fast_float
Feb 5, 2026
Merged

Use Slice.literal for fast_float when supported, part 2#16261
straight-shoota merged 1 commit intocrystal-lang:masterfrom
HertzDevil:refactor/slice-literal-fast_float

Conversation

@HertzDevil
Copy link
Contributor

Follow-up to #15667.

LARGE_POWER_OF_5 = Slice[
1414648277510068013_u64, 9180637584431281687_u64, 4539964771860779200_u64,
10482974169319127550_u64, 198276706040285095_u64,
{% if compare_versions(Crystal::VERSION, "1.16.0") < 0 %}
Copy link
Member

Choose a reason for hiding this comment

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

thought: The value lists are identical between both types. We could reduce code duplication by making only the outer layer conditional:

{% begin %}
  {% if compare_versions(Crystal::VERSION, "1.16.0") < 0 %}
    [
  {% else %}
    Slice(UInt64).literal(
  {% end %}
  1_u64,
  # ...
  7450580596923828125_u64,
  {% if compare_versions(Crystal::VERSION, "1.16.0") < 0 %}
    ]
  {% else %}
    )
  {% end %}
{% end %}

The added overhead won't make this much appealing for very short slices. But it might be a good idea for the bigger ones? WDYT?

Copy link
Member

Choose a reason for hiding this comment

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

ping @HertzDevil any thoughts on this?
It shouldn't be a blocker, so I'd like to move on if you don't think it's worth it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Duplicating the values is unfortunate but the alternative impacts readability, so I'm not sure it's really better.

Aside: maybe a macro def could help if we supported them, though it would be very inefficient (lots of allocations & transformations) so I'm not sure it would be very useful, especially for large literals and static data that won't change.

macro def slice_literal(type, *values)
  {% if compare_versions(Crystal::VERSION, "1.16.0") < 0 %}
    Slice({{type}}.literal({{values.splat}})
  {% else %}
    [{{values.splat}}]
  {% end %}
end

LARGE_POWER_OF_5 = {{ slice_literal(UInt64,
  1414648277510068013_u64,
  9180637584431281687_u64,
  4539964771860779200_u64,
  10482974169319127550_u64,
  198276706040285095_u64,
) }}

@ysbaddaden ysbaddaden added this to the 1.20.0 milestone Feb 3, 2026
@straight-shoota straight-shoota merged commit 5a0b4a6 into crystal-lang:master Feb 5, 2026
42 checks passed
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.

3 participants