From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: New lisp library -- cell.el Date: Fri, 28 Feb 2020 10:43:07 -0500 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="37196"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) Cc: emacs-devel@gnu.org To: "Zhu Zihao" Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Fri Feb 28 16:50:57 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 1j7huj-0009Zb-9z for ged-emacs-devel@m.gmane-mx.org; Fri, 28 Feb 2020 16:50:57 +0100 Original-Received: from localhost ([::1]:49100 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7hui-00018n-BA for ged-emacs-devel@m.gmane-mx.org; Fri, 28 Feb 2020 10:50:56 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:48128) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7hnF-0005EJ-Cd for emacs-devel@gnu.org; Fri, 28 Feb 2020 10:43:14 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j7hnD-0001DH-Iy for emacs-devel@gnu.org; Fri, 28 Feb 2020 10:43:12 -0500 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:23626) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j7hnD-0001Ch-Bf for emacs-devel@gnu.org; Fri, 28 Feb 2020 10:43:11 -0500 Original-Received: from pmg1.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 5EE6D101074; Fri, 28 Feb 2020 10:43:10 -0500 (EST) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 822C7100F42; Fri, 28 Feb 2020 10:43:08 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1582904588; bh=66RqDNcpYkQyN6sTUXfy/Yoa7pBs11tOz6+6s5DHcag=; h=From:To:Cc:Subject:References:Date:In-Reply-To:From; b=ZuZAYTNbg+EW1ywOzGE/vfqbx/8NjF2STHk6Kf1TOgxs1/PmSjSL6ylsEocAsl3Rq 4Whpxn577RTvKJStukAne6I9LIvJSf3B8kbrsstIx+lSrEmLvJS1aTS5GqvFrqzzHG UEKMvv8mYgyuN7EGboLYS5AdpXIu5Xr4s33A85re+CpA8Q2fMap7BLi8yPXBu2amh3 LQJBaUhetAzy7t8iOQIH7j6DCrm756Sz79Co+f0bhEbVaJA3LArH48pCcwqZAbjqpA QRUeLzxODWYe02P7QqEbC+9XvhmeczJZ2vzcZ3HDk7EqK3f5lfOQftY0lR6niAfpbA VLMuxZEEBWUxw== Original-Received: from pastel (unknown [216.154.50.221]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 52EAC120743; Fri, 28 Feb 2020 10:43:08 -0500 (EST) In-Reply-To: (Zhu Zihao's message of "Fri, 28 Feb 2020 14:21:11 +0800 (CST)") X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 132.204.25.50 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:245104 Archived-At: Some comments on the code: > (defsubst cell-option-none () > "Create an option in None variant." > cell-option--none-singleton) Why not use nil for "none"? That would simplify your code and most likely would make for much more natural/idiomatic code in the clients of your library. [ Traditionally in Lisp when you need something like a "option` (aka `Maybe`) type, you use `(cons X nil)` for `some X` and `nil` for `none`. ] > (cl-defstruct (cell-box > (:constructor nil) > (:constructor cell-box--make (-inner)) > (:copier nil)) > -inner) Rather than put the additional "-" in the slot name, you can use `(:conc-name call-box-)`. > (defalias 'cell-box-make 'cell-box--make > "Create a single mutable unit with INITIAL-VALUE.") > > (defalias 'cell-box-inner 'cell-box--inner > "Return the inner value of BOX. > \n(fn BOX)") Why bother with those indirections? > (defun cell-weak-get (cell) > "Return Some(inner) if reference in CELL still alive, otherwise return None." > (let* ((gensym (make-symbol "not-found")) > (val (gethash t (cell-weak--inner-table cell) gensym)) > (found (not (eq gensym val)))) > (if found > (cell-option-some val) > (cell-option-none)))) That makes every call to `cell-weak-get` allocate a new symbol only to throw it away. I'd recommend you define a top-level constant (defconst cell--not-found (make-symbol "not-found")) instead. There are other solutions to this problem that might be even better, tho. E.g. you could compute `found` as (< 0 (hash-table-count ...). Better yet, disallow weak cells containing `nil` (since `nil` can't be GC'd anyway it doesn't make much sense to put it inside a weak-reference) and you don't need this at all: (defun cell-weak-make (inner) "Create a cell takes a weak reference of INNER." (if (null inner) nil ;; We could also signal an error, but returning nil ;; allow us to treat nil as its own weak-reference, (let* ((cell (cell-weak--internal-make)) (internal-ht (cell-weak--inner-table cell))) (puthash t inner internal-ht) cell))) (defun cell-weak-get (cell) "Return inner if reference in CELL still alive, otherwise return nil." (when cell ;; If nil, it's a weak ref to nil. ;; The stored content is never nil, so it only returns nil if ;; the content was GC'd. (gethash t (cell-weak--inner-table cell)))) -- Stefan