From 1b150117fd9ccb7ff8adc012b74d7d95e5219b94 Mon Sep 17 00:00:00 2001 From: Joe Darcy Date: Mon, 23 Oct 2023 22:08:12 +0000 Subject: [PATCH] 8318476: Add resource consumption note to BigInteger and BigDecimal Reviewed-by: alanb, bpb --- .../share/classes/java/math/BigDecimal.java | 23 ++++++++++- .../share/classes/java/math/BigInteger.java | 38 ++++++++++++++++++- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/java.base/share/classes/java/math/BigDecimal.java b/src/java.base/share/classes/java/math/BigDecimal.java index 094c48bc02f..dd34ad24571 100644 --- a/src/java.base/share/classes/java/math/BigDecimal.java +++ b/src/java.base/share/classes/java/math/BigDecimal.java @@ -279,7 +279,7 @@ * operations indicated by {@linkplain RoundingMode rounding modes} * are a proper superset of the IEEE 754 rounding-direction * attributes. - + * *

{@code BigDecimal} arithmetic will most resemble IEEE 754 * decimal arithmetic if a {@code MathContext} corresponding to an * IEEE 754 decimal format, such as {@linkplain MathContext#DECIMAL64 @@ -292,6 +292,27 @@ * such as dividing by zero, throw an {@code ArithmeticException} in * {@code BigDecimal} arithmetic. * + *

Algorithmic Complexity

+ * + * Operations on {@code BigDecimal} values have a range of algorithmic + * complexities; in general, those complexities are a function of both + * the size of the unscaled value as well as the size of the + * scale. For example, an {@linkplain BigDecimal#multiply(BigDecimal) + * exact multiply} of two {@code BigDecimal} values is subject to the + * same {@linkplain BigInteger##algorithmicComplexity complexity + * constraints} as {@code BigInteger} multiply of the unscaled + * values. In contrast, a {@code BigDecimal} value with a compact + * representation like {@code new BigDecimal(1E-1000000000)} has a + * {@link toPlainString} result with over one billion characters. + * + *

Operations may also allocate and compute on intermediate + * results, potentially those allocations may be as large as in + * proportion to the running time of the algorithm. + * + *

Users of {@code BigDecimal} concerned with bounding the running + * time or space of operations can screen out {@code BigDecimal} + * values with unscaled values or scales above a chosen magnitude. + * * @see BigInteger * @see MathContext * @see RoundingMode diff --git a/src/java.base/share/classes/java/math/BigInteger.java b/src/java.base/share/classes/java/math/BigInteger.java index d678a0f76a6..3b751c85c55 100644 --- a/src/java.base/share/classes/java/math/BigInteger.java +++ b/src/java.base/share/classes/java/math/BigInteger.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -118,6 +118,42 @@ * the full supported positive range of {@code BigInteger}. * The range must be at least 1 to 2500000000. * + * @apiNote + * As {@code BigInteger} values are + * arbitrary precision integers, the algorithmic complexity of the + * methods of this class varies and may be superlinear in the size of + * the input. For example, a method like {@link intValue()} would be + * expected to run in O(1), that is constant time, since with + * the current internal representation only a fixed-size component of + * the {@code BigInteger} needs to be accessed to perform the + * conversion to {@code int}. In contrast, a method like {@link not()} + * would be expected to run in O(n) time where n + * is the size of the {@code BigInteger} in bits, that is, to run in + * time proportional to the size of the input. For multiplying two + * {@code BigInteger} values of size n, a naive multiplication + * algorithm would run in time O(n2) and + * theoretical results indicate a multiplication algorithm for numbers + * using this category of representation must run in at least + * O(n log n). Common multiplication + * algorithms between the bounds of the naive and theoretical cases + * include the Karatsuba multiplication + * (O(n1.585)) and 3-way Toom-Cook + * multiplication (O(n1.465)). + * + *

A particular implementation of {@link multiply(BigInteger) + * multiply} is free to switch between different algorithms for + * different inputs, such as to improve actual running time to produce + * the product by using simpler algorithms for smaller inputs even if + * the simpler algorithm has a larger asymptotic complexity. + * + *

Operations may also allocate and compute on intermediate + * results, potentially those allocations may be as large as in + * proportion to the running time of the algorithm. + * + *

Users of {@code BigInteger} concerned with bounding the running + * time or space of operations can screen out {@code BigInteger} + * values above a chosen magnitude. + * * @implNote * In the reference implementation, BigInteger constructors and * operations throw {@code ArithmeticException} when the result is out