1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2026-01-06 03:40:56 -08:00

Simplify message-unique-id etc.

* lisp/gnus/message.el (message-unique-id):
* lisp/net/sasl.el (sasl-unique-id-function):
Avoid unnecessary consing and reliance on internal timestamp
format by using (time-convert nil 'integer) which typically does
no consing, instead of using (current-time) and then ignoring
the subsecond parts of the generated list.
This commit is contained in:
Paul Eggert 2021-12-05 18:35:27 -08:00
parent 524c42fa0e
commit f4d7ca73e3
2 changed files with 26 additions and 21 deletions

View file

@ -5828,15 +5828,15 @@ In posting styles use `(\"Expires\" (make-expires-date 30))'."
;; You might for example insert a "." somewhere (not next to another dot ;; You might for example insert a "." somewhere (not next to another dot
;; or string boundary), or modify the "fsf" string. ;; or string boundary), or modify the "fsf" string.
(defun message-unique-id () (defun message-unique-id ()
;; Don't use microseconds from (current-time), they may be unsupported. ;; Don't use fractional seconds from timestamp; they may be unsupported.
;; Instead we use this randomly inited counter. ;; Instead we use this randomly inited counter.
(setq message-unique-id-char (setq message-unique-id-char
(% (1+ (or message-unique-id-char ;; 2^16 * 25 just fits into 4 digits i base 36.
(random (ash 1 20)))) (let ((base (* 25 25)))
;; (current-time) returns 16-bit ints, (if message-unique-id-char
;; and 2^16*25 just fits into 4 digits i base 36. (% (1+ message-unique-id-char) base)
(* 25 25))) (random base))))
(let ((tm (current-time))) (let ((tm (time-convert nil 'integer)))
(concat (concat
(if (or (eq system-type 'ms-dos) (if (or (eq system-type 'ms-dos)
;; message-number-base36 doesn't handle bigints. ;; message-number-base36 doesn't handle bigints.
@ -5846,10 +5846,12 @@ In posting styles use `(\"Expires\" (make-expires-date 30))'."
(aset user (match-beginning 0) ?_)) (aset user (match-beginning 0) ?_))
user) user)
(message-number-base36 (user-uid) -1)) (message-number-base36 (user-uid) -1))
(message-number-base36 (+ (car tm) (message-number-base36 (+ (ash tm -16)
(ash (% message-unique-id-char 25) 16)) 4) (ash (% message-unique-id-char 25) 16))
(message-number-base36 (+ (nth 1 tm) 4)
(ash (/ message-unique-id-char 25) 16)) 4) (message-number-base36 (+ (logand tm #xffff)
(ash (/ message-unique-id-char 25) 16))
4)
;; Append a given name, because while the generated ID is unique ;; Append a given name, because while the generated ID is unique
;; to this newsreader, other newsreaders might otherwise generate ;; to this newsreader, other newsreaders might otherwise generate
;; the same ID via another algorithm. ;; the same ID via another algorithm.

View file

@ -174,21 +174,24 @@ It contain at least 64 bits of entropy."
;; stolen (and renamed) from message.el ;; stolen (and renamed) from message.el
(defun sasl-unique-id-function () (defun sasl-unique-id-function ()
;; Don't use microseconds from (current-time), they may be unsupported. ;; Don't use fractional seconds from timestamp; they may be unsupported.
;; Instead we use this randomly inited counter. ;; Instead we use this randomly inited counter.
(setq sasl-unique-id-char (setq sasl-unique-id-char
(% (1+ (or sasl-unique-id-char (logand (random) (1- (ash 1 20))))) ;; 2^16 * 25 just fits into 4 digits i base 36.
;; (current-time) returns 16-bit ints, (let ((base (* 25 25)))
;; and 2^16*25 just fits into 4 digits i base 36. (if sasl-unique-id-char
(* 25 25))) (% (1+ sasl-unique-id-char) base)
(let ((tm (current-time))) (random base))))
(let ((tm (time-convert nil 'integer)))
(concat (concat
(sasl-unique-id-number-base36 (sasl-unique-id-number-base36
(+ (car tm) (+ (ash tm -16)
(ash (% sasl-unique-id-char 25) 16)) 4) (ash (% sasl-unique-id-char 25) 16))
4)
(sasl-unique-id-number-base36 (sasl-unique-id-number-base36
(+ (nth 1 tm) (+ (logand tm #xffff)
(ash (/ sasl-unique-id-char 25) 16)) 4)))) (ash (/ sasl-unique-id-char 25) 16))
4))))
(defun sasl-unique-id-number-base36 (num len) (defun sasl-unique-id-number-base36 (num len)
(if (if (< len 0) (if (if (< len 0)