Replace SongList with struct songvec

Our linked-list implementation is wasteful and the
SongList isn't modified enough to benefit from being a linked
list.  So use a more compact array of song pointers which
saves ~200K on a library with ~9K songs (on x86-32).
This commit is contained in:
Eric Wong 2008-09-23 20:48:39 +02:00 committed by Max Kellermann
parent afe6ce7210
commit 0bec1d3807
11 changed files with 211 additions and 112 deletions

View File

@ -86,6 +86,7 @@ mpd_headers = \
song.h \ song.h \
song_print.h \ song_print.h \
song_save.h \ song_save.h \
songvec.h \
state_file.h \ state_file.h \
stats.h \ stats.h \
tag.h \ tag.h \
@ -156,6 +157,7 @@ mpd_SOURCES = \
song.c \ song.c \
song_print.c \ song_print.c \
song_save.c \ song_save.c \
songvec.c \
state_file.c \ state_file.c \
stats.c \ stats.c \
tag.c \ tag.c \

View File

@ -51,7 +51,7 @@ static int countSongsInDirectory(Directory * directory,
{ {
int *count = (int *)data; int *count = (int *)data;
*count += directory->songs->numberOfNodes; *count += directory->songs.nr;
return 0; return 0;
} }
@ -362,7 +362,7 @@ static int sumSavedFilenameMemoryInDirectory(Directory * dir, void *data)
return 0; return 0;
*sum += (strlen(getDirectoryPath(dir)) + 1 - sizeof(Directory *)) * *sum += (strlen(getDirectoryPath(dir)) + 1 - sizeof(Directory *)) *
dir->songs->numberOfNodes; dir->songs.nr;
return 0; return 0;
} }

View File

@ -241,14 +241,14 @@ static Directory *newDirectory(const char *dirname, Directory * parent)
{ {
Directory *directory; Directory *directory;
directory = xmalloc(sizeof(Directory)); directory = xcalloc(1, sizeof(Directory));
if (dirname && strlen(dirname)) if (dirname && strlen(dirname))
directory->path = xstrdup(dirname); directory->path = xstrdup(dirname);
else else
directory->path = NULL; directory->path = NULL;
directory->subDirectories = newDirectoryList(); directory->subDirectories = newDirectoryList();
directory->songs = newSongList(); assert(!directory->songs.base);
directory->stat = 0; directory->stat = 0;
directory->inode = 0; directory->inode = 0;
directory->device = 0; directory->device = 0;
@ -260,7 +260,7 @@ static Directory *newDirectory(const char *dirname, Directory * parent)
static void freeDirectory(Directory * directory) static void freeDirectory(Directory * directory)
{ {
freeDirectoryList(directory->subDirectories); freeDirectoryList(directory->subDirectories);
freeSongList(directory->songs); songvec_free(&directory->songs);
if (directory->path) if (directory->path)
free(directory->path); free(directory->path);
free(directory); free(directory);
@ -275,13 +275,13 @@ static void freeDirectoryList(DirectoryList * directoryList)
static void removeSongFromDirectory(Directory * directory, const char *shortname) static void removeSongFromDirectory(Directory * directory, const char *shortname)
{ {
void *song; Song *song = songvec_find(&directory->songs, shortname);
if (findInList(directory->songs, shortname, &song)) { if (song) {
char path_max_tmp[MPD_PATH_MAX]; /* wasteful */ char path_max_tmp[MPD_PATH_MAX]; /* wasteful */
LOG("removing: %s\n", LOG("removing: %s\n", get_song_url(path_max_tmp, song));
get_song_url(path_max_tmp, (Song *) song)); songvec_delete(&directory->songs, song);
deleteFromList(directory->songs, shortname); freeSong(song);
} }
} }
@ -296,7 +296,7 @@ static void deleteEmptyDirectoriesInDirectory(Directory * directory)
deleteEmptyDirectoriesInDirectory(subDir); deleteEmptyDirectoriesInDirectory(subDir);
nextNode = node->nextNode; nextNode = node->nextNode;
if (subDir->subDirectories->numberOfNodes == 0 && if (subDir->subDirectories->numberOfNodes == 0 &&
subDir->songs->numberOfNodes == 0) { subDir->songs.nr == 0) {
deleteNodeFromList(directory->subDirectories, node); deleteNodeFromList(directory->subDirectories, node);
} }
node = nextNode; node = nextNode;
@ -311,7 +311,7 @@ static void deleteEmptyDirectoriesInDirectory(Directory * directory)
static int updateInDirectory(Directory * directory, static int updateInDirectory(Directory * directory,
const char *shortname, const char *name) const char *shortname, const char *name)
{ {
void *song; Song *song;
void *subDir; void *subDir;
struct stat st; struct stat st;
@ -319,14 +319,13 @@ static int updateInDirectory(Directory * directory,
return -1; return -1;
if (S_ISREG(st.st_mode) && hasMusicSuffix(name, 0)) { if (S_ISREG(st.st_mode) && hasMusicSuffix(name, 0)) {
if (0 == findInList(directory->songs, shortname, &song)) { if (!(song = songvec_find(&directory->songs, shortname))) {
addToDirectory(directory, shortname, name); addToDirectory(directory, shortname, name);
return DIRECTORY_RETURN_UPDATE; return DIRECTORY_RETURN_UPDATE;
} else if (st.st_mtime != ((Song *) song)->mtime) { } else if (st.st_mtime != song->mtime) {
LOG("updating %s\n", name); LOG("updating %s\n", name);
if (updateSongInfo((Song *) song) < 0) { if (updateSongInfo(song) < 0)
removeSongFromDirectory(directory, shortname); removeSongFromDirectory(directory, shortname);
}
return 1; return 1;
} }
} else if (S_ISDIR(st.st_mode)) { } else if (S_ISDIR(st.st_mode)) {
@ -361,6 +360,8 @@ static int removeDeletedFromDirectory(char *path_max_tmp, Directory * directory)
ListNode *node, *tmpNode; ListNode *node, *tmpNode;
DirectoryList *subdirs = directory->subDirectories; DirectoryList *subdirs = directory->subDirectories;
int ret = 0; int ret = 0;
int i;
struct songvec *sv = &directory->songs;
node = subdirs->firstNode; node = subdirs->firstNode;
while (node) { while (node) {
@ -381,23 +382,21 @@ static int removeDeletedFromDirectory(char *path_max_tmp, Directory * directory)
node = tmpNode; node = tmpNode;
} }
node = directory->songs->firstNode; for (i = sv->nr; --i >= 0; ) { /* cleaner deletes if we go backwards */
while (node) { Song *song = sv->base[i];
tmpNode = node->nextNode; if (!song || !song->url)
if (node->key) { continue; /* does this happen?, perhaps assert() */
if (dirname) if (dirname)
sprintf(path_max_tmp, "%s/%s", dirname, sprintf(path_max_tmp, "%s/%s", dirname, song->url);
node->key);
else else
strcpy(path_max_tmp, node->key); strcpy(path_max_tmp, song->url);
if (!isFile(path_max_tmp, NULL)) { if (!isFile(path_max_tmp, NULL)) {
removeSongFromDirectory(directory, node->key); removeSongFromDirectory(directory, song->url);
ret = 1; ret = 1;
} }
} }
node = tmpNode;
}
return ret; return ret;
} }
@ -721,12 +720,12 @@ static int addToDirectory(Directory * directory,
return -1; return -1;
} }
if (S_ISREG(st.st_mode) && hasMusicSuffix(name, 0)) { if (S_ISREG(st.st_mode) &&
Song *song; hasMusicSuffix(name, 0) && isMusic(name, NULL, 0)) {
song = addSongToList(directory->songs, shortname, name, Song *song = newSong(shortname, SONG_TYPE_FILE, directory);
SONG_TYPE_FILE, directory);
if (!song) if (!song)
return -1; return -1;
songvec_add(&directory->songs, song);
LOG("added %s\n", name); LOG("added %s\n", name);
return 1; return 1;
} else if (S_ISDIR(st.st_mode)) { } else if (S_ISDIR(st.st_mode)) {
@ -834,7 +833,7 @@ int printDirectoryInfo(struct client *client, const char *name)
return -1; return -1;
printDirectoryList(client, directory->subDirectories); printDirectoryList(client, directory->subDirectories);
printSongInfoFromList(client, directory->songs); songvec_print(client, &directory->songs);
return 0; return 0;
} }
@ -865,7 +864,7 @@ static void writeDirectoryInfo(FILE * fp, Directory * directory)
node = node->nextNode; node = node->nextNode;
} }
writeSongInfoFromList(fp, directory->songs); songvec_save(fp, &directory->songs);
if (directory->path) { if (directory->path) {
retv = fprintf(fp, "%s%s\n", DIRECTORY_END, retv = fprintf(fp, "%s%s\n", DIRECTORY_END,
@ -929,7 +928,7 @@ static void readDirectoryInfo(FILE * fp, Directory * directory)
readDirectoryInfo(fp, subDirectory); readDirectoryInfo(fp, subDirectory);
} else if (!prefixcmp(buffer, SONG_BEGIN)) { } else if (!prefixcmp(buffer, SONG_BEGIN)) {
readSongInfoIntoList(fp, directory->songs, directory); readSongInfoIntoList(fp, &directory->songs, directory);
} else { } else {
FATAL("Unknown line in db: %s\n", buffer); FATAL("Unknown line in db: %s\n", buffer);
} }
@ -948,7 +947,7 @@ static void sortDirectory(Directory * directory)
Directory *subDir; Directory *subDir;
sortList(directory->subDirectories); sortList(directory->subDirectories);
sortList(directory->songs); songvec_sort(&directory->songs);
while (node != NULL) { while (node != NULL) {
subDir = (Directory *) node->data; subDir = (Directory *) node->data;
@ -1140,8 +1139,7 @@ static int traverseAllInSubDirectory(Directory * directory,
int (*forEachDir) (Directory *, void *), int (*forEachDir) (Directory *, void *),
void *data) void *data)
{ {
ListNode *node = directory->songs->firstNode; ListNode *node;
Song *song;
Directory *dir; Directory *dir;
int errFlag = 0; int errFlag = 0;
@ -1152,14 +1150,16 @@ static int traverseAllInSubDirectory(Directory * directory,
} }
if (forEachSong) { if (forEachSong) {
while (node != NULL && !errFlag) { int i;
song = (Song *) node->data; struct songvec *sv = &directory->songs;
errFlag = forEachSong(song, data); Song **sp = sv->base;
node = node->nextNode;
} for (i = sv->nr; --i >= 0; ) {
if (errFlag) Song *song = *sp++;
if ((errFlag = forEachSong(song, data)))
return errFlag; return errFlag;
} }
}
node = directory->subDirectories->firstNode; node = directory->subDirectories->firstNode;
@ -1202,7 +1202,7 @@ void initMp3Directory(void)
static Song *getSongDetails(const char *file, const char **shortnameRet, static Song *getSongDetails(const char *file, const char **shortnameRet,
Directory ** directoryRet) Directory ** directoryRet)
{ {
void *song = NULL; Song *song;
Directory *directory; Directory *directory;
char *dir = NULL; char *dir = NULL;
char *duplicated = xstrdup(file); char *duplicated = xstrdup(file);
@ -1229,7 +1229,7 @@ static Song *getSongDetails(const char *file, const char **shortnameRet,
return NULL; return NULL;
} }
if (!findInList(directory->songs, shortname, &song)) { if (!(song = songvec_find(&directory->songs, shortname))) {
free(duplicated); free(duplicated);
return NULL; return NULL;
} }
@ -1239,7 +1239,7 @@ static Song *getSongDetails(const char *file, const char **shortnameRet,
*shortnameRet = shortname; *shortnameRet = shortname;
if (directoryRet) if (directoryRet)
*directoryRet = directory; *directoryRet = directory;
return (Song *) song; return song;
} }
Song *getSongFromDB(const char *file) Song *getSongFromDB(const char *file)

View File

@ -20,13 +20,14 @@
#define DIRECTORY_H #define DIRECTORY_H
#include "song.h" #include "song.h"
#include "songvec.h"
typedef List DirectoryList; typedef List DirectoryList;
typedef struct _Directory { typedef struct _Directory {
char *path; char *path;
DirectoryList *subDirectories; DirectoryList *subDirectories;
SongList *songs; struct songvec songs;
struct _Directory *parent; struct _Directory *parent;
ino_t inode; ino_t inode;
dev_t device; dev_t device;

View File

@ -25,6 +25,7 @@
#include "playlist.h" #include "playlist.h"
#include "decoder_list.h" #include "decoder_list.h"
#include "decoder_api.h" #include "decoder_api.h"
#include "os_compat.h" #include "os_compat.h"
Song *newNullSong(void) Song *newNullSong(void)

View File

@ -17,6 +17,7 @@
*/ */
#include "song_print.h" #include "song_print.h"
#include "songvec.h"
#include "directory.h" #include "directory.h"
#include "tag_print.h" #include "tag_print.h"
#include "client.h" #include "client.h"
@ -41,14 +42,14 @@ int printSongInfo(struct client *client, Song * song)
return 0; return 0;
} }
int printSongInfoFromList(struct client *client, SongList * list) int songvec_print(struct client *client, const struct songvec *sv)
{ {
ListNode *tempNode = list->firstNode; int i;
Song **sp = sv->base;
while (tempNode != NULL) { for (i = sv->nr; --i >= 0;)
printSongInfo(client, (Song *) tempNode->data); if (printSongInfo(client, *sp++) < 0)
tempNode = tempNode->nextNode; return -1;
}
return 0; return 0;
} }

View File

@ -21,9 +21,11 @@
#include "song.h" #include "song.h"
struct songvec;
int printSongInfo(struct client *client, Song * song); int printSongInfo(struct client *client, Song * song);
int printSongInfoFromList(struct client *client, SongList * list); int songvec_print(struct client *client, const struct songvec *sv);
void printSongUrl(struct client *client, Song * song); void printSongUrl(struct client *client, Song * song);

View File

@ -45,52 +45,41 @@ static void song_save(FILE *fp, Song * song)
tag_save(fp, song->tag); tag_save(fp, song->tag);
} }
void writeSongInfoFromList(FILE * fp, SongList * list) void songvec_save(FILE *fp, struct songvec *sv)
{ {
ListNode *tempNode = list->firstNode; int i;
Song **sp = sv->base;
fprintf(fp, "%s\n", SONG_BEGIN); fprintf(fp, "%s\n", SONG_BEGIN);
while (tempNode != NULL) { for (i = sv->nr; --i >= 0; ) {
fprintf(fp, "%s%s\n", SONG_KEY, tempNode->key); Song *song = *sp++;
song_save(fp, (Song *) tempNode->data); fprintf(fp, "%s%s\n", SONG_KEY, song->url);
fprintf(fp, "%s%li\n", SONG_MTIME, song_save(fp, song);
(long)((Song *) tempNode->data)->mtime); fprintf(fp, "%s%li\n", SONG_MTIME, (long)song->mtime);
tempNode = tempNode->nextNode;
} }
fprintf(fp, "%s\n", SONG_END); fprintf(fp, "%s\n", SONG_END);
} }
static void insertSongIntoList(SongList * list, ListNode ** nextSongNode, static void insertSongIntoList(struct songvec *sv, Song *newsong)
char *key, Song * song)
{ {
ListNode *nodeTemp; Song *existing = songvec_find(sv, newsong->url);
int cmpRet = 0;
while (*nextSongNode if (!existing) {
&& (cmpRet = strcmp(key, (*nextSongNode)->key)) > 0) { songvec_add(sv, newsong);
nodeTemp = (*nextSongNode)->nextNode; if (newsong->tag)
deleteNodeFromList(list, *nextSongNode); tag_end_add(newsong->tag);
*nextSongNode = nodeTemp; } else { /* prevent dupes, just update the existing song info */
if (existing->mtime != newsong->mtime) {
tag_free(existing->tag);
if (newsong->tag)
tag_end_add(newsong->tag);
existing->tag = newsong->tag;
existing->mtime = newsong->mtime;
newsong->tag = NULL;
} }
freeJustSong(newsong);
if (!(*nextSongNode)) {
insertInList(list, song->url, (void *)song);
} else if (cmpRet == 0) {
Song *tempSong = (Song *) ((*nextSongNode)->data);
if (tempSong->mtime != song->mtime) {
tag_free(tempSong->tag);
tag_end_add(song->tag);
tempSong->tag = song->tag;
tempSong->mtime = song->mtime;
song->tag = NULL;
}
freeJustSong(song);
*nextSongNode = (*nextSongNode)->nextNode;
} else {
insertInListBeforeNode(list, *nextSongNode, -1, song->url,
(void *)song);
} }
} }
@ -109,23 +98,18 @@ static int matchesAnMpdTagItemKey(char *buffer, int *itemType)
return 0; return 0;
} }
void readSongInfoIntoList(FILE * fp, SongList * list, Directory * parentDir) void readSongInfoIntoList(FILE *fp, struct songvec *sv,
Directory *parentDir)
{ {
char buffer[MPD_PATH_MAX + 1024]; char buffer[MPD_PATH_MAX + 1024];
int bufferSize = MPD_PATH_MAX + 1024; int bufferSize = MPD_PATH_MAX + 1024;
Song *song = NULL; Song *song = NULL;
ListNode *nextSongNode = list->firstNode;
ListNode *nodeTemp;
int itemType; int itemType;
while (myFgets(buffer, bufferSize, fp) && 0 != strcmp(SONG_END, buffer)) { while (myFgets(buffer, bufferSize, fp) && 0 != strcmp(SONG_END, buffer)) {
if (0 == strncmp(SONG_KEY, buffer, strlen(SONG_KEY))) { if (0 == strncmp(SONG_KEY, buffer, strlen(SONG_KEY))) {
if (song) { if (song)
insertSongIntoList(list, &nextSongNode, insertSongIntoList(sv, song);
song->url, song);
if (song->tag != NULL)
tag_end_add(song->tag);
}
song = newNullSong(); song = newNullSong();
song->url = xstrdup(buffer + strlen(SONG_KEY)); song->url = xstrdup(buffer + strlen(SONG_KEY));
@ -163,15 +147,8 @@ void readSongInfoIntoList(FILE * fp, SongList * list, Directory * parentDir)
FATAL("songinfo: unknown line in db: %s\n", buffer); FATAL("songinfo: unknown line in db: %s\n", buffer);
} }
if (song) { if (song)
insertSongIntoList(list, &nextSongNode, song->url, song); insertSongIntoList(sv, song);
if (song->tag != NULL)
tag_end_add(song->tag);
}
while (nextSongNode) { songvec_prune(sv);
nodeTemp = nextSongNode->nextNode;
deleteNodeFromList(list, nextSongNode);
nextSongNode = nodeTemp;
}
} }

View File

@ -21,9 +21,11 @@
#include "song.h" #include "song.h"
void writeSongInfoFromList(FILE * fp, SongList * list); struct songvec;
void readSongInfoIntoList(FILE * fp, SongList * list, void songvec_save(FILE *fp, struct songvec *sv);
void readSongInfoIntoList(FILE * fp, struct songvec *sv,
struct _Directory *parent); struct _Directory *parent);
#endif #endif

89
src/songvec.c Normal file
View File

@ -0,0 +1,89 @@
#include "songvec.h"
#include "utils.h"
/* Only used for sorting/searchin a songvec, not general purpose compares */
static int songvec_cmp(const void *s1, const void *s2)
{
const Song *a = ((const Song * const *)s1)[0];
const Song *b = ((const Song * const *)s2)[0];
return strcmp(a->url, b->url);
}
static size_t sv_size(struct songvec *sv)
{
return sv->nr * sizeof(Song *);
}
void songvec_sort(struct songvec *sv)
{
qsort(sv->base, sv->nr, sizeof(Song *), songvec_cmp);
}
Song *songvec_find(struct songvec *sv, const char *url)
{
int i;
for (i = sv->nr; --i >= 0; )
if (!strcmp(sv->base[i]->url, url))
return sv->base[i];
return NULL;
}
int songvec_delete(struct songvec *sv, Song *del)
{
int i;
for (i = sv->nr; --i >= 0; ) {
if (sv->base[i] != del)
continue;
/* we _don't_ call freeSong() here */
if (!--sv->nr) {
free(sv->base);
sv->base = NULL;
} else {
memmove(&sv->base[i], &sv->base[i + 1],
(sv->nr - i + 1) * sizeof(Song *));
sv->base = xrealloc(sv->base, sv_size(sv));
}
return i;
}
return -1; /* not found */
}
void songvec_add(struct songvec *sv, Song *add)
{
++sv->nr;
sv->base = xrealloc(sv->base, sv_size(sv));
sv->base[sv->nr - 1] = add;
}
void songvec_free(struct songvec *sv)
{
free(sv->base);
sv->base = NULL;
sv->nr = 0;
}
/*
* Removes missing songs from a songvec. This function is only temporary
* as updating will be moved into a thread and updating shared memory...
*/
#include "path.h"
#include "ls.h"
void songvec_prune(struct songvec *sv)
{
int i;
char tmp[MPD_PATH_MAX];
struct stat sb;
for (i = sv->nr; --i >= 0; ) {
Song *song = sv->base[i];
assert(song);
if (!myStat(get_song_url(tmp, song), &sb))
continue;
songvec_delete(sv, song);
freeSong(song);
i = sv->nr;
}
}

24
src/songvec.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef SONGVEC_H
#define SONGVEC_H
#include "song.h"
#include "os_compat.h"
struct songvec {
Song **base;
size_t nr;
};
void songvec_sort(struct songvec *sv);
Song *songvec_find(struct songvec *sv, const char *url);
int songvec_delete(struct songvec *sv, Song *del);
void songvec_add(struct songvec *sv, Song *add);
void songvec_free(struct songvec *sv);
void songvec_prune(struct songvec *sv);
#endif /* SONGVEC_H */