diff options
| author | Paul Eggert | 2021-12-05 18:35:27 -0800 |
|---|---|---|
| committer | Paul Eggert | 2021-12-05 23:24:09 -0800 |
| commit | f4d7ca73e3ab975fd920a2b0f2d1a7fdb5276d99 (patch) | |
| tree | 122ab47a5a2289b703625ecc4b16622d226042e1 | |
| parent | 524c42fa0eb9e4bf02d39e4d04353a354a84cebc (diff) | |
| download | emacs-f4d7ca73e3ab975fd920a2b0f2d1a7fdb5276d99.tar.gz emacs-f4d7ca73e3ab975fd920a2b0f2d1a7fdb5276d99.zip | |
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.
| -rw-r--r-- | lisp/gnus/message.el | 24 | ||||
| -rw-r--r-- | lisp/net/sasl.el | 23 |
2 files changed, 26 insertions, 21 deletions
diff --git a/lisp/gnus/message.el b/lisp/gnus/message.el index 562bc64f6fb..8e7983a33c3 100644 --- a/lisp/gnus/message.el +++ b/lisp/gnus/message.el | |||
| @@ -5828,15 +5828,15 @@ In posting styles use `(\"Expires\" (make-expires-date 30))'." | |||
| 5828 | ;; You might for example insert a "." somewhere (not next to another dot | 5828 | ;; You might for example insert a "." somewhere (not next to another dot |
| 5829 | ;; or string boundary), or modify the "fsf" string. | 5829 | ;; or string boundary), or modify the "fsf" string. |
| 5830 | (defun message-unique-id () | 5830 | (defun message-unique-id () |
| 5831 | ;; Don't use microseconds from (current-time), they may be unsupported. | 5831 | ;; Don't use fractional seconds from timestamp; they may be unsupported. |
| 5832 | ;; Instead we use this randomly inited counter. | 5832 | ;; Instead we use this randomly inited counter. |
| 5833 | (setq message-unique-id-char | 5833 | (setq message-unique-id-char |
| 5834 | (% (1+ (or message-unique-id-char | 5834 | ;; 2^16 * 25 just fits into 4 digits i base 36. |
| 5835 | (random (ash 1 20)))) | 5835 | (let ((base (* 25 25))) |
| 5836 | ;; (current-time) returns 16-bit ints, | 5836 | (if message-unique-id-char |
| 5837 | ;; and 2^16*25 just fits into 4 digits i base 36. | 5837 | (% (1+ message-unique-id-char) base) |
| 5838 | (* 25 25))) | 5838 | (random base)))) |
| 5839 | (let ((tm (current-time))) | 5839 | (let ((tm (time-convert nil 'integer))) |
| 5840 | (concat | 5840 | (concat |
| 5841 | (if (or (eq system-type 'ms-dos) | 5841 | (if (or (eq system-type 'ms-dos) |
| 5842 | ;; message-number-base36 doesn't handle bigints. | 5842 | ;; message-number-base36 doesn't handle bigints. |
| @@ -5846,10 +5846,12 @@ In posting styles use `(\"Expires\" (make-expires-date 30))'." | |||
| 5846 | (aset user (match-beginning 0) ?_)) | 5846 | (aset user (match-beginning 0) ?_)) |
| 5847 | user) | 5847 | user) |
| 5848 | (message-number-base36 (user-uid) -1)) | 5848 | (message-number-base36 (user-uid) -1)) |
| 5849 | (message-number-base36 (+ (car tm) | 5849 | (message-number-base36 (+ (ash tm -16) |
| 5850 | (ash (% message-unique-id-char 25) 16)) 4) | 5850 | (ash (% message-unique-id-char 25) 16)) |
| 5851 | (message-number-base36 (+ (nth 1 tm) | 5851 | 4) |
| 5852 | (ash (/ message-unique-id-char 25) 16)) 4) | 5852 | (message-number-base36 (+ (logand tm #xffff) |
| 5853 | (ash (/ message-unique-id-char 25) 16)) | ||
| 5854 | 4) | ||
| 5853 | ;; Append a given name, because while the generated ID is unique | 5855 | ;; Append a given name, because while the generated ID is unique |
| 5854 | ;; to this newsreader, other newsreaders might otherwise generate | 5856 | ;; to this newsreader, other newsreaders might otherwise generate |
| 5855 | ;; the same ID via another algorithm. | 5857 | ;; the same ID via another algorithm. |
diff --git a/lisp/net/sasl.el b/lisp/net/sasl.el index b7f814f7237..0a3ecf9f534 100644 --- a/lisp/net/sasl.el +++ b/lisp/net/sasl.el | |||
| @@ -174,21 +174,24 @@ It contain at least 64 bits of entropy." | |||
| 174 | 174 | ||
| 175 | ;; stolen (and renamed) from message.el | 175 | ;; stolen (and renamed) from message.el |
| 176 | (defun sasl-unique-id-function () | 176 | (defun sasl-unique-id-function () |
| 177 | ;; Don't use microseconds from (current-time), they may be unsupported. | 177 | ;; Don't use fractional seconds from timestamp; they may be unsupported. |
| 178 | ;; Instead we use this randomly inited counter. | 178 | ;; Instead we use this randomly inited counter. |
| 179 | (setq sasl-unique-id-char | 179 | (setq sasl-unique-id-char |
| 180 | (% (1+ (or sasl-unique-id-char (logand (random) (1- (ash 1 20))))) | 180 | ;; 2^16 * 25 just fits into 4 digits i base 36. |
| 181 | ;; (current-time) returns 16-bit ints, | 181 | (let ((base (* 25 25))) |
| 182 | ;; and 2^16*25 just fits into 4 digits i base 36. | 182 | (if sasl-unique-id-char |
| 183 | (* 25 25))) | 183 | (% (1+ sasl-unique-id-char) base) |
| 184 | (let ((tm (current-time))) | 184 | (random base)))) |
| 185 | (let ((tm (time-convert nil 'integer))) | ||
| 185 | (concat | 186 | (concat |
| 186 | (sasl-unique-id-number-base36 | 187 | (sasl-unique-id-number-base36 |
| 187 | (+ (car tm) | 188 | (+ (ash tm -16) |
| 188 | (ash (% sasl-unique-id-char 25) 16)) 4) | 189 | (ash (% sasl-unique-id-char 25) 16)) |
| 190 | 4) | ||
| 189 | (sasl-unique-id-number-base36 | 191 | (sasl-unique-id-number-base36 |
| 190 | (+ (nth 1 tm) | 192 | (+ (logand tm #xffff) |
| 191 | (ash (/ sasl-unique-id-char 25) 16)) 4)))) | 193 | (ash (/ sasl-unique-id-char 25) 16)) |
| 194 | 4)))) | ||
| 192 | 195 | ||
| 193 | (defun sasl-unique-id-number-base36 (num len) | 196 | (defun sasl-unique-id-number-base36 (num len) |
| 194 | (if (if (< len 0) | 197 | (if (if (< len 0) |