emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Minor optimization in json.c


From: Eli Zaretskii
Subject: Minor optimization in json.c
Date: Sat, 22 Sep 2018 17:10:03 +0300

Currently, json.c has this FIXME:

  /* FIXME: This should be possible without creating an intermediate
     string object.  */
  Lisp_Object string
    = json_make_string (buffer_and_size->buffer, buffer_and_size->size);
  insert1 (string);

IIUC the issue, the change below should fix this.  All the tests in
json-tests.el pass after the change, but maybe you had failures in
other scenarios?  If not, I think we should make this change.

diff --git a/src/json.c b/src/json.c
index 17cc096..20280d8 100644
--- a/src/json.c
+++ b/src/json.c
@@ -630,11 +630,29 @@ static Lisp_Object
 json_insert (void *data)
 {
   struct json_buffer_and_size *buffer_and_size = data;
-  /* FIXME: This should be possible without creating an intermediate
-     string object.  */
-  Lisp_Object string
-    = json_make_string (buffer_and_size->buffer, buffer_and_size->size);
-  insert1 (string);
+  struct coding_system coding;
+  ptrdiff_t bytes = buffer_and_size->size, chars = bytes, opoint;
+
+  memset (&coding, 0, sizeof (coding));
+  /* JSON strings are UTF-8 strings.  */
+  setup_coding_system (Qutf_8_unix, &coding);
+  coding.source = buffer_and_size->buffer;
+  coding.mode |= CODING_MODE_LAST_BLOCK;
+  /* JSON strings are unibyte strings.  */
+  coding.src_chars = coding.src_bytes = bytes;
+
+  /* Call before-change hooks.  */
+  prepare_to_modify_buffer (PT, PT, NULL);
+  decode_coding_object (&coding, Qnil, 0, 0, chars, bytes, Fcurrent_buffer ());
+  /* Update buffer's point due to insertion of the string.  */
+  SET_BUF_PT_BOTH (current_buffer,
+                  PT + coding.produced_char, PT_BYTE + coding.produced);
+
+  /* Call after-change hooks.  */
+  opoint = PT - coding.produced_char;
+  signal_after_change (opoint, 0, coding.produced_char);
+  update_compositions (opoint, PT, CHECK_BORDER);
+
   return Qnil;
 }
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]