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

Automatically unpack Tuple when sorting arrays #3539

Merged
merged 1 commit into from
Nov 16, 2016
Merged

Automatically unpack Tuple when sorting arrays #3539

merged 1 commit into from
Nov 16, 2016

Conversation

luislavena
Copy link
Contributor

Avoid capturing the block used for sorting, allowing automatic unpacking of Tuples.

Since the block is only required in the inner #quicksort! function, this change gives some flexibility to the user allowing them write more expressive code.

In the following example:

a = [{"c", 3}, {"a", 1}, {"b", 2}]
a.sort_by &.[1]
a.sort_by { |(x, y)| y }

Both sort_by calls produce the same result, one being more cryptic and the other being explicit about unpacking.

In comparison:

a.sort_by { |x, y| y }

Produces the same results more transparently to the developer writing the code and behaves similar to other methods that perform automatic unpack like #map or #each.

Performance-wise, this change does introduce a speedup on synthetic benchmarks:

      sort_by:   2.29M (± 1.03%)  1.10× slower
sort_by (new):   2.51M (± 0.99%)       fastest

Fixes #3419

Avoid capturing the block used for sorting, allowing automatic
unpacking of Tuples.

Since the block is only required in the inner `#quicksort!` function,
this change gives some flexibility to the user allowing them write
more expressive code.

In the following example:

    a = [{"c", 3}, {"a", 1}, {"b", 2}]
    a.sort_by &.[1]
    a.sort_by { |(x, y)| y }

Both `sort_by` calls produce the same result, one being more cryptic
and the other being explicit about unpacking.

In comparison:

    a.sort_by { |x, y| y }

Produces the same results more transparently to the developer
writing the code and behaves similar to other methods that perform
automatic unpack like `#map` or `#each`.

Performance-wise, this change does introduce a speedup on synthetic
benchmarks:

          sort_by:   2.29M (± 1.03%)  1.10× slower
    sort_by (new):   2.51M (± 0.99%)       fastest

Fixes #3419
@asterite
Copy link
Member

Nice, thank you! ❤️

This indeed creates less closures, I think.

@asterite asterite merged commit 7e62375 into crystal-lang:master Nov 16, 2016
@asterite asterite added this to the 0.20.0 milestone Nov 16, 2016
@luislavena luislavena deleted the auto-unpack-tuple-array-sort_by branch April 24, 2017 02:40
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.

None yet

2 participants