From 821c61e9967e62fd81038e4b879c5452bffe2dfb Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Mon, 21 Jan 2013 13:13:23 +0100 Subject: improved reading files by using memory-mapping * file must be in primary memory for scintilla * we cannot write to scintilla's buffer memory directly * so mapping the file is best: in the best case it is not copied to primary memory and resides in kernel cache * in any case, mapping to memory is faster than read()ing into primary memory * copying from mapped virtual memory to scintilla buffer (via SCI_APPENDTEXT) is faster than copying from primary memory --- src/ring.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/ring.cpp b/src/ring.cpp index a7c3ebe..ed9e77c 100644 --- a/src/ring.cpp +++ b/src/ring.cpp @@ -105,21 +105,24 @@ Buffer::UndoTokenClose::run(void) bool Buffer::load(const gchar *filename) { - gchar *contents; - gsize size; + GMappedFile *file; + gsize length; - /* FIXME: prevent excessive allocations by reading file into buffer */ - if (!g_file_get_contents(filename, &contents, &size, NULL)) + file = g_mapped_file_new(filename, FALSE, NULL); + if (!file) return false; + length = g_mapped_file_get_length(file); edit(); interface.ssm(SCI_BEGINUNDOACTION); interface.ssm(SCI_CLEARALL); - interface.ssm(SCI_APPENDTEXT, size, (sptr_t)contents); + if (length > 0) + interface.ssm(SCI_APPENDTEXT, length, + (sptr_t)g_mapped_file_get_contents(file)); interface.ssm(SCI_ENDUNDOACTION); - g_free(contents); + g_mapped_file_unref(file); /* NOTE: currently buffer cannot be dirty */ #if 0 -- cgit v1.2.3