Hey Ian! On Sat, Feb 15, 2003 at 02:46:33AM -0600, Ian Zink wrote: > I noticed gtetrinet was using quite a bit of CPU, so I decided to > profile it and I found that tetrinet_sendfield was using a about %40 of > the CPU time. So I determined the code on each call was looping > 15*22*12=3960 plus it was doing some other things that weren't > efficient. So I rewrote it... now it hardly shows up on the profile. > It's at like 6usec/call compared to 80usec/call. It could be optimized > more, but I don't see much point now that its fast enough. Below is the > new function: Very cool! I'm Cc:ing the gtetrinet list. Please submit any further patch to gtetrinet-list gnome org instead of me directly, it'll reach much more people involved in gtetrinet now. I, of course, would be glad if you joined the team. The list currently has a traffic of like... 1 post per week, as stuff happens too on IRC (#gtetrinet @ GIMPnet), but it should be interesting still. Thanks for the patch! > void tetrinet_sendfield (int reset) > { > int x, y, i, d = FALSE; /* d is the number of differences */ > char buf[1024], *p; > > char diff_buf[15][(FIELDWIDTH + 1)* FIELDHEIGHT * 2] = {0}; > > int row_count[15] = {1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1}; > > > g_snprintf (buf, sizeof(buf), "%d ", playernum); > > if(!reset) { > /* Find out which blocks changed, how, and how many */ > for (y = 0; y < FIELDHEIGHT; y ++) { > for (x = 0; x < FIELDWIDTH; x ++) { > > const int block = fields[playernum][y][x]; > > if (block != sentfield[y][x]) { > diff_buf[block][row_count[block]++] = x + '3'; > diff_buf[block][row_count[block]++] = y + '3'; > d++; > } > } > } > if (d == 0) return; // no differences > } > > if (reset || d >= FIELDHEIGHT*FIELDWIDTH) { > /* sending entire field is more efficient */ > p = buf + 2; > for (y = 0; y < FIELDHEIGHT; y ++) > for (x = 0; x < FIELDWIDTH; x ++) > *p++ = blocks[(int)fields[playernum][y][x]]; > *p = 0; > } > else { /* so now we need to create the buffer strings */ > for(i = 0; i < 15; ++i) { > if(row_count[i] > 1) { > diff_buf[i][0] = '!' + i; > GTET_O_STRCAT (buf, diff_buf[i]); > } > } > } > > > /* send it */ > client_outmessage (OUT_F, buf); > /* update the one in our memory */ > copyfield (sentfield, fields[playernum]); > } Jordi -- Jordi Mallach Pérez -- Debian developer http://www.debian.org/ jordi sindominio net jordi debian org http://www.sindominio.net/ GnuPG public key information available at http://oskuro.net/~jordi/
Attachment:
pgpnrfn7tzHUb.pgp
Description: PGP signature