Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
5f3bda42
Commit
5f3bda42
authored
8 years ago
by
Damien George
Browse files
Options
Downloads
Patches
Plain Diff
py: If str/bytes hash is 0 then explicitly compute it.
parent
f127bef3
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
py/objstr.c
+6
-0
6 additions, 0 deletions
py/objstr.c
py/objstr.h
+1
-0
1 addition, 0 deletions
py/objstr.h
py/runtime.c
+4
-0
4 additions, 0 deletions
py/runtime.c
with
11 additions
and
0 deletions
py/objstr.c
+
6
−
0
View file @
5f3bda42
...
...
@@ -158,6 +158,9 @@ mp_obj_t mp_obj_str_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_
if
(
MP_OBJ_IS_TYPE
(
args
[
0
],
&
mp_type_bytes
))
{
GET_STR_DATA_LEN
(
args
[
0
],
str_data
,
str_len
);
GET_STR_HASH
(
args
[
0
],
str_hash
);
if
(
str_hash
==
0
)
{
str_hash
=
qstr_compute_hash
(
str_data
,
str_len
);
}
mp_obj_str_t
*
o
=
MP_OBJ_TO_PTR
(
mp_obj_new_str_of_type
(
type
,
NULL
,
str_len
));
o
->
data
=
str_data
;
o
->
hash
=
str_hash
;
...
...
@@ -191,6 +194,9 @@ STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, size
}
GET_STR_DATA_LEN
(
args
[
0
],
str_data
,
str_len
);
GET_STR_HASH
(
args
[
0
],
str_hash
);
if
(
str_hash
==
0
)
{
str_hash
=
qstr_compute_hash
(
str_data
,
str_len
);
}
mp_obj_str_t
*
o
=
MP_OBJ_TO_PTR
(
mp_obj_new_str_of_type
(
&
mp_type_bytes
,
NULL
,
str_len
));
o
->
data
=
str_data
;
o
->
hash
=
str_hash
;
...
...
This diff is collapsed.
Click to expand it.
py/objstr.h
+
1
−
0
View file @
5f3bda42
...
...
@@ -39,6 +39,7 @@ typedef struct _mp_obj_str_t {
#define MP_DEFINE_STR_OBJ(obj_name, str) mp_obj_str_t obj_name = {{&mp_type_str}, 0, sizeof(str) - 1, (const byte*)str}
// use this macro to extract the string hash
// warning: the hash can be 0, meaning invalid, and must then be explicitly computed from the data
#define GET_STR_HASH(str_obj_in, str_hash) \
mp_uint_t str_hash; if (MP_OBJ_IS_QSTR(str_obj_in)) \
{ str_hash = qstr_hash(MP_OBJ_QSTR_VALUE(str_obj_in)); } else { str_hash = ((mp_obj_str_t*)MP_OBJ_TO_PTR(str_obj_in))->hash; }
...
...
This diff is collapsed.
Click to expand it.
py/runtime.c
+
4
−
0
View file @
5f3bda42
...
...
@@ -217,6 +217,10 @@ mp_obj_t mp_unary_op(mp_uint_t op, mp_obj_t arg) {
}
else
if
(
op
==
MP_UNARY_OP_HASH
&&
MP_OBJ_IS_STR_OR_BYTES
(
arg
))
{
// fast path for hashing str/bytes
GET_STR_HASH
(
arg
,
h
);
if
(
h
==
0
)
{
GET_STR_DATA_LEN
(
arg
,
data
,
len
);
h
=
qstr_compute_hash
(
data
,
len
);
}
return
MP_OBJ_NEW_SMALL_INT
(
h
);
}
else
{
mp_obj_type_t
*
type
=
mp_obj_get_type
(
arg
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment