How does memchr() work under the hood?

后端 未结 5 1419
迷失自我
迷失自我 2020-12-31 07:59

Background: I\'m trying to create a pure D language implementation of functionality that\'s roughly equivalent to C\'s memchr but uses arrays and indices i

5条回答
  •  猫巷女王i
    2020-12-31 08:23

    memchr like memset and memcpy generally reduce to fairly small amount of machine code. You are unlikely to be able to reproduce that kind of speed without inlining similar assembly code. One major issue to consider in an implementation is data alignment.

    One generic technique you may be able to use is to insert a sentinel at the end of the string being searched, which guarantees that you will find it. It allows you to move the test for end of string from inside the loop, to after the loop.

提交回复
热议问题