Skip to content

Commit 68257e8

Browse files
TomasRikerrtoy
authored andcommitted
Optimize bigfloat memory usage by sharing headers
Bigfloats now share their header (CAR), like other expressions already do via EQTEST and SIMPIND. New headers are created only when precision changes. This reduces memory usage and slightly improves bigfloat computation speed. Bigfloats could already share their CDR (mantissa and exponent) in certain cases. For example, computing bfloat(1)^0 twice results in bigfloats with the same CDR object (EQ). Thus, destructive modification of bigfloats was already problematic before this change, countering concerns that this might introduce risks. Richard Fateman commented that destructively modifying bigfloat headers is not something he would have done in the original code, and that he doubts anyone would have added such code.
1 parent f486cbf commit 68257e8

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

share/numeric/decfp-core.lisp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,19 @@ is (sum+1/10^50=1.0L0) ; should be true
268268
(scale-float mantissa e))))
269269
))
270270

271+
(defvar *decbfloat-header* nil
272+
"Current header ('BIGFLOAT 'SIMP $FPPREC 'DECIMAL) for new decimal bigfloats")
273+
274+
(defvar *decbfloat-header-prec* nil
275+
"Precision of current bigfloat header")
276+
277+
(defun decbcons (s)
278+
(unless (eql $fpprec *decbfloat-header-prec*)
279+
;; Precision was changed, make a new header.
280+
(setq *decbfloat-header* `(bigfloat simp ,$fpprec decimal)
281+
*decbfloat-header-prec* $fpprec))
282+
(cons *decbfloat-header* s))
283+
271284
(defun decbcons (s)
272285
`((bigfloat simp ,$fpprec decimal) . ,(decimalfptrim s)))
273286

src/float.lisp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,8 +712,18 @@
712712
;; always be a positive power of 2, this number will not always be in lowest
713713
;; terms.
714714

715+
(defvar *bfloat-header* nil
716+
"Current header ('BIGFLOAT 'SIMP FPPREC) for new bigfloats")
717+
718+
(defvar *bfloat-header-prec* nil
719+
"Precision of current bigfloat header")
720+
715721
(defun bcons (s)
716-
`((bigfloat simp ,fpprec) . ,s))
722+
(unless (eql fpprec *bfloat-header-prec*)
723+
;; Precision was changed, make a new header.
724+
(setq *bfloat-header* `(bigfloat simp ,fpprec)
725+
*bfloat-header-prec* fpprec))
726+
(cons *bfloat-header* s))
717727

718728
(defmfun ($bfloat :properties ((evfun t))) (x)
719729
(let (y)

0 commit comments

Comments
 (0)