diff -urN old/storage/myisam/mi_close.c new/storage/myisam/mi_close.c
--- old/storage/myisam/mi_close.c	2007-09-24 11:30:12.000000000 +0100
+++ new/storage/myisam/mi_close.c	2007-12-02 10:56:34.000000000 +0000
@@ -70,6 +70,13 @@
       error=my_errno;
     if (share->kfile >= 0)
     {
+      DBUG_PRINT("info", ("MR:Closing keyfile no %d", share->kfile));
+      /* If we are mmap'd, unmap. */
+      if (share->kfile_mmap != 0) {
+        DBUG_PRINT("info", ("MR:munmap"));
+      	munmap(share->kfile_mmap, share->kfile_mmap_length);
+      	share->kfile_mmap = 0;
+      }
       /*
         If we are crashed, we can safely flush the current state as it will
         not change the crashed state.
diff -urN old/storage/myisam/mi_open.c new/storage/myisam/mi_open.c
--- old/storage/myisam/mi_open.c	2007-09-24 11:29:42.000000000 +0100
+++ new/storage/myisam/mi_open.c	2007-12-02 11:11:02.000000000 +0000
@@ -120,6 +120,8 @@
 	  (kfile=my_open(name_buff,(open_mode=O_RDONLY) | O_SHARE,MYF(0))) < 0)
 	goto err;
     }
+    DBUG_PRINT("info", ("MR:Opened keyfile %s fileno %d", name_buff, kfile));
+    
     share->mode=open_mode;
     errpos=1;
     if (my_read(kfile, share->state.header.file_version, head_length,
@@ -280,7 +282,7 @@
 
     if (share->options & HA_OPTION_COMPRESS_RECORD)
       share->base.max_key_length+=2;	/* For safety */
-
+      
     if (!my_multi_malloc(MY_WME,
 			 &share,sizeof(*share),
 			 &share->state.rec_per_key_part,sizeof(long)*key_parts,
@@ -475,6 +477,25 @@
 
     if (mi_open_datafile(&info, share, -1))
       goto err;
+      
+    /* mmap key file. */
+    if (1)
+    {
+	    DBUG_PRINT("info", ("MR: try to mmap: key start %08x", share->base.keystart));
+	    
+	    my_off_t maplen = _mi_round_mmap_size(share->state.state.key_file_length);
+    	share->kfile_mmap_length = maplen;
+    	share->kfile_mmap = mmap(0 /* start ptr*/, 
+    				maplen /* length */, 
+    				PROT_READ | PROT_WRITE,
+    				MAP_SHARED, kfile, 0 /* offset */);
+    	if (share->kfile_mmap == MAP_FAILED) {
+			DBUG_PRINT("error",("MR: mmap failed"));
+			share->kfile_mmap = 0; /* Our "not set" error value */
+			goto err;
+    	}
+    }
+
     errpos=5;
 
     share->kfile=kfile;
diff -urN old/storage/myisam/mi_page.c new/storage/myisam/mi_page.c
--- old/storage/myisam/mi_page.c	2007-09-24 11:29:59.000000000 +0100
+++ new/storage/myisam/mi_page.c	2007-12-02 10:56:34.000000000 +0000
@@ -27,6 +27,29 @@
   uint page_size;
   DBUG_ENTER("_mi_fetch_keypage");
   DBUG_PRINT("enter",("page: %ld", (long) page));
+  /* Check if we can get it from mmap'd space. */
+  if (info->s->kfile_mmap != 0) {
+  	if ((page + keyinfo->block_length) <= (info->s->kfile_mmap_length) ) {
+		DBUG_PRINT("info",("Getting page : %ld from mmap area", (long) page));
+		/* copy the page from the mmap. */
+		memcpy(buff, info->s->kfile_mmap + page, keyinfo->block_length);
+		tmp = buff;
+		/* cool. */
+		/* If we used info->buff one way or another, set buff_used=1 */
+		if (tmp == info->buff)
+			info->buff_used=1;
+		/* Set last_keypage apparently. */
+		info->last_keypage=page;
+		DBUG_RETURN(buff);
+  	}
+  } else {
+    DBUG_PRINT("error",("Page not mmap'd ",my_errno));
+    info->last_keypage=HA_OFFSET_ERROR;
+    mi_print_error(info->s, HA_ERR_CRASHED);
+    my_errno=HA_ERR_CRASHED;
+    DBUG_RETURN(0);
+  }
+  /*
 
   tmp=(uchar*) key_cache_read(info->s->key_cache,
                              info->s->kfile, page, level, (uchar*) buff,
@@ -56,6 +79,7 @@
     tmp = 0;
   }
   DBUG_RETURN(tmp);
+  */
 } /* _mi_fetch_keypage */
 
 
@@ -83,6 +107,22 @@
   DBUG_DUMP("buff",(uchar*) buff,mi_getint(buff));
 #endif
 
+  /* Check if we can write it to mmap'd space. */
+  if (info->s->kfile_mmap != 0) {
+  	if ((page + keyinfo->block_length) <= (info->s->kfile_mmap_length) ) {
+		DBUG_PRINT("info",("Writing page : %ld to mmap area", (long) page));
+		/* Copy the page to the mmap. */
+		memcpy(info->s->kfile_mmap + page, buff, keyinfo->block_length);
+		/* cool. */
+		DBUG_RETURN(0);
+  	}
+  } else {
+    DBUG_PRINT("error",("Page not mmap'd ",my_errno));
+    my_errno=EINVAL;
+    DBUG_RETURN((-1));
+  }
+  /*
+
   if ((length=keyinfo->block_length) > IO_SIZE*2 &&
       info->state->key_file_length != page+length)
     length= ((mi_getint(buff)+IO_SIZE-1) & (uint) ~(IO_SIZE-1));
@@ -98,6 +138,7 @@
 			 (uint) keyinfo->block_length,
 			 (int) ((info->lock_type != F_UNLCK) ||
 				info->s->delay_key_write))));
+	*/
 } /* mi_write_keypage */
 
 
@@ -110,51 +151,109 @@
   uchar buff[8];
   DBUG_ENTER("_mi_dispose");
   DBUG_PRINT("enter",("pos: %ld", (long) pos));
-
+  
   old_link= info->s->state.key_del[keyinfo->block_size_index];
   info->s->state.key_del[keyinfo->block_size_index]= pos;
   mi_sizestore(buff,old_link);
   info->s->state.changed|= STATE_NOT_SORTED_PAGES;
+  /*
   DBUG_RETURN(key_cache_write(info->s->key_cache,
                               info->s->kfile, pos , level, buff,
 			      sizeof(buff),
 			      (uint) keyinfo->block_length,
-			      (int) (info->lock_type != F_UNLCK)));
+			      (int) (info->lock_type != F_UNLCK))); 
+  */
+  /* Write the buff into the mmap'd file. */
+  memcpy(info->s->kfile_mmap + pos, buff, sizeof(buff));
+  DBUG_RETURN(0);
 } /* _mi_dispose */
 
+static int _mi_resize_mmap(MYISAM_SHARE *share, my_off_t newsize)
+{
+    /* Check if the existing mmap area is big enough. */
+    my_off_t new_mapsize = _mi_round_mmap_size(newsize);
+    if (new_mapsize != share->kfile_mmap_length) {
+    	/* If not, resize it.*/
+    	void * old_map = share->kfile_mmap;
+    	my_off_t old_mapsize = share->kfile_mmap_length;
+    	void * new_map = mremap(old_map,
+    				old_mapsize,
+    				new_mapsize,
+    				MREMAP_MAYMOVE /* flags */ );
+    	if (new_map == MAP_FAILED) {
+			DBUG_PRINT("error",("MR: mmap failed"));
+			return 1;
+    	}
+    	share->kfile_mmap_length = new_mapsize;
+    	share->kfile_mmap = new_map;
+    }
+    /* Resize the file on disk. */
+    if (ftruncate(share->kfile, newsize) != 0) {
+    	DBUG_PRINT("error",("MR: ftruncate failed"));
+    	return 1;
+    }
+    return 0;
+}
 
 	/* Make new page on disk */
 
 my_off_t _mi_new(register MI_INFO *info, MI_KEYDEF *keyinfo, int level)
 {
   my_off_t pos;
-  uchar buff[8];
   DBUG_ENTER("_mi_new");
 
+  /* Check if the first free block exists.... */
   if ((pos= info->s->state.key_del[keyinfo->block_size_index]) ==
       HA_OFFSET_ERROR)
   {
+  	/* No deleted blocks in the key. Allocate a new one
+  		at the end. */
+  	/* Check if full */
     if (info->state->key_file_length >=
 	info->s->base.max_key_file_length - keyinfo->block_length)
     {
+      /* Index file can't become any bigger. */
       my_errno=HA_ERR_INDEX_FILE_FULL;
       DBUG_RETURN(HA_OFFSET_ERROR);
     }
+    /* No deleted blocks and not full - allocate at end. */
     pos=info->state->key_file_length;
-    info->state->key_file_length+= keyinfo->block_length;
+    info->state->key_file_length += keyinfo->block_length;
+    /* Resize the mmap area. */
+    if (_mi_resize_mmap(info->s, info->state->key_file_length) != 0) {
+    	/* mmap resize failed. */
+    	DBUG_PRINT("error",("MR: mmap resize failed"));
+		my_errno=HA_ERR_INDEX_FILE_FULL;
+		DBUG_RETURN(HA_OFFSET_ERROR);
+    }
   }
   else
   {
-    if (!key_cache_read(info->s->key_cache,
-                        info->s->kfile, pos, level,
-			buff,
-			(uint) sizeof(buff),
-			(uint) keyinfo->block_length,0))
-      pos= HA_OFFSET_ERROR;
-    else
-      info->s->state.key_del[keyinfo->block_size_index]= mi_sizekorr(buff);
+    /* Allocate the first free block,
+    	change the first free block pointer to
+    	point at the next free block */
+    /* The next free block is pointed to by the contents
+    	of the one we're now allocating. */
+	uchar * buff = (info->s->kfile_mmap + pos);
+    info->s->state.key_del[keyinfo->block_size_index]= mi_sizekorr(buff);
   }
   info->s->state.changed|= STATE_NOT_SORTED_PAGES;
   DBUG_PRINT("exit",("Pos: %ld",(long) pos));
   DBUG_RETURN(pos);
 } /* _mi_new */
+
+/* 
+ * round up to the smallest number of bytes we can map,
+ * based on the page size. So that mmap() will work.
+ */
+my_off_t _mi_round_mmap_size(my_off_t desiredsize)
+{
+	my_off_t pagesize = getpagesize();
+	if (desiredsize == 0) {
+		return pagesize;
+	}
+	my_off_t pagecount = (desiredsize + pagesize - 1) / pagesize;
+	return pagecount * pagesize;
+}
+    
+
diff -urN old/storage/myisam/myisamdef.h new/storage/myisam/myisamdef.h
--- old/storage/myisam/myisamdef.h	2007-09-24 11:29:40.000000000 +0100
+++ new/storage/myisam/myisamdef.h	2007-12-02 10:56:34.000000000 +0000
@@ -219,6 +219,9 @@
   uint     nonmmaped_inserts;           /* counter of writing in non-mmaped
                                            area */
   rw_lock_t mmap_lock;
+  /* MR: mmaped keys. */
+  void * kfile_mmap;
+  size_t kfile_mmap_length;
 } MYISAM_SHARE;
 
 
@@ -785,6 +788,9 @@
 int sort_write_record(MI_SORT_PARAM *sort_param);
 int _create_index_by_sort(MI_SORT_PARAM *info,my_bool no_messages, ulong);
 
+/* Function for calculating mmap sizes. In mi_page.c */
+my_off_t _mi_round_mmap_size(my_off_t desiredsize);
+
 #ifdef __cplusplus
 }
 #endif

