Skip to content

Divide By Zero Panics in Unchecked Arithmetic Kernels #2647

@tustvold

Description

@tustvold

Which part is this question about

In Rust division and modulus is always checked - you can see that here. We currently provide checked and unchecked division kernels, and a single modulus kernel which is always checked.

The result is that the checked kernels will return an error on divide by zero, whilst the unchecked kernel will panic. This feels a little counter-intuitive, I would expect the unchecked kernel to silently do "something defined but not necessarily meaningful".

Describe your question

The comparison kernels handle the null mask separately from the values data, and this massively improves performance. I would like to apply the same optimisation to the unchecked arithmetic kernels, but cannot do this whilst dividing or modulus by 0 results in a panic, as the null values may have arbitrary values which could trigger this.

I would like to propose the unchecked variant of these division kernels set the value to be 0 on division by 0 (or some other sentinel value). As we're checking regardless there is no performance impact of doing this, and it allows blindly processing the values without first consulting the bitmask.

An alternative option would be to only have checked variants, however, this would preclude handling the null mask separately for the non-scalar variants of the kernels. I think this would be a shame.

One further alternative I did consider was to only consult the bitmask on divide by zero, however, the reality is most null slots will have a value of 0, and consequently this cost of this conditional checking is likely to undermine any benefits from the optimisation.

Additional context

Thoughts @alamb @viirya @Dandandan @jhorstmann ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions