Skip to content

Is the need_allocation comparison incorrect? #2476

@edbird

Description

@edbird

I am currently looking at the performance page.

which contains this function.

bool need_allocation(const char *buf, size_t len) {
  return ((reinterpret_cast<uintptr_t>(buf + len - 1) % page_size())
    + simdjson::SIMDJSON_PADDING > static_cast<uintptr_t>(page_size()));
}

I am writing some unit tests.

  • system_page_size is 4096 (4k)
  • buf starts at 0x0
  • len = 4033
  • SIMDJSON_PADDING = 64 bytes

The last address of such an array is 0 + 4033 - 1 = 4032.

Add the padding, we get 4096. 4096, if interpreted as a pointer to an address, is in the next page. So the function should return true.

Instead, it returns false.

In other words, the operator should be >= and not >.

One possible solution occurs to me. While SIMDJSON_PADDING is defined to be 64 bytes, the actual amount of padding required is only 63 bytes.

Why? Because the SIMD machine reads blocks of 64 bytes at a time, and if our array of data to be processed ends on the final byte before a new block of 64 starts, we do not need any additional padding. If the array ended on the first address of a new block of 64 bytes, then an additional 63 bytes would be required.

Can anyone confirm what is the expected behavior?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions