From 8e12a666543e50043419630d5e4ff00c7dd277e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dagfinn=20Ilmari=20Manns=C3=A5ker?= Date: Fri, 17 Nov 2017 23:59:30 +0000 Subject: [PATCH] Re-weaken timer references after sorting Perl 5.27.4 fixes the in-place sort optimisation (@a = sort @a) to strengthen references, like assignment should. However, we need to keep the references to timers weak, so callers can cancel them by dropping their reference. --- lib/AnyEvent/Loop.pm | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/AnyEvent/Loop.pm b/lib/AnyEvent/Loop.pm index fb9351a..f7145fd 100644 --- a/lib/AnyEvent/Loop.pm +++ b/lib/AnyEvent/Loop.pm @@ -173,6 +173,9 @@ BEGIN { $round = 0.001 if $round < 0.001; # 1ms is enough for us $round -= $round * 1e-2; # 0.1 => 0.099 eval "sub ROUNDUP() { $round }"; + + my $unweakens = $] >= 5.027004 ? 1 : 0; + eval "sub INPLACE_SORT_UNWEAKENS() { $unweakens }"; } _update_clock; @@ -203,6 +206,9 @@ sub one_event { if ($MNOW >= $need_sort) { $need_sort = 1e300; @timer = sort { $a->[0] <=> $b->[0] } @timer; + if (INPLACE_SORT_UNWEAKENS) { + weaken($_) for @timer; + } } # handle all pending timers -- 2.7.4