Skip to content

Commit ac88e7f

Browse files
committed
Storable: use utf8_to_bytes_overwrite() if available
This is simpler and saves a malloc each time. Note that this code could use plain utf8_to_bytes() on older perls, but it is less convenient, so would require more code; I don't think the performance gain is worth it.
1 parent 87191dc commit ac88e7f

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

dist/Storable/Storable.xs

+18
Original file line numberDiff line numberDiff line change
@@ -2960,6 +2960,19 @@ static int store_hash(pTHX_ stcxt_t *cxt, HV *hv)
29602960
keyval = SvPV(key, keylen_tmp);
29612961
keylen = keylen_tmp;
29622962
if (SvUTF8(key)) {
2963+
2964+
#ifdef utf8_to_bytes_overwrite
2965+
2966+
/* If we are able to downgrade here; that means that we have a
2967+
* key which only had chars 0-255, but was utf8 encoded. */
2968+
if (utf8_to_bytes_overwrite( (U8**) &keyval, &keylen_tmp)) {
2969+
keylen = keylen_tmp;
2970+
flags |= SHV_K_WASUTF8;
2971+
}
2972+
else {
2973+
flags |= SHV_K_UTF8;
2974+
}
2975+
#else
29632976
const char *keysave = keyval;
29642977
bool is_utf8 = TRUE;
29652978

@@ -2982,6 +2995,7 @@ static int store_hash(pTHX_ stcxt_t *cxt, HV *hv)
29822995
to assign back to keylen. */
29832996
flags |= SHV_K_UTF8;
29842997
}
2998+
#endif
29852999
}
29863000

29873001
if (flagged_hash) {
@@ -3000,8 +3014,12 @@ static int store_hash(pTHX_ stcxt_t *cxt, HV *hv)
30003014
WLEN(keylen);
30013015
if (keylen)
30023016
WRITE(keyval, keylen);
3017+
3018+
#ifndef utf8_to_bytes_overwrite
3019+
30033020
if (flags & SHV_K_WASUTF8)
30043021
Safefree (keyval);
3022+
#endif
30053023
}
30063024

30073025
/*

dist/Storable/lib/Storable.pm

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ our @EXPORT_OK = qw(
3030
our ($canonical, $forgive_me);
3131

3232
BEGIN {
33-
our $VERSION = '3.35';
33+
our $VERSION = '3.36';
3434
}
3535

3636
our $recursion_limit;

0 commit comments

Comments
 (0)