Repository: skyline-emu/skyline
File: app/src/main/cpp/skyline/services/account/IProfile.cpp (C++)

Code before:
        } accountProfileBase = {
            .uid = userId,
        };

        size_t usernameSize{std::min(accountProfileBase.nickname.size() - 1, (*state.settings->usernameValue).size())};
        std::memcpy(accountProfileBase.nickname.data(), (*state.settings->usernameValue).c_str(), usernameSize);

        response.Push(accountProfileBase);

        return {};
    }

    Result IProfile::GetImageSize(type::KSession &session, ipc::IpcRequest &request, ipc::IpcResponse &response) {
        std::shared_ptr<vfs::Backing> profileImageIcon{GetProfilePicture()};
        response.Push<u32>(profileImageIcon->size);

        return {};
    }

    Result IProfile::LoadImage(type::KSession &session, ipc::IpcRequest &request, ipc::IpcResponse &response) {
        std::shared_ptr<vfs::Backing> profileImageIcon{GetProfilePicture()};
        std::vector<char> profileImage(profileImageIcon->size);

        profileImageIcon->Read(skyline::span<char>(profileImage), 0);

        request.outputBuf.at(0).copy_from(profileImage);
        response.Push<u32>(profileImageIcon->size);

        return {};
    }

    std::shared_ptr<vfs::Backing> IProfile::GetProfilePicture(){
        const std::string profilePicturePath{*state.settings->profilePictureValue};
        int fd{open((profilePicturePath).c_str(), O_RDONLY)};
        if (fd < 0)
            return state.os->assetFileSystem->OpenFile("skyline-logo.jpeg");
        else
            return std::make_shared<vfs::OsBacking>(fd, true);
    }
}


Review comment (suggestion):
Rather than reading it into `profileImage` first then copying it into the output buffer, you can directly read it into `outputBuf`.

Code after:
            .uid = userId,
        };

        size_t usernameSize{std::min(accountProfileBase.nickname.size() - 1, (*state.settings->usernameValue).size())};
        std::memcpy(accountProfileBase.nickname.data(), (*state.settings->usernameValue).c_str(), usernameSize);

        response.Push(accountProfileBase);

        return {};
    }

    Result IProfile::GetImageSize(type::KSession &session, ipc::IpcRequest &request, ipc::IpcResponse &response) {
        std::shared_ptr<vfs::Backing> profileImageIcon{GetProfilePicture()};
        response.Push(static_cast<u32>(profileImageIcon->size));

        return {};
    }

    Result IProfile::LoadImage(type::KSession &session, ipc::IpcRequest &request, ipc::IpcResponse &response) {
        std::shared_ptr<vfs::Backing> profileImageIcon{GetProfilePicture()};

        profileImageIcon->Read(span(request.outputBuf.at(0)).first(profileImageIcon->size), 0);
        response.Push(static_cast<u32>(profileImageIcon->size));

        return {};
    }

    std::shared_ptr<vfs::Backing> IProfile::GetProfilePicture() {
        std::string profilePicturePath{*state.settings->profilePictureValue};
        int fd{open((profilePicturePath).c_str(), O_RDONLY)};
        if (fd < 0)
            // If we can't find the profile picture then just return the placeholder profile picture
            return state.os->assetFileSystem->OpenFile("profile_picture.jpeg");
        else
            return std::make_shared<vfs::OsBacking>(fd, true);
    }
}