Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Efficient lookup by zero-terminated C string #87

Open
gsnoff opened this issue Jun 22, 2016 · 4 comments
Open

Efficient lookup by zero-terminated C string #87

gsnoff opened this issue Jun 22, 2016 · 4 comments

Comments

@gsnoff
Copy link

gsnoff commented Jun 22, 2016

In some use cases (like returning a pointer by a symbol in FFI scenario) it is needed to look up a value by a given *const c_char, without assuming anything about the encoding except that a single NUL byte always ends the sequence. In this regard it could be similar to a binary lookup, but without having the length of the input sequence as an immediate value beforehand.

Is it reasonable to provide a specialized routine for incremental comparison to save cycles by not performing the length count in a separate pass?

@sfackler
Copy link
Collaborator

It seems like you can have the PhfHash implementation take care of looking for the null terminator as it's hashing?

@abonander
Copy link
Collaborator

We could implement PhfHash and FmtConst for CStr now.

@abonander
Copy link
Collaborator

Thinking about this more, I'm not sure if hiding the unsafety of looking for a \0 behind a raw pointer is worth saving an extra scan of the string since the initial length finding will bring it into cache anyway.

@gsnoff
Copy link
Author

gsnoff commented Jul 9, 2019

@abonander The unsafety can be dealt with in strncmp-esque manner, by having a wrapper type with both a c_char pointer and a maximum length, not actual length. (A slice might work here too, I guess? It’s the semantics of stopping at null character that’s important, anyway.)

And I think the caching might not always work as intended in low-end embedded environments such as Cortex-M0. Individual cycles can be pretty expensive there, especially in real-time tasks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants