From 4faf6d6f65244895cf3b9d6172540661eb70a9f9 Mon Sep 17 00:00:00 2001 From: Robert Stone Date: Fri, 8 Nov 2024 11:54:21 -0800 Subject: [PATCH] Pre-allocate memory for webservice responses --- .../vlc/webserver/RemoteAccessRouting.kt | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/application/webserver/src/main/java/org/videolan/vlc/webserver/RemoteAccessRouting.kt b/application/webserver/src/main/java/org/videolan/vlc/webserver/RemoteAccessRouting.kt index 842eb0ea4..3dad1d67d 100644 --- a/application/webserver/src/main/java/org/videolan/vlc/webserver/RemoteAccessRouting.kt +++ b/application/webserver/src/main/java/org/videolan/vlc/webserver/RemoteAccessRouting.kt @@ -432,7 +432,7 @@ fun Route.setupRouting(appContext: Context, scope: CoroutineScope) { return@get } - val list = ArrayList() + val list = ArrayList(videos.size) videos.forEach { video -> when (video) { is MediaWrapper->list.add(video.toPlayQueueItem()) @@ -494,7 +494,7 @@ fun Route.setupRouting(appContext: Context, scope: CoroutineScope) { } val albums = appContext.getFromMl { getAlbums(false, false) } - val list = ArrayList() + val list = ArrayList(albums.size) albums.forEach { album -> list.add(album.toPlayQueueItem()) } @@ -509,7 +509,7 @@ fun Route.setupRouting(appContext: Context, scope: CoroutineScope) { } val artists = appContext.getFromMl { getArtists(settings.getBoolean(KEY_ARTISTS_SHOW_ALL, false), false, false) } - val list = ArrayList() + val list = ArrayList(artists.size) artists.forEach { artist -> list.add(artist.toPlayQueueItem(appContext)) } @@ -524,7 +524,7 @@ fun Route.setupRouting(appContext: Context, scope: CoroutineScope) { } val tracks = appContext.getFromMl { getAudio(Medialibrary.SORT_DEFAULT, false, false, false) } - val list = ArrayList() + val list = ArrayList(tracks.size) tracks.forEach { track -> list.add(track.toPlayQueueItem(defaultArtist = appContext.getString(R.string.unknown_artist))) } @@ -539,7 +539,7 @@ fun Route.setupRouting(appContext: Context, scope: CoroutineScope) { } val genres = appContext.getFromMl { getGenres(false, false) } - val list = ArrayList() + val list = ArrayList(genres.size) genres.forEach { genre -> list.add(genre.toPlayQueueItem(appContext)) } @@ -556,7 +556,7 @@ fun Route.setupRouting(appContext: Context, scope: CoroutineScope) { val album = appContext.getFromMl { getAlbum(id) } - val list = ArrayList() + val list = ArrayList(album.tracksCount) album.tracks.forEach { track -> list.add(track.toPlayQueueItem(album.albumArtist)) } @@ -574,7 +574,7 @@ fun Route.setupRouting(appContext: Context, scope: CoroutineScope) { val genre = appContext.getFromMl { getGenre(id) } - val list = ArrayList() + val list = ArrayList(genre.tracksCount) genre.tracks.forEach { track -> list.add(track.toPlayQueueItem()) } @@ -592,7 +592,7 @@ fun Route.setupRouting(appContext: Context, scope: CoroutineScope) { val playlist = appContext.getFromMl { getPlaylist(id, false, false) } - val list = ArrayList() + val list = ArrayList(playlist.tracksCount) playlist.tracks.forEach { track -> list.add(track.toPlayQueueItem(defaultArtist = if (track.type == MediaWrapper.TYPE_AUDIO) appContext.getString(R.string.unknown_artist) else "").apply { if (track.type == MediaWrapper.TYPE_VIDEO) fileType = "video" @@ -694,7 +694,7 @@ fun Route.setupRouting(appContext: Context, scope: CoroutineScope) { val artist = appContext.getFromMl { getArtist(id) } - val list = ArrayList() + val list = ArrayList(artist.albumsCount) artist.albums.forEach { album -> list.add(album.toPlayQueueItem()) } @@ -710,7 +710,7 @@ fun Route.setupRouting(appContext: Context, scope: CoroutineScope) { } val playlists = appContext.getFromMl { getPlaylists(Playlist.Type.All, false) } - val list = ArrayList() + val list = ArrayList(playlists.size) playlists.forEach { playlist -> list.add(playlist.toPlayQueueItem(appContext)) } @@ -819,8 +819,9 @@ fun Route.setupRouting(appContext: Context, scope: CoroutineScope) { call.respond(HttpStatusCode.Forbidden) return@get } - val list = ArrayList() - RemoteAccessServer.getInstance(appContext).networkSharesLiveData.getList().forEachIndexed { index, mediaLibraryItem -> + val networkShares = RemoteAccessServer.getInstance(appContext).networkSharesLiveData.getList() + val list = ArrayList(networkShares.size) + networkShares.forEachIndexed { index, mediaLibraryItem -> list.add(RemoteAccessServer.PlayQueueItem(3000L + index, mediaLibraryItem.title, " ", 0, mediaLibraryItem.artworkMrl ?: "", false, "", (mediaLibraryItem as MediaWrapper).uri.toString(), true, favorite = mediaLibraryItem.isFavorite)) } @@ -831,7 +832,7 @@ fun Route.setupRouting(appContext: Context, scope: CoroutineScope) { val stream = appContext.getFromMl { history(Medialibrary.HISTORY_TYPE_NETWORK) } - val list = ArrayList() + val list = ArrayList(stream.size) stream.forEachIndexed { index, mediaLibraryItem -> list.add(RemoteAccessServer.PlayQueueItem(3000L + index, mediaLibraryItem.title, " ", 0, mediaLibraryItem.artworkMrl ?: "", false, "", (mediaLibraryItem as MediaWrapper).uri.toString(), true, favorite = mediaLibraryItem.isFavorite))