Message from discussion
Weak references. Does node-weak and other simliar libraries, add significant overhead?
Received: by 10.52.72.100 with SMTP id c4mr3479647vdv.5.1350940989247;
Mon, 22 Oct 2012 14:23:09 -0700 (PDT)
X-BeenThere: nodejs@googlegroups.com
Received: by 10.52.141.116 with SMTP id rn20ls5308445vdb.5.gmail; Mon, 22 Oct
2012 14:23:00 -0700 (PDT)
Received: by 10.52.37.12 with SMTP id u12mr1740047vdj.8.1350940980423;
Mon, 22 Oct 2012 14:23:00 -0700 (PDT)
Date: Mon, 22 Oct 2012 14:22:59 -0700 (PDT)
From: Fredrik O <evoo...@gmail.com>
To: nodejs@googlegroups.com
Message-Id: <cb8bfce0-db08-48dd-9838-197d20d1e060@googlegroups.com>
In-Reply-To: <CAN5MXofucy7fWOBbCmQH-w-Tq0OKWr3FUmEeftm-FdBB2QHFKw@mail.gmail.com>
References: <0707b882-ef28-4f54-ad36-e9eaf03651f0@googlegroups.com> <4D68974870B54DB382F907695BE70046@thinkdigital.pt>
<CAN5MXofucy7fWOBbCmQH-w-Tq0OKWr3FUmEeftm-FdBB2QHFKw@mail.gmail.com>
Subject: Re: [nodejs] Weak references. Does node-weak and other simliar
libraries, add significant overhead?
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_812_18800932.1350940979975"
------=_Part_812_18800932.1350940979975
Content-Type: multipart/alternative;
boundary="----=_Part_813_5190207.1350940979975"
------=_Part_813_5190207.1350940979975
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Thanks for your responses, all!
I use it too support a simple (C++) RAII functionally, to reclaim
resources, both external and internal resources. I believe it is good idea.
It allows a resource, even if an exception get thrown be reclaimed. I
simply code in mind that a exception can get thrown almost anywhere. For
example this code is not safe:
var obj = pool.create(); //may be a connection pool
// code here which may throw an exception
pool.release(obj); //may not ever be reached, if the code before thrown an
exception, therefore not safe
The regular exception handling will not be a solution, because of the
asynchronous design of node.js. However, if I use weak references, I can
ignore the call to "release" if I want too. This feature is specially
useful for implementing multiple design patterns, for example flyweight:
//copy of C++ boost.flyweight, but in JavaScript
var obj = flyweight("This string will only exist once in memory, so even if
I create thousands of them, will no memory increase significantly
happen");//may be of any type
console.log(obj); //we can treat the object just like the object we
constructed it with: "string", with the exception it is read-only.
When all references dies will the object be released automatically, and
when we want to create a new instance will the library create a new cheap
reference to the value already in memory. This would never be possible
without weak references. I have actually created a module like this, which
I plans to release to the public some time.
So in conclusion, I have started to use it whenever there I believe it
fits, in multiple places, but I wonder if I need to worry about any
significant performance degradation?
PS. How would weak maps be used to call a custom function on garbage
collection?
What are your thoughts?
Thanks in advance!
------=_Part_813_5190207.1350940979975
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
Thanks for your responses, all!<br><br>I use it too support a simple (C++) =
RAII functionally, to reclaim resources, both external and internal resourc=
es. I believe it is good idea. It allows a resource, even if an exception g=
et thrown be reclaimed. I simply code in mind that a exception can get thro=
wn almost anywhere. For example this code is not safe:<br><br>var obj =3D p=
ool.create(); //may be a connection pool<br>// code here which may throw an=
exception<br>pool.release(obj); //may not ever be reached, if the code bef=
ore thrown an exception, therefore not safe<br><br>The regular exception ha=
ndling will not be a solution, because of the asynchronous design of node.j=
s. However, if I use weak references, I can ignore the call to "release" if=
I want too. This feature is specially useful for implementing multiple des=
ign patterns, for example flyweight:<br><br>//copy of C++ boost.flyweight, =
but in JavaScript<br>var obj =3D flyweight("This string will only exist onc=
e in memory, so even if I create thousands of them, will no memory increase=
significantly happen");//may be of any type<br>console.log(obj); //we can =
treat the object just like the object we constructed it with: "string", wit=
h the exception it is read-only.<br><br>When all references dies will the o=
bject be released automatically, and when we want to create a new instance =
will the library create a new cheap reference to the value already in memor=
y. This would never be possible without weak references. I have actually cr=
eated a module like this, which I plans to release to the public some time.=
<br><br>So in conclusion, I have started to use it whenever there I believe=
it fits, in multiple places, but I wonder if I need to worry about any sig=
nificant performance degradation? <br><br>PS. How would weak maps be used t=
o call a custom function on garbage collection?<br><br>What are your though=
ts?<br><br>Thanks in advance!<br><br>
------=_Part_813_5190207.1350940979975--
------=_Part_812_18800932.1350940979975--