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

hoist boundary check code to the out of loop #10

Merged
merged 2 commits into from
Dec 3, 2014

Conversation

imasahiro
Copy link
Contributor

Current implementation of sprintf(..., "%x",...) always execute boundary check.
(e.g. https://github.com/h2o/qrintf/blob/master/share/qrintf/qrintf.h#L1321)

This pull request hoists this check to the out of loop to simplify main code of hexadecimal to string conversion.

@kazuho
Copy link
Member

kazuho commented Dec 2, 2014

Thank you for the PR.

I agree that boundary check should not be performed for every byte, I am basically fine to merge the changes. Before that, would you mind making following changes so that the diff would be minimal?

  • there is no need to introduce *p, IMO using ctx.str[ctx.off++] should be fine in terms of performance
    • the reasons why I believe so is:
    • both ctx.str and ctx.off would be stored on register since they are always passed-by-value
    • the expression can be compiled into movb %r1, (%r2,%r3) or something alike
    • ctx needs to be returned, and using the value to store both intermediate and final results will save the number of registers used
  • do not introduce wlen as well - if possible, please update len for the purpose in order to save the registers

It would be great if you could make such changes so that the diff of gets limited to the topic of the branch (i.e. only change the _qrintf_chk_?x functions).

I would appreciate it if you could either open a separate PR or a separate commit within this PR to change other things in case you are convinced that my understanding described in the above listing is wrong.

@imasahiro
Copy link
Contributor Author

@kazuho
Thanks for the feedback. I updated the branch with the suggested changes:

  • remove *p and use ctx.str[off++] in _qrintf_chk_?x, use ctx.str[ctx.off++] in _qrintf_nck_?x.
  • remove wlen

@kazuho
Copy link
Member

kazuho commented Dec 2, 2014

Thanks for the changes. I think we are almost complete. Please consider the nit-picking comments on the commit.

@imasahiro
Copy link
Contributor Author

Thank you for your comment.

But we cannot modify the code as above because it breaks a compatibility with snprintf.
Acording to man page of printf(http://linux.die.net/man/3/sprintf), when the
buffer does not have enough space to write, the return value of the snprintf
is the number of characters which would have been written to the final string
if enough space has been available.

Return Value
...
If the output was truncated due to this limit then the return value
is the number of characters (excluding the terminating null byte)
which would have been written to the final string if enough space
had been available.

@imasahiro
Copy link
Contributor Author

I tried to reconsider about above comments and I updated the branch.

  • Remove local variable off
  • Introduce new local variable rest
  • Both qrintf_chk?x and qrintf_nck?x now use ctx.str[ctx.off++]

@kazuho
Copy link
Member

kazuho commented Dec 3, 2014

Hmm. Let me see. Thought that it was possible to not add any local vars...

@kazuho kazuho merged commit 008a174 into h2o:master Dec 3, 2014
kazuho added a commit that referenced this pull request Dec 3, 2014
@kazuho
Copy link
Member

kazuho commented Dec 3, 2014

Sorry for the fuss. The proposed code seems optimal. Merged to master with little tweaks.

@imasahiro imasahiro deleted the hoist_check branch December 3, 2014 06:15
@imasahiro
Copy link
Contributor Author

@kazuho
Thank you very much for your review and final merge.

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

Successfully merging this pull request may close these issues.

2 participants