Other: Use msgpack instead of json in vault

msgpack is much faster at serializing and deserializing than json. Using
it in the vault gives a performance boost.
This commit is contained in:
James Houlahan
2022-10-20 16:36:53 +02:00
parent c7a5b8559c
commit d376b88cf0
7 changed files with 75 additions and 10 deletions

View File

@ -168,13 +168,13 @@ func (vault *Vault) SetAutoUpdate(autoUpdate bool) error {
// GetLastVersion returns the last version of the bridge that was run.
func (vault *Vault) GetLastVersion() *semver.Version {
return vault.get().Settings.LastVersion
return semver.MustParse(vault.get().Settings.LastVersion)
}
// SetLastVersion sets the last version of the bridge that was run.
func (vault *Vault) SetLastVersion(version *semver.Version) error {
return vault.mod(func(data *Data) {
data.Settings.LastVersion = version
data.Settings.LastVersion = version.String()
})
}

View File

@ -20,7 +20,6 @@ package vault
import (
"math/rand"
"github.com/Masterminds/semver/v3"
"github.com/ProtonMail/gluon/imap"
"github.com/ProtonMail/proton-bridge/v2/internal/updater"
)
@ -58,7 +57,7 @@ type Settings struct {
Autostart bool
AutoUpdate bool
LastVersion *semver.Version
LastVersion string
FirstStart bool
FirstStartGUI bool
}
@ -81,7 +80,7 @@ func newDefaultSettings(gluonDir string) Settings {
Autostart: false,
AutoUpdate: true,
LastVersion: semver.MustParse("0.0.0"),
LastVersion: "0.0.0",
FirstStart: true,
FirstStartGUI: true,
}

View File

@ -22,7 +22,6 @@ import (
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"io/fs"
@ -32,6 +31,7 @@ import (
"github.com/ProtonMail/proton-bridge/v2/internal/certs"
"github.com/bradenaw/juniper/xslices"
"github.com/vmihailenco/msgpack/v5"
)
// Vault is an encrypted data vault that stores bridge and user data.
@ -253,7 +253,7 @@ func (vault *Vault) get() Data {
var data Data
if err := json.Unmarshal(dec, &data); err != nil {
if err := msgpack.Unmarshal(dec, &data); err != nil {
panic(err)
}
@ -271,13 +271,13 @@ func (vault *Vault) mod(fn func(data *Data)) error {
var data Data
if err := json.Unmarshal(dec, &data); err != nil {
if err := msgpack.Unmarshal(dec, &data); err != nil {
return err
}
fn(&data)
mod, err := json.Marshal(data)
mod, err := msgpack.Marshal(data)
if err != nil {
return err
}
@ -314,7 +314,7 @@ func initVault(path, gluonDir string, gcm cipher.AEAD) ([]byte, error) {
return nil, err
}
dec, err := json.Marshal(Data{
dec, err := msgpack.Marshal(Data{
Settings: newDefaultSettings(gluonDir),
Certs: Certs{

View File

@ -0,0 +1,58 @@
// Copyright (c) 2022 Proton AG
//
// This file is part of Proton Mail Bridge.
//
// Proton Mail Bridge 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 3 of the License, or
// (at your option) any later version.
//
// Proton Mail Bridge 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 Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
package vault_test
import (
"bytes"
"testing"
"github.com/ProtonMail/proton-bridge/v2/internal/vault"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
)
func BenchmarkVault(b *testing.B) {
vaultDir, gluonDir := b.TempDir(), b.TempDir()
// Create a new vault.
s, corrupt, err := vault.New(vaultDir, gluonDir, []byte("my secret key"))
require.NoError(b, err)
require.False(b, corrupt)
// Add 10kB of cookies to the vault.
require.NoError(b, s.SetCookies(bytes.Repeat([]byte("a"), 10_000)))
// Create 10 vault users.
for idx := 0; idx < 10; idx++ {
user, err := s.AddUser(uuid.NewString(), "username", "authUID", "authRef", []byte("keyPass"))
require.NoError(b, err)
require.NoError(b, user.SetKeyPass([]byte("new key pass")))
}
b.ResetTimer()
// Time how quickly we can iterate through the users and get their key pass and bridge pass.
for i := 0; i < b.N; i++ {
require.NoError(b, s.ForUser(func(user *vault.User) error {
require.NotEmpty(b, user.KeyPass())
require.NotEmpty(b, user.BridgePass())
return nil
}))
}
}