Skip to content

Commit 19efb25

Browse files
author
Ken Raeburn
committed
Use #N# syntax for repeated symbols in dumped.elc.
Parsing symbol names involves processing for possible multibyte characters and comparisons against other symbol-name strings in the obarray. The #N# syntax is simpler, uses an automatically resized hash table keyed by integers, and is in most cases shorter, so reading can be a little faster. When doing this we have to avoid the special "," syntax because we would wind up printing "#1=,foo" which reads back as setting #1# to ,foo when we really wanted to set #1# to just the comma symbol. * src/print.c (syms_of_print): Define new Lisp variable print-symbols-as-references. (PRINT_CIRCLE_CANDIDATE_P): If it's set, accept interned symbols. (print_preprocess): Update comment. (print_object): When printing "," or related symbols with special syntax, don't use print_object on the special symbol itself. * lisp/loadup.el: Bind print-symbols-as-references to t while creating the dumped.elc file.
1 parent 1184a17 commit 19efb25

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed

lisp/loadup.el

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ lost after dumping")))
566566
(print-level nil)
567567
(print-length nil)
568568
(print-escape-newlines t)
569+
(print-symbols-as-references t)
569570
(standard-output (current-buffer)))
570571
(print '(setq purify-flag nil))
571572
(print '(get-buffer-create "*Messages*"))

src/print.c

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,16 +1131,19 @@ print (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
11311131
print_object (obj, printcharfun, escapeflag);
11321132
}
11331133

1134-
#define PRINT_CIRCLE_CANDIDATE_P(obj) \
1135-
(STRINGP (obj) || CONSP (obj) \
1136-
|| (VECTORLIKEP (obj) \
1137-
&& (VECTORP (obj) || COMPILEDP (obj) \
1138-
|| CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj) \
1139-
|| HASH_TABLE_P (obj) || FONTP (obj) \
1140-
|| RECORDP (obj))) \
1141-
|| (! NILP (Vprint_gensym) \
1142-
&& SYMBOLP (obj) \
1143-
&& !SYMBOL_INTERNED_P (obj)))
1134+
#define PRINT_CIRCLE_CANDIDATE_P(obj) \
1135+
(STRINGP (obj) || CONSP (obj) \
1136+
|| (VECTORLIKEP (obj) \
1137+
&& (VECTORP (obj) || COMPILEDP (obj) \
1138+
|| CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj) \
1139+
|| HASH_TABLE_P (obj) || FONTP (obj) \
1140+
|| RECORDP (obj))) \
1141+
|| (SYMBOLP (obj) \
1142+
&& !SYMBOL_INTERNED_P (obj) \
1143+
&& ! NILP (Vprint_gensym)) \
1144+
|| (SYMBOLP (obj) \
1145+
&& SYMBOL_INTERNED_P (obj) \
1146+
&& ! NILP (Vprint_symbols_as_references)))
11441147

11451148
/* Construct Vprint_number_table according to the structure of OBJ.
11461149
OBJ itself and all its elements will be added to Vprint_number_table
@@ -1181,8 +1184,9 @@ print_preprocess (Lisp_Object obj)
11811184
if (!HASH_TABLE_P (Vprint_number_table))
11821185
Vprint_number_table = CALLN (Fmake_hash_table, QCtest, Qeq);
11831186

1184-
/* In case print-circle is nil and print-gensym is t,
1185-
add OBJ to Vprint_number_table only when OBJ is a symbol. */
1187+
/* In case print-circle is nil and print-gensym or
1188+
print-symbols-as-references is t, add OBJ to Vprint_number_table only
1189+
when OBJ is a symbol. */
11861190
if (! NILP (Vprint_circle) || SYMBOLP (obj))
11871191
{
11881192
Lisp_Object num = Fgethash (obj, Vprint_number_table, Qnil);
@@ -2013,7 +2017,20 @@ print_object (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
20132017
|| EQ (XCAR (obj), Qcomma_at)
20142018
|| EQ (XCAR (obj), Qcomma_dot)))
20152019
{
2016-
print_object (XCAR (obj), printcharfun, false);
2020+
/* If print-symbols-as-references is enabled, symbols may
2021+
print with "#N=" or "#N#" form. When we print a cons
2022+
cell with parens and separated elements, that's fine, but
2023+
for comma symbols we depend on the reader to generate the
2024+
cons cell from the special syntax. The Lisp reader will
2025+
treat "#1=,#2=foo" as setting reference 1 to ",foo", not
2026+
to ",", so we can't use print_object to print out the
2027+
comma symbols without breaking the ability to read the
2028+
result back properly. */
2029+
printchar (',', printcharfun);
2030+
if (EQ (XCAR (obj), Qcomma_at))
2031+
printchar ('@', printcharfun);
2032+
else if (EQ (XCAR (obj), Qcomma_dot))
2033+
printchar ('.', printcharfun);
20172034
new_backquote_output--;
20182035
print_object (XCAR (XCDR (obj)), printcharfun, escapeflag);
20192036
new_backquote_output++;
@@ -2422,6 +2439,15 @@ the value is different from what is guessed in the current charset
24222439
priorities. */);
24232440
Vprint_charset_text_property = Qdefault;
24242441

2442+
DEFVAR_LISP ("print-symbols-as-references", Vprint_symbols_as_references,
2443+
doc: /* Non-nil means print interned symbols using #N= and #N# syntax.
2444+
If nil, symbols are printed normally.
2445+
2446+
Setting this true makes the output harder for a human to read, but may
2447+
parse more efficiently as input to the Lisp reader if some symbols appear
2448+
in the output many times. */);
2449+
Vprint_symbols_as_references = Qnil;
2450+
24252451
/* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
24262452
staticpro (&Vprin1_to_string_buffer);
24272453

0 commit comments

Comments
 (0)