Skip to content

Inefficient code generated for length #97

Description

@nick8325

Here is a a function that checks if two unboxed vectors have the same length:

module Test where

import qualified Data.Vector.Unboxed as V

sameLength :: V.Vector Int -> V.Vector Int -> Bool
sameLength x y = V.length x == V.length y

I would expect sameLength to compile into code that just pattern matches on its arguments. Unfortunately, what I get is the following Core:

sameLength :: V.Vector Int -> V.Vector Int -> Bool
sameLength =
  \ (x_a2P6 :: V.Vector Int) (y_a2P7 :: V.Vector Int) ->
    case Test.$slength x_a2P6 of _ { GHC.Types.I# x1_a48j ->
    case Test.$slength y_a2P7 of _ { GHC.Types.I# y1_a48n ->
    GHC.Prim.tagToEnum# @ Bool (GHC.Prim.==# x1_a48j y1_a48n)
    }
    }

Test.$slength [InlPrag=[NEVER]] :: V.Vector Int -> Int
[GblId, Arity=1, Caf=NoCafRefs, Str=DmdType <S,1*U(A,U,A)>m]
Test.$slength = ...

For some reason, the $slength function, which computes the length of the vector, is marked with InlPrag=[NEVER]. Hence the sameLength function does lots of unnecessary boxing and unboxing.

In reality, this boxing and unboxing happens inside a tight loop where it causes a ~2x slowdown in my code.

Activity

  1. added a commit that references this issue on Feb 17, 2017
    a811a86
  2. added a commit that references this issue on Feb 18, 2017
    0cc7805
  3. added a commit that references this issue on Jun 20, 2020
  4. added a commit that references this issue on Jun 21, 2020
  5. added a commit that references this issue on Jan 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions