Disable threaded index writing by default The interlocking on the write threads was not being done properly (index entries were sometimes written out of order). With proper interlocking, the threaded write is only marginally faster on big index files, and slower on the smaller ones because of the overhead when creating threads. The threaded index writing has been temporarily disabled; after more accurate benchmarks, if might be possible to enable it again only when writing very large index files (> 1000 entries). Signed-off-by: Vicent Marti <tanoku@gmail.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
diff --git a/src/index.c b/src/index.c
index e4271ef..297836f 100644
--- a/src/index.c
+++ b/src/index.c
@@ -730,9 +730,9 @@ static void *create_disk_entry(size_t *disk_size, git_index_entry *entry)
return ondisk;
}
-#ifdef GIT_THREADS
+#if defined(GIT_THREADS) && defined(GIT_INDEX_THREADED)
-#define THREAD_QUEUE_SIZE 8
+#define THREAD_QUEUE_SIZE 64
typedef struct {
void *data;
@@ -745,7 +745,7 @@ typedef struct {
void (*process_entry)(void *extra_data, index_thread_entry *entry);
index_thread_entry *buffer[THREAD_QUEUE_SIZE];
- int count, pos;
+ int count, read_pos, write_pos;
git_lck mutex;
git_cnd entry_available, space_available;
@@ -757,7 +757,7 @@ void index_thread_enqueue(index_thread_queue *queue, index_thread_entry *entry)
if (queue->count == THREAD_QUEUE_SIZE)
gitcnd_wait(&queue->space_available, &queue->mutex);
- queue->buffer[queue->pos++ % THREAD_QUEUE_SIZE] = entry;
+ queue->buffer[queue->write_pos++ % THREAD_QUEUE_SIZE] = entry;
queue->count++;
gitcnd_signal(&queue->entry_available);
@@ -785,12 +785,15 @@ void *index_thread(void *attr)
if (queue->count == 0)
gitcnd_wait(&queue->entry_available, &queue->mutex);
- entry = queue->buffer[(queue->pos - 1) % THREAD_QUEUE_SIZE];
+ entry = queue->buffer[queue->read_pos++ % THREAD_QUEUE_SIZE];
queue->count--;
gitcnd_signal(&queue->space_available);
gitlck_unlock(&queue->mutex);
+ if (entry == NULL)
+ break;
+
queue->process_entry(queue->extra_data, entry);
if (gitrc_dec(&entry->refcount)) {
@@ -827,14 +830,16 @@ static int write_entries(git_index *index, git_filelock *file, git_hash_ctx *dig
write_queue->process_entry = thread_write_entry;
write_queue->count = 0;
- write_queue->pos = 0;
+ write_queue->read_pos = 0;
+ write_queue->write_pos = 0;
+
gitlck_init(&write_queue->mutex);
gitcnd_init(&write_queue->space_available, NULL);
gitcnd_init(&write_queue->entry_available, NULL);
if (git_thread_create(&write_thread, NULL, index_thread, (void *)write_queue) < 0) {
error = GIT_EOSERR;
- goto cleanup;
+ goto thread_error;
}
}
@@ -850,14 +855,16 @@ static int write_entries(git_index *index, git_filelock *file, git_hash_ctx *dig
hash_queue->process_entry = thread_hash_entry;
hash_queue->count = 0;
- hash_queue->pos = 0;
+ hash_queue->read_pos = 0;
+ hash_queue->write_pos = 0;
+
gitlck_init(&hash_queue->mutex);
gitcnd_init(&hash_queue->space_available, NULL);
gitcnd_init(&hash_queue->entry_available, NULL);
if (git_thread_create(&hash_thread, NULL, index_thread, (void *)hash_queue) < 0) {
error = GIT_EOSERR;
- goto cleanup;
+ goto thread_error;
}
}
@@ -875,13 +882,13 @@ static int write_entries(git_index *index, git_filelock *file, git_hash_ctx *dig
thread_entry = git__malloc(sizeof(index_thread_entry));
if (thread_entry == NULL) {
error = GIT_ENOMEM;
- goto cleanup;
+ goto thread_error;
}
thread_entry->data = create_disk_entry(&thread_entry->size, entry);
if (thread_entry->data == NULL) {
error = GIT_ENOMEM;
- goto cleanup;
+ goto thread_error;
}
/* queue in both queues */
@@ -891,7 +898,22 @@ static int write_entries(git_index *index, git_filelock *file, git_hash_ctx *dig
index_thread_enqueue(hash_queue, thread_entry);
}
-cleanup:
+ /* kill the two threads by queuing a NULL item */
+ {
+ index_thread_enqueue(write_queue, NULL);
+ index_thread_enqueue(hash_queue, NULL);
+ }
+
+ /* wait for them to terminate */
+ git_thread_join(write_thread, NULL);
+ git_thread_join(hash_thread, NULL);
+
+ free(write_queue);
+ free(hash_queue);
+
+ return GIT_SUCCESS;
+
+thread_error:
git_thread_kill(write_thread);
git_thread_kill(hash_thread);