Skip to content

Commit 789eb18

Browse files
committed
Improve decoding performance by always inlining Codec::index_of()
In the gaspardpetit/base64 test, perf showed a significant amount of time spent in that function, which seemed off because that function should not exist at all in a release build. Making sure it's inline reduces decoding time for Gaspard's 256 workload by almost 30% (from approx. 2.38 to 1.69 on my machine). It seems logical to assume that other codecs get a similar performance boost this way, so I'm applying the change everywhere. This puts performance characteristics around the same level of adamvr/arduino-base64 or the Manuel Martinez one (but without the latter's apparent setup overhead). Still not the best, but now definitely in the "fair" spectrum for decoding.
1 parent 56fccf7 commit 789eb18

File tree

8 files changed

+48
-48
lines changed

8 files changed

+48
-48
lines changed

cppcodec/base32_crockford.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@ static_assert(sizeof(base32_crockford_alphabet) == 32, "base32 alphabet must hav
4444
class base32_crockford_base
4545
{
4646
public:
47-
static inline constexpr bool generates_padding() { return false; }
48-
static inline constexpr bool requires_padding() { return false; }
49-
static inline constexpr bool is_padding_symbol(char /*c*/) { return false; }
47+
static CPPCODEC_ALWAYS_INLINE constexpr bool generates_padding() { return false; }
48+
static CPPCODEC_ALWAYS_INLINE constexpr bool requires_padding() { return false; }
49+
static CPPCODEC_ALWAYS_INLINE constexpr bool is_padding_symbol(char /*c*/) { return false; }
5050

51-
static inline constexpr char symbol(uint8_t index)
51+
static CPPCODEC_ALWAYS_INLINE constexpr char symbol(uint8_t index)
5252
{
5353
return base32_crockford_alphabet[index];
5454
}
5555

56-
static inline constexpr uint8_t index_of(char c)
56+
static CPPCODEC_ALWAYS_INLINE constexpr uint8_t index_of(char c)
5757
{
5858
return (c >= '0' && c <= '9') ? (c - '0')
5959
// upper-case letters
@@ -76,9 +76,9 @@ class base32_crockford_base
7676
: throw symbol_error(c);
7777
}
7878

79-
static inline constexpr bool should_ignore(uint8_t index) { return index == 253; }
80-
static inline constexpr bool is_special_character(uint8_t index) { return index > 32; }
81-
static inline constexpr bool is_eof(uint8_t index) { return index == 255; }
79+
static CPPCODEC_ALWAYS_INLINE constexpr bool should_ignore(uint8_t index) { return index == 253; }
80+
static CPPCODEC_ALWAYS_INLINE constexpr bool is_special_character(uint8_t index) { return index > 32; }
81+
static CPPCODEC_ALWAYS_INLINE constexpr bool is_eof(uint8_t index) { return index == 255; }
8282
};
8383

8484
// base32_crockford is a concatenative iterative (i.e. streaming) interpretation of Crockford base32.

cppcodec/base32_hex.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@ class base32_hex
4444
public:
4545
template <typename Codec> using codec_impl = stream_codec<Codec, base32_hex>;
4646

47-
static inline constexpr bool generates_padding() { return true; }
48-
static inline constexpr bool requires_padding() { return true; }
49-
static inline constexpr char padding_symbol() { return '='; }
47+
static CPPCODEC_ALWAYS_INLINE constexpr bool generates_padding() { return true; }
48+
static CPPCODEC_ALWAYS_INLINE constexpr bool requires_padding() { return true; }
49+
static CPPCODEC_ALWAYS_INLINE constexpr char padding_symbol() { return '='; }
5050

51-
static inline constexpr char symbol(uint8_t index)
51+
static CPPCODEC_ALWAYS_INLINE constexpr char symbol(uint8_t index)
5252
{
5353
return base32_hex_alphabet[index];
5454
}
5555

56-
static inline constexpr uint8_t index_of(char c)
56+
static CPPCODEC_ALWAYS_INLINE constexpr uint8_t index_of(char c)
5757
{
5858
return (c >= '0' && c <= '9') ? (c - '0')
5959
: (c >= 'A' && c <= 'V') ? (c - 'A' + 10)
@@ -64,10 +64,10 @@ class base32_hex
6464
}
6565

6666
// RFC4648 does not specify any whitespace being allowed in base32 encodings.
67-
static inline constexpr bool should_ignore(uint8_t /*index*/) { return false; }
68-
static inline constexpr bool is_special_character(uint8_t index) { return index > 32; }
69-
static inline constexpr bool is_padding_symbol(uint8_t index) { return index == 254; }
70-
static inline constexpr bool is_eof(uint8_t index) { return index == 255; }
67+
static CPPCODEC_ALWAYS_INLINE constexpr bool should_ignore(uint8_t /*index*/) { return false; }
68+
static CPPCODEC_ALWAYS_INLINE constexpr bool is_special_character(uint8_t index) { return index > 32; }
69+
static CPPCODEC_ALWAYS_INLINE constexpr bool is_padding_symbol(uint8_t index) { return index == 254; }
70+
static CPPCODEC_ALWAYS_INLINE constexpr bool is_eof(uint8_t index) { return index == 255; }
7171
};
7272

7373
} // namespace detail

cppcodec/base32_rfc4648.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@ class base32_rfc4648
4444
public:
4545
template <typename Codec> using codec_impl = stream_codec<Codec, base32_rfc4648>;
4646

47-
static inline constexpr bool generates_padding() { return true; }
48-
static inline constexpr bool requires_padding() { return true; }
49-
static inline constexpr char padding_symbol() { return '='; }
47+
static CPPCODEC_ALWAYS_INLINE constexpr bool generates_padding() { return true; }
48+
static CPPCODEC_ALWAYS_INLINE constexpr bool requires_padding() { return true; }
49+
static CPPCODEC_ALWAYS_INLINE constexpr char padding_symbol() { return '='; }
5050

51-
static inline constexpr char symbol(uint8_t index)
51+
static CPPCODEC_ALWAYS_INLINE constexpr char symbol(uint8_t index)
5252
{
5353
return base32_rfc4648_alphabet[index];
5454
}
5555

56-
static inline constexpr uint8_t index_of(char c)
56+
static CPPCODEC_ALWAYS_INLINE constexpr uint8_t index_of(char c)
5757
{
5858
return (c >= 'A' && c <= 'Z') ? (c - 'A')
5959
: (c >= '2' && c <= '7') ? (c - '2' + 26)
@@ -64,10 +64,10 @@ class base32_rfc4648
6464
}
6565

6666
// RFC4648 does not specify any whitespace being allowed in base32 encodings.
67-
static inline constexpr bool should_ignore(uint8_t /*index*/) { return false; }
68-
static inline constexpr bool is_special_character(uint8_t index) { return index > 32; }
69-
static inline constexpr bool is_padding_symbol(uint8_t index) { return index == 254; }
70-
static inline constexpr bool is_eof(uint8_t index) { return index == 255; }
67+
static CPPCODEC_ALWAYS_INLINE constexpr bool should_ignore(uint8_t /*index*/) { return false; }
68+
static CPPCODEC_ALWAYS_INLINE constexpr bool is_special_character(uint8_t index) { return index > 32; }
69+
static CPPCODEC_ALWAYS_INLINE constexpr bool is_padding_symbol(uint8_t index) { return index == 254; }
70+
static CPPCODEC_ALWAYS_INLINE constexpr bool is_eof(uint8_t index) { return index == 255; }
7171
};
7272

7373
} // namespace detail

cppcodec/base64_rfc4648.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@ class base64_rfc4648
4545
public:
4646
template <typename Codec> using codec_impl = stream_codec<Codec, base64_rfc4648>;
4747

48-
static inline constexpr bool generates_padding() { return true; }
49-
static inline constexpr bool requires_padding() { return true; }
50-
static inline constexpr char padding_symbol() { return '='; }
48+
static CPPCODEC_ALWAYS_INLINE constexpr bool generates_padding() { return true; }
49+
static CPPCODEC_ALWAYS_INLINE constexpr bool requires_padding() { return true; }
50+
static CPPCODEC_ALWAYS_INLINE constexpr char padding_symbol() { return '='; }
5151

52-
static inline constexpr char symbol(uint8_t index)
52+
static CPPCODEC_ALWAYS_INLINE constexpr char symbol(uint8_t index)
5353
{
5454
return base64_rfc4648_alphabet[index];
5555
}
5656

57-
static inline constexpr uint8_t index_of(char c)
57+
static CPPCODEC_ALWAYS_INLINE constexpr uint8_t index_of(char c)
5858
{
5959
return (c >= 'A' && c <= 'Z') ? (c - 'A')
6060
: (c >= 'a' && c <= 'z') ? (c - 'a' + 26)
@@ -67,10 +67,10 @@ class base64_rfc4648
6767
}
6868

6969
// RFC4648 does not specify any whitespace being allowed in base64 encodings.
70-
static inline constexpr bool should_ignore(uint8_t /*index*/) { return false; }
71-
static inline constexpr bool is_special_character(uint8_t index) { return index > 64; }
72-
static inline constexpr bool is_padding_symbol(uint8_t index) { return index == 254; }
73-
static inline constexpr bool is_eof(uint8_t index) { return index == 255; }
70+
static CPPCODEC_ALWAYS_INLINE constexpr bool should_ignore(uint8_t /*index*/) { return false; }
71+
static CPPCODEC_ALWAYS_INLINE constexpr bool is_special_character(uint8_t index) { return index > 64; }
72+
static CPPCODEC_ALWAYS_INLINE constexpr bool is_padding_symbol(uint8_t index) { return index == 254; }
73+
static CPPCODEC_ALWAYS_INLINE constexpr bool is_eof(uint8_t index) { return index == 255; }
7474
};
7575

7676
} // namespace detail

cppcodec/base64_url.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ class base64_url
4747
public:
4848
template <typename Codec> using codec_impl = stream_codec<Codec, base64_url>;
4949

50-
static inline constexpr bool generates_padding() { return true; }
51-
static inline constexpr bool requires_padding() { return true; }
52-
static inline constexpr char padding_symbol() { return '='; }
50+
static CPPCODEC_ALWAYS_INLINE constexpr bool generates_padding() { return true; }
51+
static CPPCODEC_ALWAYS_INLINE constexpr bool requires_padding() { return true; }
52+
static CPPCODEC_ALWAYS_INLINE constexpr char padding_symbol() { return '='; }
5353

54-
static inline constexpr char symbol(uint8_t index)
54+
static CPPCODEC_ALWAYS_INLINE constexpr char symbol(uint8_t index)
5555
{
5656
return base64_url_alphabet[index];
5757
}
5858

59-
static inline constexpr uint8_t index_of(char c)
59+
static CPPCODEC_ALWAYS_INLINE constexpr uint8_t index_of(char c)
6060
{
6161
return (c >= 'A' && c <= 'Z') ? (c - 'A')
6262
: (c >= 'a' && c <= 'z') ? (c - 'a' + 26)
@@ -69,10 +69,10 @@ class base64_url
6969
}
7070

7171
// RFC4648 does not specify any whitespace being allowed in base64 encodings.
72-
static inline constexpr bool should_ignore(uint8_t /*index*/) { return false; }
73-
static inline constexpr bool is_special_character(uint8_t index) { return index > 64; }
74-
static inline constexpr bool is_padding_symbol(uint8_t index) { return index == 254; }
75-
static inline constexpr bool is_eof(uint8_t index) { return index == 255; }
72+
static CPPCODEC_ALWAYS_INLINE constexpr bool should_ignore(uint8_t /*index*/) { return false; }
73+
static CPPCODEC_ALWAYS_INLINE constexpr bool is_special_character(uint8_t index) { return index > 64; }
74+
static CPPCODEC_ALWAYS_INLINE constexpr bool is_padding_symbol(uint8_t index) { return index == 254; }
75+
static CPPCODEC_ALWAYS_INLINE constexpr bool is_eof(uint8_t index) { return index == 255; }
7676
};
7777

7878
} // namespace detail

cppcodec/base64_url_unpadded.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ class base64_url_unpadded : public base64_url
3535
public:
3636
template <typename Codec> using codec_impl = stream_codec<Codec, base64_url_unpadded>;
3737

38-
static inline constexpr bool generates_padding() { return false; }
39-
static inline constexpr bool requires_padding() { return false; }
38+
static CPPCODEC_ALWAYS_INLINE constexpr bool generates_padding() { return false; }
39+
static CPPCODEC_ALWAYS_INLINE constexpr bool requires_padding() { return false; }
4040
};
4141

4242
} // namespace detail

cppcodec/hex_lower.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class hex_lower : public hex_base
4242
public:
4343
template <typename Codec> using codec_impl = stream_codec<Codec, hex_lower>;
4444

45-
static inline constexpr char symbol(uint8_t index)
45+
static CPPCODEC_ALWAYS_INLINE constexpr char symbol(uint8_t index)
4646
{
4747
return hex_lower_alphabet[index];
4848
}

cppcodec/hex_upper.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class hex_upper : public hex_base
4242
public:
4343
template <typename Codec> using codec_impl = stream_codec<Codec, hex_upper>;
4444

45-
static inline constexpr char symbol(uint8_t index)
45+
static CPPCODEC_ALWAYS_INLINE constexpr char symbol(uint8_t index)
4646
{
4747
return hex_upper_alphabet[index];
4848
}

0 commit comments

Comments
 (0)