1999Q1/
<!-- MHonArc v2.4.4 -->
<!--X-Subject: [MUD&#45;Dev] Re: optimizing code -->
<!--X-From-R13: Quevf Uenl <ptNnzv&#45;pt.UenlEntr.Sqzbagba.OP.QO> -->
<!--X-Date: Tue, 9 Feb 1999 18:41:04 &#45;0800 -->
<!--X-Message-Id: 199902100240.TAA01388@ami&#45;cg.GraySage.Edmonton.AB.CA -->
<!--X-Content-Type: text/plain -->
<!--X-Head-End-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>MUD-Dev message, [MUD-Dev] Re: optimizing code</title>
<!-- meta name="robots" content="noindex,nofollow" -->
<link rev="made" href="mailto:cg#ami-cg,GraySage.Edmonton.AB.CA">
</head>
<body background="/backgrounds/paperback.gif" bgcolor="#ffffff"
      text="#000000" link="#0000FF" alink="#FF0000" vlink="#006000">

  <font size="+4" color="#804040">
    <strong><em>MUD-Dev<br>mailing list archive</em></strong>
  </font>
      
<br>
[&nbsp;<a href="../">Other Periods</a>
&nbsp;|&nbsp;<a href="../../">Other mailing lists</a>
&nbsp;|&nbsp;<a href="/search.php3">Search</a>
&nbsp;]
<br clear=all><hr>
<!--X-Body-Begin-->
<!--X-User-Header-->
<!--X-User-Header-End-->
<!--X-TopPNI-->

Date:&nbsp;
[&nbsp;<a href="msg00374.html">Previous</a>
&nbsp;|&nbsp;<a href="msg00376.html">Next</a>
&nbsp;]
&nbsp;&nbsp;&nbsp;&nbsp;
Thread:&nbsp;
[&nbsp;<a href="msg00377.html">Previous</a>
&nbsp;|&nbsp;<a href="msg00371.html">Next</a>
&nbsp;]
&nbsp;&nbsp;&nbsp;&nbsp;
Index:&nbsp;
[&nbsp;<A HREF="author.html#00375">Author</A>
&nbsp;|&nbsp;<A HREF="#00375">Date</A>
&nbsp;|&nbsp;<A HREF="thread.html#00375">Thread</A>
&nbsp;]

<!--X-TopPNI-End-->
<!--X-MsgBody-->
<!--X-Subject-Header-Begin-->
<H1>[MUD-Dev] Re: optimizing code</H1>
<HR>
<!--X-Subject-Header-End-->
<!--X-Head-of-Message-->
<UL>
<LI><em>To</em>: <A HREF="mailto:mud-dev#kanga,nu">mud-dev#kanga,nu</A></LI>
<LI><em>Subject</em>: [MUD-Dev] Re: optimizing code</LI>
<LI><em>From</em>: Chris Gray &lt;<A HREF="mailto:cg#ami-cg,GraySage.Edmonton.AB.CA">cg#ami-cg,GraySage.Edmonton.AB.CA</A>&gt;</LI>
<LI><em>Date</em>: Tue, 9 Feb 1999 19:40:04 -0700</LI>
<LI><em>Reply-To</em>: <A HREF="mailto:mud-dev#kanga,nu">mud-dev#kanga,nu</A></LI>
</UL>
<!--X-Head-of-Message-End-->
<!--X-Head-Body-Sep-Begin-->
<HR>
<!--X-Head-Body-Sep-End-->
<!--X-Body-of-Message-->
<PRE>
[Hans-Henrik Staerfeldt replying to &lt;diablo#best,com&gt;:]

 &gt;The way to do this is to implement an 'event queue'. The queue is a sorted
 &gt;list of whan what is going to happend. So you peek at the first element,
 &gt;and notice that it need activation at time x, then wait (sleep) for x-now
 &gt;timeslices and then start the event at the top of the queue (might be
 &gt;a mobile moving, or parsing of one players commands). If the event is
 &gt;to be recurring regularly, insert a new event in the list at the time
 &gt;the event is to recur .. as simple as that.
 &gt;
 &gt;If a mobile has two timers, it should have two event in the queue.
 &gt;We have used this system extremely successfully in Valhalla where the
 &gt;event queue is the very core of the engine (along with messages)
 &gt;to handle unlimited number of co-routines to be attached to each
 &gt;object.

Agreement here. This sort of question has come up a couple of times
over the years, and I think it comes down to how your MUD is structured.
Mine is completely single-threaded - it isn't linked with a threading
library. So, I have a sorted list of events (sorted by expiry time)
like Hans-Henrik describes. My way of waiting for the time to the
earliest event to expire is to put that time into the main 'select'
call on the connection and client sockets. Since all new events will
be created either by user action or an internal event from the timer
queue, I know that the select is not active when any new event is added
to the queue. (It was a bit more compicated on the Amiga, where I had
to cancel a too-long timer and restart it at the desired interval.)

A couple of refinements to this can be done. One is that the MUD is
there to service players, not its internal workings. So, I put a limit
on how many timer events I would service before going back to check
for client socket activity by just doing the select with a 0 timeout -
no need for special code.

Also, whatever wakeup mechanism is used (select in this discussion) isn't
guaranteed to wakeup right on time - it may be a bit late. So, after the
wakeup and servicing of sockets, I find the current time, and run
through the event list doing events scheduled for any time before the
current time (subject to the above-mentioned event limit).

My mobiles ("machines" in my terminology) normally only have one event
each. When that goes off, they do whatever they need to do, and end the
event processing by scheduling the next one. Having two separate events
might complicate the code in those events, if they have any possibilities
of interacting.

[P.S. No need to be formal about names - Chris G. works fine. The 'G.'
being needed since there are at least 2 active Chris's on the list.
Although the other seems to go by JC or JCL.]

--
Don't design inefficiency in - it'll happen in the implementation. - me

Chris Gray     cg#ami-cg,GraySage.Edmonton.AB.CA
               <A  HREF="http://www.GraySage.Edmonton.AB.CA/cg/">http://www.GraySage.Edmonton.AB.CA/cg/</A>


</PRE>

<!--X-Body-of-Message-End-->
<!--X-MsgBody-End-->
<!--X-Follow-Ups-->
<HR>
<!--X-Follow-Ups-End-->
<!--X-References-->
<!--X-References-End-->
<!--X-BotPNI-->
<UL>
<LI>Prev by Date:
<STRONG><A HREF="msg00374.html">[MUD-Dev] Re: optimizing code</A></STRONG>
</LI>
<LI>Next by Date:
<STRONG><A HREF="msg00376.html">[MUD-Dev] World-file parsing and RTTI?</A></STRONG>
</LI>
<LI>Prev by thread:
<STRONG><A HREF="msg00377.html">[MUD-Dev] Re: World-file parsing and RTTI?</A></STRONG>
</LI>
<LI>Next by thread:
<STRONG><A HREF="msg00371.html">[MUD-Dev] code profiling</A></STRONG>
</LI>
<LI>Index(es):
<UL>
<LI><A HREF="index.html#00375"><STRONG>Date</STRONG></A></LI>
<LI><A HREF="thread.html#00375"><STRONG>Thread</STRONG></A></LI>
</UL>
</LI>
</UL>

<!--X-BotPNI-End-->
<!--X-User-Footer-->
<!--X-User-Footer-End-->
<ul><li>Thread context:
<BLOCKQUOTE><UL>
<LI><STRONG>[MUD-Dev] Re: pet peeves</STRONG>, <EM>(continued)</EM>
<ul compact>
<LI><strong><A NAME="00379" HREF="msg00379.html">[MUD-Dev] Re: pet peeves</A></strong>, 
diablo <a href="mailto:diablo#best,com">diablo#best,com</a>, Thu 11 Feb 1999, 21:51 GMT
<UL>
<LI><strong><A NAME="00381" HREF="msg00381.html">[MUD-Dev] Re: pet peeves</A></strong>, 
Richard Woolcock <a href="mailto:KaVir#dial,pipex.com">KaVir#dial,pipex.com</a>, Thu 11 Feb 1999, 23:02 GMT
</LI>
</UL>
</LI>
</ul>
</LI>
<LI><strong><A NAME="00376" HREF="msg00376.html">[MUD-Dev] World-file parsing and RTTI?</A></strong>, 
The Arrow <a href="mailto:arrow#trelleborg,mail.telia.com">arrow#trelleborg,mail.telia.com</a>, Wed 10 Feb 1999, 18:45 GMT
<UL>
<LI><strong><A NAME="00377" HREF="msg00377.html">[MUD-Dev] Re: World-file parsing and RTTI?</A></strong>, 
Mark Gritter <a href="mailto:mark#erdos,Stanford.EDU">mark#erdos,Stanford.EDU</a>, Wed 10 Feb 1999, 22:55 GMT
</LI>
</UL>
</LI>
<LI><strong><A NAME="00375" HREF="msg00375.html">[MUD-Dev] Re: optimizing code</A></strong>, 
Chris Gray <a href="mailto:cg#ami-cg,GraySage.Edmonton.AB.CA">cg#ami-cg,GraySage.Edmonton.AB.CA</a>, Wed 10 Feb 1999, 02:41 GMT
<LI><strong><A NAME="00371" HREF="msg00371.html">[MUD-Dev] code profiling</A></strong>, 
Chris Gray <a href="mailto:cg#ami-cg,GraySage.Edmonton.AB.CA">cg#ami-cg,GraySage.Edmonton.AB.CA</a>, Tue 09 Feb 1999, 06:00 GMT
<UL>
<LI><strong><A NAME="00373" HREF="msg00373.html">[MUD-Dev] optimizing code</A></strong>, 
diablo <a href="mailto:diablo#best,com">diablo#best,com</a>, Tue 09 Feb 1999, 06:31 GMT
<UL>
<LI><strong><A NAME="00374" HREF="msg00374.html">[MUD-Dev] Re: optimizing code</A></strong>, 
Hans-Henrik Staerfeldt <a href="mailto:hhs#cbs,dtu.dk">hhs#cbs,dtu.dk</a>, Tue 09 Feb 1999, 14:58 GMT
</LI>
</UL>
</LI>
</UL>
</LI>
<LI><strong><A NAME="00366" HREF="msg00366.html">[MUD-Dev] Re: Question on c++ switch optimization, and parsers in general.</A></strong>, 
Chris Gray <a href="mailto:cg#ami-cg,GraySage.Edmonton.AB.CA">cg#ami-cg,GraySage.Edmonton.AB.CA</a>, Tue 09 Feb 1999, 03:31 GMT
</LI>
</UL></BLOCKQUOTE>

</ul>
<hr>
<center>
[&nbsp;<a href="../">Other Periods</a>
&nbsp;|&nbsp;<a href="../../">Other mailing lists</a>
&nbsp;|&nbsp;<a href="/search.php3">Search</a>
&nbsp;]
</center>
<hr>
</body>
</html>