[GRASS-SVN] r45869 - grass/trunk/lib/segment
svn_grass at osgeo.org
svn_grass at osgeo.org
Fri Apr 8 02:30:18 EDT 2011
Author: mmetz
Date: 2011-04-07 23:30:18 -0700 (Thu, 07 Apr 2011)
New Revision: 45869
Modified:
grass/trunk/lib/segment/format.c
grass/trunk/lib/segment/get_row.c
grass/trunk/lib/segment/init.c
grass/trunk/lib/segment/pagein.c
grass/trunk/lib/segment/pageout.c
grass/trunk/lib/segment/put.c
grass/trunk/lib/segment/put_row.c
grass/trunk/lib/segment/release.c
grass/trunk/lib/segment/seek.c
grass/trunk/lib/segment/setup.c
Log:
segment lib: code cleanup and optimization
Modified: grass/trunk/lib/segment/format.c
===================================================================
--- grass/trunk/lib/segment/format.c 2011-04-08 06:29:15 UTC (rev 45868)
+++ grass/trunk/lib/segment/format.c 2011-04-08 06:30:18 UTC (rev 45869)
@@ -12,12 +12,12 @@
* \date 2005-2009
*/
-#include <grass/config.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <limits.h>
+#include <grass/gis.h>
#include <grass/segment.h>
@@ -169,7 +169,7 @@
static int zero_fill(int fd, off_t nbytes)
{
#ifndef USE_LSEEK
- char buf[10240];
+ char buf[16384];
register char *b;
register int n;
Modified: grass/trunk/lib/segment/get_row.c
===================================================================
--- grass/trunk/lib/segment/get_row.c 2011-04-08 06:29:15 UTC (rev 45868)
+++ grass/trunk/lib/segment/get_row.c 2011-04-08 06:30:18 UTC (rev 45869)
@@ -16,6 +16,7 @@
#include <unistd.h>
#include <string.h>
#include <errno.h>
+#include <grass/gis.h>
#include <grass/segment.h>
Modified: grass/trunk/lib/segment/init.c
===================================================================
--- grass/trunk/lib/segment/init.c 2011-04-08 06:29:15 UTC (rev 45868)
+++ grass/trunk/lib/segment/init.c 2011-04-08 06:30:18 UTC (rev 45869)
@@ -21,6 +21,7 @@
#include <unistd.h>
#include <string.h>
#include <errno.h>
+#include <grass/gis.h>
#include <grass/segment.h>
Modified: grass/trunk/lib/segment/pagein.c
===================================================================
--- grass/trunk/lib/segment/pagein.c 2011-04-08 06:29:15 UTC (rev 45868)
+++ grass/trunk/lib/segment/pagein.c 2011-04-08 06:30:18 UTC (rev 45869)
@@ -16,8 +16,8 @@
#include <unistd.h>
#include <string.h>
#include <errno.h>
+#include <grass/gis.h>
#include <grass/segment.h>
-#include <grass/rbtree.h>
/**
@@ -38,19 +38,16 @@
{
int cur;
int read_result;
- SEGID *seg_found, seg_search;
/* is n the current segment? */
if (n == SEG->scb[SEG->cur].n)
return SEG->cur;
- /* search the in memory segments */
- seg_search.i = 0;
- seg_search.n = n;
- seg_found = rbtree_find(SEG->loaded, &seg_search);
- if (seg_found) {
- cur = seg_found->i;
+ /* segment n is in memory ? */
+ if (SEG->load_idx[n] >= 0) {
+ cur = SEG->load_idx[n];
+
if (SEG->scb[cur].age != SEG->youngest) {
/* splice out */
SEG->scb[cur].age->younger->older = SEG->scb[cur].age->older;
@@ -74,12 +71,9 @@
cur = SEG->oldest->cur;
SEG->oldest->cur = -1;
- /* unload segment (remove from search tree) */
+ /* unload segment */
if (SEG->scb[cur].n >= 0) {
- seg_search.n = SEG->scb[cur].n;
- if (rbtree_remove(SEG->loaded, &seg_search) == 0)
- G_fatal_error("could not remove segment");
- seg_search.n = n;
+ SEG->load_idx[SEG->scb[cur].n] = -1;
/* write it out if dirty */
if (SEG->scb[cur].dirty) {
@@ -115,10 +109,8 @@
return -1;
}
- /* remember loaded segment */
- seg_search.i = cur;
- if (rbtree_insert(SEG->loaded, &seg_search) == 0)
- G_fatal_error("could not insert segment");
+ /* add loaded segment to index */
+ SEG->load_idx[n] = cur;
/* make it youngest segment */
SEG->youngest = SEG->youngest->younger;
Modified: grass/trunk/lib/segment/pageout.c
===================================================================
--- grass/trunk/lib/segment/pageout.c 2011-04-08 06:29:15 UTC (rev 45868)
+++ grass/trunk/lib/segment/pageout.c 2011-04-08 06:30:18 UTC (rev 45869)
@@ -16,6 +16,7 @@
#include <unistd.h>
#include <string.h>
#include <errno.h>
+#include <grass/gis.h>
#include <grass/segment.h>
Modified: grass/trunk/lib/segment/put.c
===================================================================
--- grass/trunk/lib/segment/put.c 2011-04-08 06:29:15 UTC (rev 45868)
+++ grass/trunk/lib/segment/put.c 2011-04-08 06:30:18 UTC (rev 45869)
@@ -13,6 +13,7 @@
*/
#include <string.h>
+#include <grass/gis.h>
#include <grass/segment.h>
Modified: grass/trunk/lib/segment/put_row.c
===================================================================
--- grass/trunk/lib/segment/put_row.c 2011-04-08 06:29:15 UTC (rev 45868)
+++ grass/trunk/lib/segment/put_row.c 2011-04-08 06:30:18 UTC (rev 45869)
@@ -16,8 +16,8 @@
#include <string.h>
#include <errno.h>
#include <unistd.h>
+#include <grass/gis.h>
#include <grass/segment.h>
-#include <grass/gis.h>
/* buf is CELL * WRAT code */
Modified: grass/trunk/lib/segment/release.c
===================================================================
--- grass/trunk/lib/segment/release.c 2011-04-08 06:29:15 UTC (rev 45868)
+++ grass/trunk/lib/segment/release.c 2011-04-08 06:30:18 UTC (rev 45869)
@@ -13,8 +13,8 @@
*/
#include <stdlib.h>
+#include <grass/gis.h>
#include <grass/segment.h>
-#include <grass/rbtree.h>
/**
@@ -30,7 +30,7 @@
*
* \param[in,out] seg
* \return 1 if successful
- * \return -1 if segment is not available (not open)
+ * \return -1 if SEGMENT is not available (not open)
*/
int segment_release(SEGMENT * SEG)
@@ -41,12 +41,12 @@
return -1;
for (i = 0; i < SEG->nseg; i++)
- free(SEG->scb[i].buf);
- free(SEG->scb);
+ G_free(SEG->scb[i].buf);
+ G_free(SEG->scb);
- free(SEG->freeslot);
- free(SEG->agequeue);
- rbtree_destroy(SEG->loaded);
+ G_free(SEG->freeslot);
+ G_free(SEG->agequeue);
+ G_free(SEG->load_idx);
SEG->open = 0;
Modified: grass/trunk/lib/segment/seek.c
===================================================================
--- grass/trunk/lib/segment/seek.c 2011-04-08 06:29:15 UTC (rev 45868)
+++ grass/trunk/lib/segment/seek.c 2011-04-08 06:30:18 UTC (rev 45869)
@@ -12,7 +12,6 @@
* \date 2005-2009
*/
-#include <grass/config.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
@@ -48,10 +47,8 @@
int segment_seek_slow(const SEGMENT * SEG, int n, int index)
{
- off_t offset;
+ off_t offset = (off_t) n * SEG->size + index + SEG->offset;
- offset = (off_t) n * SEG->size + index + SEG->offset;
-
if (lseek(SEG->fd, offset, SEEK_SET) == (off_t) - 1) {
G_warning("segment_seek: %s", strerror(errno));
return -1;
Modified: grass/trunk/lib/segment/setup.c
===================================================================
--- grass/trunk/lib/segment/setup.c 2011-04-08 06:29:15 UTC (rev 45868)
+++ grass/trunk/lib/segment/setup.c 2011-04-08 06:30:18 UTC (rev 45869)
@@ -17,7 +17,6 @@
#include <math.h>
#include <grass/gis.h>
#include <grass/segment.h>
-#include <grass/rbtree.h>
/**
@@ -37,7 +36,7 @@
int segment_setup(SEGMENT * SEG)
{
int i;
- int seg_exp;
+ int seg_exp, n_total_segs;
SEG->open = 0;
@@ -90,11 +89,11 @@
}
/* adjust number of open segments if larger than number of total segments */
- if (SEG->nseg > SEG->spr * ((SEG->nrows + SEG->srows - 1) / SEG->srows)) {
+ n_total_segs = SEG->spr * ((SEG->nrows + SEG->srows - 1) / SEG->srows);
+ if (SEG->nseg > n_total_segs) {
G_debug(1, "segment: reducing number of open segments from %d to %d",
- SEG->nseg,
- SEG->spr * ((SEG->nrows + SEG->srows - 1) / SEG->srows));
- SEG->nseg = SEG->spr * ((SEG->nrows + SEG->srows - 1) / SEG->srows);
+ SEG->nseg, n_total_segs);
+ SEG->nseg = n_total_segs;
}
if ((SEG->scb =
@@ -140,25 +139,15 @@
SEG->cur = 0;
SEG->open = 1;
- SEG->loaded = rbtree_create(segment_compare, sizeof(SEGID));
+ /* SEG->loaded = rbtree_create(segment_compare, sizeof(SEGID)); */
+ /* SEG->loaded = NULL; */
+
+ /* index for each segment, same like cache of r.proj */
+ SEG->load_idx = G_malloc(n_total_segs * sizeof(int));
+ for (i = 0; i < n_total_segs; i++)
+ SEG->load_idx[i] = -1;
+
+
return 1;
}
-
-int segment_compare(const void *sega, const void *segb)
-{
- SEGID *a = (SEGID *) sega;
- SEGID *b = (SEGID *) segb;
-
- return a->n < b->n ? -1 : (a->n > b->n);
-
- /* short version of
-
- if (a->n > b->n)
- return 1;
- else if (a->n < b->n)
- return -1;
-
- return 0;
- */
-}
More information about the grass-commit
mailing list