From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: =?UTF-8?B?VHXhuqVuLUFuaCBOZ3V54buFbg==?= Newsgroups: gmane.emacs.devel Subject: Re: Reliable after-change-functions (via: Using incremental parsing in Emacs) Date: Thu, 2 Apr 2020 00:55:45 +0700 Message-ID: References: <83369o1khx.fsf@gnu.org> <83imijz68s.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="17084"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Eli Zaretskii Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Wed Apr 01 19:57:00 2020 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jJhbn-0004IC-5o for ged-emacs-devel@m.gmane-mx.org; Wed, 01 Apr 2020 19:56:59 +0200 Original-Received: from localhost ([::1]:35388 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jJhbl-0005KH-PM for ged-emacs-devel@m.gmane-mx.org; Wed, 01 Apr 2020 13:56:57 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:48391) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jJhax-0004FM-91 for emacs-devel@gnu.org; Wed, 01 Apr 2020 13:56:08 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jJhaw-0008Ds-0S for emacs-devel@gnu.org; Wed, 01 Apr 2020 13:56:07 -0400 Original-Received: from mail-pj1-x1042.google.com ([2607:f8b0:4864:20::1042]:53816) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1jJhau-0008C4-3A; Wed, 01 Apr 2020 13:56:04 -0400 Original-Received: by mail-pj1-x1042.google.com with SMTP id l36so362906pjb.3; Wed, 01 Apr 2020 10:56:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=yu4rnjGTfDs7cNGumWvThOsSEXkadLHAFJYIxrUR8Zo=; b=tq0vy2wBaZINU3IcEWRSZyKTKvfgwywaiCNXvUKMb+pI1X4bIGjYxrU2mNDqVAKb6x 0x5JhGcqIl/dK4Qj3iUKe4r89kLxHzy/388+Hg8yS/jkDeLW/M0ds/SYdb7y6BueVWqJ O8ZQNG5QMnJ4tg3p30R7HD2FUNCUWOqqEfpkbII4DVLtgaeutDXq24nI4F2y84vD7u8j 1a0Bz+s1iDLecxvsU/HDkVGmuhOr8n9Q5TtwDafUBTeTPILl6iVMGHbLEF6PcEvnFbCd Da4BKo16uaXT12gNleea++pzoF7mKx7te81PV/mm7Ppigat1s0PZVGhiDoRZ5SBqn/kt oPqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=yu4rnjGTfDs7cNGumWvThOsSEXkadLHAFJYIxrUR8Zo=; b=AWaEzySSqZ/FWMwiQCRNlyp4fyUmBCOOyJnJi4jaCdUQAA+mEdb62sGrEyCIQjuJyE xwNSWsECtZqmkOXydq2d0Xc6nSZDRShUafJbS76SarRMoCjbAKhje6n6UOipUqfeTU/a B+CZgEfOi3lRrGEGYj6iY7G4Bp81nTvk4WO/0xLc5rs1QVuKiEvin4Vh7QvDg051DM/R 88M0qt0u1uDlat4BdwR1LLV6UhBKzG03PoipmF6DvrX9DwZbw9Aerh7POOo5PPAIeon6 RSzBvLcu7pXEOpGnSFGIbnca1HMDl+UR0msl6J/iWqG6CkezdjXpELEB/F8pKhltxRdy KWMA== X-Gm-Message-State: AGi0PuYxmiJC6MJNF878Fqrw9tnFloq+ONnRhgfNpybMC55t+9WaP1sY 57g3TrzAVFfGvtu8K1lfOJfIlXBTtyCpOLsYRkvB+HMeFZM= X-Google-Smtp-Source: APiQypKRg+tdT7HFPl1Y+sHnzasr7ElnqbmAU6HAYPb35dTOHzAkqHyP9n80KoDlSIPvXwZALrKqw+eHvQ3ymMCYVbw= X-Received: by 2002:a17:90a:e649:: with SMTP id ep9mr5972220pjb.161.1585763762196; Wed, 01 Apr 2020 10:56:02 -0700 (PDT) In-Reply-To: <83imijz68s.fsf@gnu.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::1042 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:246212 Archived-At: On Wed, Apr 1, 2020 at 8:26 PM Eli Zaretskii wrote: > > > From: Tu=E1=BA=A5n Anh Nguy=E1=BB=85n > > Date: Wed, 1 Apr 2020 13:17:42 +0700 > > Cc: emacs-devel@gnu.org > > > > Real usage with "xdisp.c": > > > > (define-advice tree-sitter--do-parse (:around (f &rest args) benchm= ark) > > (message "%s" (benchmark-run (apply f args)))) > > > > (0.257998 1 0.13326100000000096) > > And that is even without encoding the buffer text, IIUC what the > package does. > > > So yes, direct access to buffer's text from dynamic modules would be ni= ce. > > Did you consider using the API where an application can provide a > function to return text at a given offset? Such a function could be > relatively easily implemented for Emacs. > I don't understand what you mean. Below I'll explain how it works currently. `ts-parse' uses the Tree-sitter's API that consumes text in chunks: TSTree *ts_parser_parse( TSParser *self, const TSTree *old_tree, TSInput input ); typedef struct { void *payload; const char *(*read)( void *payload, uint32_t byte_offset, TSPoint position, uint32_t *bytes_read ); TSInputEncoding encoding; } TSInput; Because dynamic modules don't have direct access to buffer text, `ts-parse' uses the module function `copy_string_contents', and exposes this interface: (ts-parse PARSER INPUT-FUNCTION OLD-TREE) Here INPUT-FUNCTION must return a chunk of the buffer text, starting from the given byte offset, as a Lisp string. `ts-buffer-input' is one such function. So: 1. Chunks of the buffer text are copied into Lisp strings, through `buffer-substring-no-properties'. 2. These Lisp strings are copied into buffers of null-terminated utf-8 bytes, through `copy_string_contents'. 3. All these temporary Lisp strings create GC pressure. In the xdisp.c example, it was 100ms for GC, in addition to 150ms for parsing. 4. emacs-module-rs has an automatic, blanket workaround for this bug https://debbugs.gnu.org/cgi/bugreport.cgi?bug=3D31238. The workaround involves pairs of `make_global_ref' and `free_global_ref' calls, on all "suspected" `emacs_value's. #4 can be avoided if emacs-module-rs allows selectively disabling the blanket workaround. It's band-aid on top of band-aid, but at least it's workable. #3 can probably be alleviated by increasing the chunk size. However, they are consequences of #1 and #2. If dynamic modules have direct access to the buffer text, none of the above is an issue. Such direct access can be enabled by something like this: char* (*access_buffer_text) (emacs_env *env, emacs_value buffer, ptrdiff_t byte_offset, ptrdiff_t *size_inout); Of course, such an API would require extensive documentation on how it must be used, to ensure safety and correctness. > Btw, what do you do with the tree returned by the tree-sitter parser? > store it in some buffer-local variable? If so, how much memory does > such a tree take, and when, if ever, is that memory released? > It's stored in a buffer-local variable. I haven't measured the memory they take. Memory is released when the tree object is garbage-collected (it's a `user-ptr'). -- Tu=E1=BA=A5n-Anh Nguy=E1=BB=85n Software Engineer