[array] Optimize serializing copy()

This commit is contained in:
Behdad Esfahbod 2022-11-25 14:23:57 -07:00
parent 22990fca1d
commit e333223f26
2 changed files with 21 additions and 6 deletions

View File

@ -262,7 +262,9 @@ struct hb_array_t : hb_iter_with_fallback_t<hb_array_t<Type>, Type&>
void fini ()
{ hb_free ((void *) arrayZ); arrayZ = nullptr; length = 0; }
template <typename hb_serialize_context_t>
template <typename hb_serialize_context_t,
typename U = Type,
hb_enable_if (!(sizeof (U) < sizeof (long long) && hb_is_trivially_copy_assignable(Type)))>
hb_array_t copy (hb_serialize_context_t *c) const
{
TRACE_SERIALIZE (this);
@ -273,6 +275,18 @@ struct hb_array_t : hb_iter_with_fallback_t<hb_array_t<Type>, Type&>
return_trace (hb_array_t (out, length));
}
template <typename hb_serialize_context_t,
typename U = Type,
hb_enable_if (sizeof (U) < sizeof (long long) && hb_is_trivially_copy_assignable(Type))>
hb_array_t copy (hb_serialize_context_t *c) const
{
TRACE_SERIALIZE (this);
auto* out = c->start_embed (arrayZ);
if (unlikely (!c->extend_size (out, get_size ()))) return_trace (hb_array_t ());
hb_memcpy (out, arrayZ, length);
return_trace (hb_array_t (out, length));
}
template <typename hb_sanitize_context_t>
bool sanitize (hb_sanitize_context_t *c) const
{ return c->check_array (arrayZ, length); }

View File

@ -268,12 +268,13 @@ struct hb_vector_t
{
length = other.length;
#ifndef HB_OPTIMIZE_SIZE
if (sizeof (T) >= sizeof (long long))
/* This runs faster because of alignment. */
for (unsigned i = 0; i < length; i++)
arrayZ[i] = other.arrayZ[i];
#else
hb_memcpy ((void *) arrayZ, (const void *) other.arrayZ, length * item_size);
else
#endif
hb_memcpy ((void *) arrayZ, (const void *) other.arrayZ, length * item_size);
}
template <typename T = Type,
hb_enable_if (!hb_is_trivially_copyable (T) &&