Patchwork debugdeltachain: avoid division by zero when a chain is empty

login
register
mail settings
Submitter Paul Morelle
Date June 29, 2018, 4:17 p.m.
Message ID <9b9cb7abec13ed745c14.1530289053@belenos.localdomain>
Download mbox | patch
Permalink /patch/32502/
State New
Headers show

Comments

Paul Morelle - June 29, 2018, 4:17 p.m.
# HG changeset patch
# User Paul Morelle <paul.morelle@octobus.net>
# Date 1529597997 -7200
#      Thu Jun 21 18:19:57 2018 +0200
# Node ID 9b9cb7abec13ed745c14c3a1357ee2c2dd55c4b5
# Parent  5d88fd1bc2af0af02129f0ad2b267d778349d95a
# EXP-Topic debugdeltachain-divbyzero
# Available At https://bitbucket.org/octobus/mercurial-devel/
#              hg pull https://bitbucket.org/octobus/mercurial-devel/ -r 9b9cb7abec13
debugdeltachain: avoid division by zero when a chain is empty

The two ratios chainratio and extraratio are computed using dividers
that may be zero when the file is empty.
As the denominators are integers, the limit of the ratio "just before zero" is
the numerator value itself.
If the numerator itself is zero, the ratio value is still meaningful: in both
cases, a "good" value is a low ratio, and a size of zero is the optimal case.
Yuya Nishihara - June 30, 2018, 1:16 a.m.
On Fri, 29 Jun 2018 18:17:33 +0200, Paul Morelle wrote:
> # HG changeset patch
> # User Paul Morelle <paul.morelle@octobus.net>
> # Date 1529597997 -7200
> #      Thu Jun 21 18:19:57 2018 +0200
> # Node ID 9b9cb7abec13ed745c14c3a1357ee2c2dd55c4b5
> # Parent  5d88fd1bc2af0af02129f0ad2b267d778349d95a
> # EXP-Topic debugdeltachain-divbyzero
> # Available At https://bitbucket.org/octobus/mercurial-devel/
> #              hg pull https://bitbucket.org/octobus/mercurial-devel/ -r 9b9cb7abec13
> debugdeltachain: avoid division by zero when a chain is empty
> 
> The two ratios chainratio and extraratio are computed using dividers
> that may be zero when the file is empty.
> As the denominators are integers, the limit of the ratio "just before zero" is
> the numerator value itself.
> If the numerator itself is zero, the ratio value is still meaningful: in both
> cases, a "good" value is a low ratio, and a size of zero is the optimal case.
> 
> diff -r 5d88fd1bc2af -r 9b9cb7abec13 mercurial/debugcommands.py
> --- a/mercurial/debugcommands.py	Sat Jun 16 23:26:40 2018 +0900
> +++ b/mercurial/debugcommands.py	Thu Jun 21 18:19:57 2018 +0200
> @@ -678,8 +678,15 @@
>          except IndexError:
>              prevrev = -1
>  
> -        chainratio = float(chainsize) / float(uncomp)
> -        extraratio = float(extradist) / float(chainsize)
> +        if uncomp != 0:
> +            chainratio = float(chainsize) / float(uncomp)
> +        else:
> +            chainratio = chainsize
> +
> +        if chainsize != 0:
> +            extraratio = float(extradist) / float(chainsize)
> +        else:
> +            extraratio = extradist

Seems fine though I don't think the result is 100% correct. Can you add a
test?

Patch

diff -r 5d88fd1bc2af -r 9b9cb7abec13 mercurial/debugcommands.py
--- a/mercurial/debugcommands.py	Sat Jun 16 23:26:40 2018 +0900
+++ b/mercurial/debugcommands.py	Thu Jun 21 18:19:57 2018 +0200
@@ -678,8 +678,15 @@ 
         except IndexError:
             prevrev = -1
 
-        chainratio = float(chainsize) / float(uncomp)
-        extraratio = float(extradist) / float(chainsize)
+        if uncomp != 0:
+            chainratio = float(chainsize) / float(uncomp)
+        else:
+            chainratio = chainsize
+
+        if chainsize != 0:
+            extraratio = float(extradist) / float(chainsize)
+        else:
+            extraratio = extradist
 
         fm.startitem()
         fm.write('rev chainid chainlen prevrev deltatype compsize '