www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Disabling synchronized blocks locking overhead?

reply d coder <dlang.coder gmail.com> writes:
--000e0cd343ac506d5a04a67168ef
Content-Type: text/plain; charset=ISO-8859-1

Hello List

I am working on an application where I would normally be using
multithreading. But sometimes, depending on how much data sharing is being
shared, I might want to invoke the same application in single-threaded mode.

Now when I invoke it in single-threaded mode, I would not like to have all
the overhead incurred due to mutex/monitor locking. Does DMD have some
compile time flags to achieve that.

One way I can think about achieving this is by enclosing all the
'synchronized' declarations inside small version blocks. But I am afraid
this would make the code quite unwieldy.
Is there a recommended way of achieving this end?

Regards
- Puneet

--000e0cd343ac506d5a04a67168ef
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Hello List<div><br></div><div>I am working on an application where I would =
normally be using multithreading. But sometimes, depending on how much data=
 sharing is being shared, I might want to invoke the same application in si=
ngle-threaded mode.</div>

<div><br></div><div>Now when I invoke it in single-threaded mode, I would n=
ot like to have all the overhead incurred due to mutex/monitor locking. Doe=
s DMD have some compile time flags to achieve that.</div><div><br></div>

<div>One way I can think about achieving this is by enclosing all the &#39;=
synchronized&#39; declarations inside small version blocks. But I am afraid=
 this would make the code quite unwieldy.</div><div>Is there a recommended =
way of achieving this end?</div>

<div><br></div><div>Regards</div><div>- Puneet</div>

--000e0cd343ac506d5a04a67168ef--
Jun 24 2011
next sibling parent reply Khint Enco <khint none.net> writes:
On 24/06/11 09:57, d coder wrote:
 Hello List

 I am working on an application where I would normally be using
 multithreading. But sometimes, depending on how much data sharing is
 being shared, I might want to invoke the same application in
 single-threaded mode.

 Now when I invoke it in single-threaded mode, I would not like to have
 all the overhead incurred due to mutex/monitor locking. Does DMD have
 some compile time flags to achieve that.

 One way I can think about achieving this is by enclosing all the
 'synchronized' declarations inside small version blocks. But I am afraid
 this would make the code quite unwieldy.
 Is there a recommended way of achieving this end?

 Regards
 - Puneet

Yes, there is a flag .. in the source code! Just download the source and apply this patch: diff -r 2c283d0f3a2c dmd/func.c --- a/dmd/func.c Fri Jun 24 10:26:15 2011 +0100 +++ b/dmd/func.c Fri Jun 24 10:26:31 2011 +0100 -183,7 +183,7 stc |= STCimmutable; if (type->isConst()) stc |= STCconst; - if (type->isShared() || storage_class & STCsynchronized) + if (type->isShared()) stc |= STCshared; if (type->isWild()) stc |= STCwild; Then compile and keep both on standby for when you need it! Doing the above removes the following stuff from the object file, which is not present with unsynchronized methods. sub ESP,8 mov -4[EBP],EAX mov -8[EBP],EAX push EAX call _d_monitorenter PC32 mov EAX,-4[EBP] 86a94,96 push dword ptr -8[EBP] call _d_monitorexit PC32 add ESP,8
Jun 24 2011
parent reply Khint Enco <khint none.net> writes:
On 24/06/11 13:04, d coder wrote:
 Would the patch you sent still work?

 Regards
 - Puneet

Misfire! No, that 'patch' does nothing .. admittedly I'm still finding my way through the compiler source. It looks like I simply skipped the semantic check for the synchronized storage class .. or something like that.
Jun 24 2011
parent Khint Enco <khint none.net> writes:
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

On 25/06/11 00:50, Khint Enco wrote:
 Misfire! No, that 'patch' does nothing ..

I've added a -nosync flag to the compiler (v2.053) that removes all the effects of synchronized. It has been tested and I ran all unittests, or at least I think I did .. it took less than half a second .. It's a small patch, but does anyone mind checking it?
Jun 24 2011
prev sibling next sibling parent d coder <dlang.coder gmail.com> writes:
--bcaec554019a293f5c04a674030f
Content-Type: text/plain; charset=ISO-8859-1

 Yes, there is a flag .. in the source code! Just download the source and
 apply this patch:

Most of the times I use explicit synchronized code blocks, instead of adding "synchronized" attribute to the function itself. Would the patch you sent still work? Regards - Puneet --bcaec554019a293f5c04a674030f Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote"><blockquote class=3D"gmail_quote" style=3D"margi= n:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div><div class= =3D"h5"><br></div></div> Yes, there is a flag .. in the source code! Just download the source and ap= ply this patch:<br><br></blockquote><div><br></div><div>Thanks</div><div><b= r></div><div>Most of the times I use explicit synchronized code blocks, ins= tead of adding &quot;synchronized&quot; attribute to the function itself.</= div> <div><br></div><div>Would the patch you sent still work?</div><div><br></di= v><div>Regards</div><div>- Puneet</div></div> --bcaec554019a293f5c04a674030f--
Jun 24 2011
prev sibling next sibling parent Sean Kelly <sean invisibleduck.org> writes:
--Apple-Mail-4--92379700
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=us-ascii

If you synchronize via a Mutex you could create a type that detects whatever=
 and doesn't actually lock if single threaded.  core.thread.thread_needLock(=
) is an option if you can be sure a thread won't be spawned inside your magi=
cally not-locked function.=20

Sent from my iPhone

On Jun 24, 2011, at 5:04 AM, d coder <dlang.coder gmail.com> wrote:

=20
 Yes, there is a flag .. in the source code! Just download the source and a=

=20
=20
 Thanks
=20
 Most of the times I use explicit synchronized code blocks, instead of addi=

=20
 Would the patch you sent still work?
=20
 Regards
 - Puneet

--Apple-Mail-4--92379700 Content-Transfer-Encoding: 7bit Content-Type: text/html; charset=utf-8 <html><body bgcolor="#FFFFFF"><div>If you synchronize via a Mutex you could create a type that detects whatever and doesn't actually lock if single threaded. &nbsp;core.thread.thread_needLock() is an option if you can be sure a thread won't be spawned inside your magically not-locked function.&nbsp;</div><div><br>Sent from my iPhone</div><div><br>On Jun 24, 2011, at 5:04 AM, d coder &lt;<a href="mailto:dlang.coder gmail.com">dlang.coder gmail.com</a>&gt; wrote:<br><br></div><div></div><blockquote type="cite"><div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div><div class="h5"><br></div></div> Yes, there is a flag .. in the source code! Just download the source and apply this patch:<br><br></blockquote><div><br></div><div>Thanks</div><div <br></div><div>Most of the times I use explicit synchronized code blocks, instead of adding "synchronized" attribute to the function itself.</div> <div><br></div><div>Would the patch you sent still work?</div><div><br></div><div>Regards</div><div>- Puneet</div></div> </div></blockquote></body></html> --Apple-Mail-4--92379700--
Jun 24 2011
prev sibling parent d coder <dlang.coder gmail.com> writes:
--00151750ddfc2fcb4f04a7b6107c
Content-Type: text/plain; charset=ISO-8859-1

 I've added a -nosync flag to the compiler (v2.053) that removes all the

least I think I did .. it took less than half a second .. It's a small patch, but does anyone mind checking it?

Thanks. But I get loads of errors from druntime when I try your patch and turn on the -nosync compiler flag. I have patched against the latest dmd from github. Regards - Puneet /home/puneet/work/nosync-d/src/druntime/import/object.di(27): function declaration without return type. (Note that constructors are always named 'this') /home/puneet/work/nosync-d/src/druntime/import/object.di(27): no identifier for declarator immutable(char) /home/puneet/work/nosync-d/src/druntime/import/object.di(27): semicolon expected to close alias declaration /home/puneet/work/nosync-d/src/druntime/import/object.di(27): Declaration expected, not '[' /home/puneet/work/nosync-d/src/druntime/import/object.di(28): function declaration without return type. (Note that constructors are always named 'this') /home/puneet/work/nosync-d/src/druntime/import/object.di(28): no identifier for declarator immutable(wchar) /home/puneet/work/nosync-d/src/druntime/import/object.di(28): semicolon expected to close alias declaration /home/puneet/work/nosync-d/src/druntime/import/object.di(28): Declaration expected, not '[' /home/puneet/work/nosync-d/src/druntime/import/object.di(29): function declaration without return type. (Note that constructors are always named 'this') /home/puneet/work/nosync-d/src/druntime/import/object.di(29): no identifier for declarator immutable(dchar) /home/puneet/work/nosync-d/src/druntime/import/object.di(29): semicolon expected to close alias declaration /home/puneet/work/nosync-d/src/druntime/import/object.di(29): Declaration expected, not '[' /home/puneet/work/nosync-d/src/druntime/import/object.di(51): found 'ownee' when expecting ')' /home/puneet/work/nosync-d/src/druntime/import/object.di(51): semicolon expected following function declaration /home/puneet/work/nosync-d/src/druntime/import/object.di(51): Declaration expected, not ',' /home/puneet/work/nosync-d/src/phobos/std/stdio.d(776): found 'buf' when expecting ';' following statement /home/puneet/work/nosync-d/src/phobos/std/stdio.d(1861): function declaration without return type. (Note that constructors are always named 'this') /home/puneet/work/nosync-d/src/phobos/std/stdio.d(1861): found '[' when expecting ')' /home/puneet/work/nosync-d/src/phobos/std/stdio.d(1861): semicolon expected following auto declaration, not ']' /home/puneet/work/nosync-d/src/phobos/std/stdio.d(1861): found ']' instead of statement /home/puneet/work/nosync-d/src/phobos/std/stdio.d(1952): found ')' when expecting '.' following ubyte --00151750ddfc2fcb4f04a7b6107c Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div><meta http-equiv=3D"content-type" content=3D"text/html; charset=3Dutf-= 8"><div class=3D"gmail_quote"><blockquote class=3D"gmail_quote" style=3D"ma= rgin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; b= order-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-s= tyle: solid; padding-left: 1ex; "> <div class=3D"im"><blockquote class=3D"gmail_quote" style=3D"margin-top: 0p= x; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-w= idth: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid;= padding-left: 1ex; "> <br></blockquote></div>I&#39;ve added a -nosync flag to the compiler (v2.05= 3) that removes all the effects of synchronized. It has been tested and I r= an all unittests, or at least I think I did .. it took less than half a sec= ond ..<br> <br>It&#39;s a small patch, but does anyone mind checking it?<br></blockquo= te><div><br></div><div><br></div></div></div>Thanks. But I get loads of err= ors from druntime when I try your patch and turn on the -nosync compiler fl= ag. I have patched against the latest dmd from github.<div> <br></div><div>Regards</div><div>- Puneet<br><div><br></div><div><div>/home= /puneet/work/nosync-d/src/druntime/import/object.di(27): function declarati= on without return type. (Note that constructors are always named &#39;this&= #39;)</div> <div>/home/puneet/work/nosync-d/src/druntime/import/object.di(27): no ident= ifier for declarator immutable(char)</div><div>/home/puneet/work/nosync-d/s= rc/druntime/import/object.di(27): semicolon expected to close alias declara= tion</div> <div>/home/puneet/work/nosync-d/src/druntime/import/object.di(27): Declarat= ion expected, not &#39;[&#39;</div><div>/home/puneet/work/nosync-d/src/drun= time/import/object.di(28): function declaration without return type. (Note = that constructors are always named &#39;this&#39;)</div> <div>/home/puneet/work/nosync-d/src/druntime/import/object.di(28): no ident= ifier for declarator immutable(wchar)</div><div>/home/puneet/work/nosync-d/= src/druntime/import/object.di(28): semicolon expected to close alias declar= ation</div> <div>/home/puneet/work/nosync-d/src/druntime/import/object.di(28): Declarat= ion expected, not &#39;[&#39;</div><div>/home/puneet/work/nosync-d/src/drun= time/import/object.di(29): function declaration without return type. (Note = that constructors are always named &#39;this&#39;)</div> <div>/home/puneet/work/nosync-d/src/druntime/import/object.di(29): no ident= ifier for declarator immutable(dchar)</div><div>/home/puneet/work/nosync-d/= src/druntime/import/object.di(29): semicolon expected to close alias declar= ation</div> <div>/home/puneet/work/nosync-d/src/druntime/import/object.di(29): Declarat= ion expected, not &#39;[&#39;</div><div>/home/puneet/work/nosync-d/src/drun= time/import/object.di(51): found &#39;ownee&#39; when expecting &#39;)&#39;= </div> <div>/home/puneet/work/nosync-d/src/druntime/import/object.di(51): semicolo= n expected following function declaration</div><div>/home/puneet/work/nosyn= c-d/src/druntime/import/object.di(51): Declaration expected, not &#39;,&#39= ;</div> <div>/home/puneet/work/nosync-d/src/phobos/std/stdio.d(776): found &#39;buf= &#39; when expecting &#39;;&#39; following statement</div><div>/home/puneet= /work/nosync-d/src/phobos/std/stdio.d(1861): function declaration without r= eturn type. (Note that constructors are always named &#39;this&#39;)</div> <div>/home/puneet/work/nosync-d/src/phobos/std/stdio.d(1861): found &#39;[&= #39; when expecting &#39;)&#39;</div><div>/home/puneet/work/nosync-d/src/ph= obos/std/stdio.d(1861): semicolon expected following auto declaration, not = &#39;]&#39;</div> <div>/home/puneet/work/nosync-d/src/phobos/std/stdio.d(1861): found &#39;]&= #39; instead of statement</div><div>/home/puneet/work/nosync-d/src/phobos/s= td/stdio.d(1952): found &#39;)&#39; when expecting &#39;.&#39; following ub= yte</div> </div><div><br></div></div> --00151750ddfc2fcb4f04a7b6107c--
Jul 10 2011