Skip to content

fix flatmap element space should align with usertype#2288

Merged
serverglen merged 1 commit into
apache:masterfrom
ehds:fix-flatmap-element-space-alignment
Jul 14, 2023
Merged

fix flatmap element space should align with usertype#2288
serverglen merged 1 commit into
apache:masterfrom
ehds:fix-flatmap-element-space-alignment

Conversation

@ehds

@ehds ehds commented Jun 26, 2023

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number:

Problem Summary:

FlatMapBucketElement 用于存放用户的 key 和 value,初始化 element 的空间(element_space) 地址要满足 Element 的对齐要求。

未修改前,可以看到 element_space 的起始地值,并未满足 Element 的对齐要求:

   249      struct Bucket {
   250          explicit Bucket(const _K& k) : next(NULL)
-> 251          { new (element_spaces) Element(k); }
...
(lldb) p &element_spaces
(char (*)[128]) $1 = 0x0000000000385428
(lldb) p alignof(Element)
(unsigned long) $2 = 16

5.3.4 New [expr.new]
[14] Note: when the allocation function returns a value other than null, it must be a pointer to a block of storage in which space for the object has been reserved. The block of storage is assumed to be appropriately aligned and of the requested size.

一些编译器在开启优化选项后,会根据数据的对齐字节,而选择更高效的指令,例如 movaps, 如果实际的数据未按照其对齐要求分配内存,则会导致错误发生。

What is changed and the side effects?

Changed:
采用 std::aligned_storage<sizeof(Element), alignof(Element)> 用于初始化 element_space.
Side effects:

  • Performance effects(性能影响):
  • Breaking backward compatibility(向后兼容性):

Check List:

  • Please make sure your changes are compilable(请确保你的更改可以通过编译).
  • When providing us with a new feature, it is best to add related tests(如果你向我们增加一个新的功能, 请添加相关测试).
  • Please follow Contributor Covenant Code of Conduct.(请遵循贡献者准则).

@wwbmmm

wwbmmm commented Jun 27, 2023

Copy link
Copy Markdown
Contributor

LGTM

1 similar comment
@serverglen

Copy link
Copy Markdown
Contributor

LGTM

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.

3 participants