From 10380df8c78fae5d92c3f9cd1ef305453417ea17 Mon Sep 17 00:00:00 2001 From: Neven Sajko Date: Tue, 6 Oct 2026 18:07:24 +0200 Subject: [PATCH] disallow mutation of BinaryHeap fields I see no reason to allow mutating the fields. The implementation does not require it, and I think the fields are not a public interface. Making the fields `const` allows the Julia compiler/optimizer more freedom when reasoning about code that involves `BinaryHeap` values. NB: for the case when the subtype of `Ordering` is zero bytes wide, it would make more sense to just change `mutable struct` into `struct`. While the order is often of singleton type, that is not always the case. Thus this change is conservative, going for the straight improvement while avoiding a trade off. If necessary, it is possible to change to just `struct BinaryHeap` after this change is merged. Cross-reference issue #988. Corresponding PR to the BinaryHeaps package: * SciML/BinaryHeaps.jl#27 --- src/heaps/binary_heap.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/heaps/binary_heap.jl b/src/heaps/binary_heap.jl index d39b5f584..6ca070cc9 100644 --- a/src/heaps/binary_heap.jl +++ b/src/heaps/binary_heap.jl @@ -31,8 +31,8 @@ struct FasterReverse <: Base.Ordering end Base.lt(o::FasterReverse, a, b) = a > b mutable struct BinaryHeap{T, O <: Base.Ordering} <: AbstractHeap{T} - ordering::O - valtree::Vector{T} + const ordering::O + const valtree::Vector{T} function BinaryHeap{T}(ordering::Base.Ordering) where T new{T, typeof(ordering)}(ordering, Vector{T}())