unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Minor optimization in json.c
@ 2018-09-22 14:10 Eli Zaretskii
  2018-09-23  9:06 ` Eli Zaretskii
  0 siblings, 1 reply; 6+ messages in thread
From: Eli Zaretskii @ 2018-09-22 14:10 UTC (permalink / raw)
  To: Philipp Stephani; +Cc: emacs-devel

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;
 }
 



^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2018-10-13 15:28 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-09-22 14:10 Minor optimization in json.c Eli Zaretskii
2018-09-23  9:06 ` Eli Zaretskii
2018-09-23 13:06   ` Eli Zaretskii
2018-10-03 19:57     ` Philipp Stephani
2018-10-13  7:25       ` Eli Zaretskii
2018-10-13 15:28         ` Tom Tromey

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).