db/simple/Song: convert "uri" to a std::string

No longer allocate it as a "VarSize".  This used to be a clever trick
to save memory 10 years ago, but these days, keeping the code
maintainable seems more important than saving a few kilobytes of
memory.
This commit is contained in:
Max Kellermann 2019-09-03 20:23:46 +02:00
parent af3f637d3f
commit 7f9a8b8748
10 changed files with 17 additions and 78 deletions

View File

@ -52,7 +52,7 @@ range_save(BufferedOutputStream &os, unsigned start_ms, unsigned end_ms)
void void
song_save(BufferedOutputStream &os, const Song &song) song_save(BufferedOutputStream &os, const Song &song)
{ {
os.Format(SONG_BEGIN "%s\n", song.uri); os.Format(SONG_BEGIN "%s\n", song.uri.c_str());
range_save(os, song.start_time.ToMS(), song.end_time.ToMS()); range_save(os, song.start_time.ToMS(), song.end_time.ToMS());

View File

@ -20,7 +20,6 @@
#include "Directory.hxx" #include "Directory.hxx"
#include "SongSort.hxx" #include "SongSort.hxx"
#include "Song.hxx" #include "Song.hxx"
#include "Disposer.hxx"
#include "Mount.hxx" #include "Mount.hxx"
#include "db/LightDirectory.hxx" #include "db/LightDirectory.hxx"
#include "song/LightSong.hxx" #include "song/LightSong.hxx"
@ -51,7 +50,7 @@ Directory::~Directory() noexcept
mounted_database.reset(); mounted_database.reset();
} }
songs.clear_and_dispose(SongDisposer()); songs.clear_and_dispose(DeleteDisposer());
children.clear_and_dispose(DeleteDisposer()); children.clear_and_dispose(DeleteDisposer());
} }
@ -192,7 +191,7 @@ Directory::FindSong(const char *name_utf8) const noexcept
for (auto &song : songs) { for (auto &song : songs) {
assert(song.parent == this); assert(song.parent == this);
if (strcmp(song.uri, name_utf8) == 0) if (song.uri == name_utf8)
return &song; return &song;
} }

View File

@ -1,29 +0,0 @@
/*
* Copyright 2003-2019 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_SONG_DISPOSER_HXX
#define MPD_SONG_DISPOSER_HXX
struct Song;
struct SongDisposer {
void operator()(Song *song) const noexcept;
};
#endif

View File

@ -20,10 +20,10 @@
#ifndef MPD_SONG_PTR_HXX #ifndef MPD_SONG_PTR_HXX
#define MPD_SONG_PTR_HXX #define MPD_SONG_PTR_HXX
#include "Disposer.hxx"
#include <memory> #include <memory>
using SongPtr = std::unique_ptr<Song, SongDisposer>; struct Song;
using SongPtr = std::unique_ptr<Song>;
#endif #endif

View File

@ -18,36 +18,22 @@
*/ */
#include "Song.hxx" #include "Song.hxx"
#include "Disposer.hxx"
#include "Directory.hxx" #include "Directory.hxx"
#include "tag/Tag.hxx" #include "tag/Tag.hxx"
#include "util/VarSize.hxx"
#include "song/DetachedSong.hxx" #include "song/DetachedSong.hxx"
#include "song/LightSong.hxx" #include "song/LightSong.hxx"
#include "util/StringView.hxx" #include "util/StringView.hxx"
#include <assert.h>
#include <string.h>
inline inline
Song::Song(StringView _uri, Directory &_parent) noexcept Song::Song(StringView _uri, Directory &_parent) noexcept
:parent(&_parent) :parent(&_parent), uri(_uri.data, _uri.size)
{
memcpy(uri, _uri.data, _uri.size + 1);
}
inline
Song::~Song() noexcept
{ {
} }
static SongPtr static SongPtr
song_alloc(StringView uri, Directory &parent) noexcept song_alloc(StringView uri, Directory &parent) noexcept
{ {
auto *song = NewVarSize<Song>(sizeof(Song::uri), return std::make_unique<Song>(uri, parent);
uri.size + 1,
uri, parent);
return SongPtr(song);
} }
SongPtr SongPtr
@ -67,30 +53,16 @@ Song::NewFile(const char *path, Directory &parent) noexcept
return SongPtr(song_alloc(path, parent)); return SongPtr(song_alloc(path, parent));
} }
void
Song::Free() noexcept
{
DeleteVarSize(this);
}
void
SongDisposer::operator()(Song *song) const noexcept
{
song->Free();
}
std::string std::string
Song::GetURI() const noexcept Song::GetURI() const noexcept
{ {
assert(*uri);
if (parent->IsRoot()) if (parent->IsRoot())
return std::string(uri); return uri;
else { else {
const char *path = parent->GetPath(); const char *path = parent->GetPath();
std::string result; std::string result;
result.reserve(strlen(path) + 1 + strlen(uri)); result.reserve(strlen(path) + 1 + uri.length());
result.assign(path); result.assign(path);
result.push_back('/'); result.push_back('/');
result.append(uri); result.append(uri);
@ -101,7 +73,7 @@ Song::GetURI() const noexcept
LightSong LightSong
Song::Export() const noexcept Song::Export() const noexcept
{ {
LightSong dest(uri, tag); LightSong dest(uri.c_str(), tag);
dest.directory = parent->IsRoot() dest.directory = parent->IsRoot()
? nullptr : parent->GetPath(); ? nullptr : parent->GetPath();
dest.real_uri = nullptr; dest.real_uri = nullptr;

View File

@ -92,10 +92,9 @@ struct Song {
/** /**
* The file name. * The file name.
*/ */
char uri[sizeof(int)]; std::string uri;
Song(StringView _uri, Directory &parent) noexcept; Song(StringView _uri, Directory &parent) noexcept;
~Song() noexcept;
static SongPtr NewFrom(DetachedSong &&other, Directory &parent) noexcept; static SongPtr NewFrom(DetachedSong &&other, Directory &parent) noexcept;
@ -115,8 +114,6 @@ struct Song {
static SongPtr LoadFile(Storage &storage, const char *name_utf8, static SongPtr LoadFile(Storage &storage, const char *name_utf8,
Directory &parent); Directory &parent);
void Free() noexcept;
/** /**
* Throws on error. * Throws on error.
* *

View File

@ -96,7 +96,7 @@ song_cmp(const Song &a, const Song &b) noexcept
return ret < 0; return ret < 0;
/* still no difference? compare file name */ /* still no difference? compare file name */
return IcuCollate(a.uri, b.uri) < 0; return IcuCollate(a.uri.c_str(), b.uri.c_str()) < 0;
} }
void void

View File

@ -75,7 +75,7 @@ UpdateWalk::UpdateContainerFile(Directory &directory,
song->mtime = info.mtime; song->mtime = info.mtime;
FormatDefault(update_domain, "added %s/%s", FormatDefault(update_domain, "added %s/%s",
contdir->GetPath(), song->uri); contdir->GetPath(), song->uri.c_str());
{ {
const ScopeDatabaseLock protect; const ScopeDatabaseLock protect;

View File

@ -41,7 +41,7 @@ DatabaseEditor::DeleteSong(Directory &dir, Song *del)
remove.Remove(del->GetURI()); remove.Remove(del->GetURI());
/* finally, all possible references gone, free it */ /* finally, all possible references gone, free it */
del->Free(); delete del;
} }
void void

View File

@ -81,7 +81,7 @@ UpdateWalk::RemoveExcludedFromDirectory(Directory &directory,
directory.ForEachSongSafe([&](Song &song){ directory.ForEachSongSafe([&](Song &song){
assert(song.parent == &directory); assert(song.parent == &directory);
const auto name_fs = AllocatedPath::FromUTF8(song.uri); const auto name_fs = AllocatedPath::FromUTF8(song.uri.c_str());
if (name_fs.IsNull() || exclude_list.Check(name_fs)) { if (name_fs.IsNull() || exclude_list.Check(name_fs)) {
editor.DeleteSong(directory, &song); editor.DeleteSong(directory, &song);
modified = true; modified = true;
@ -103,7 +103,7 @@ UpdateWalk::PurgeDeletedFromDirectory(Directory &directory) noexcept
directory.ForEachSongSafe([&](Song &song){ directory.ForEachSongSafe([&](Song &song){
if (!directory_child_is_regular(storage, directory, if (!directory_child_is_regular(storage, directory,
song.uri)) { song.uri.c_str())) {
editor.LockDeleteSong(directory, &song); editor.LockDeleteSong(directory, &song);
modified = true; modified = true;