grpc 1.73.0 → 1.74.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (499) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +38 -17
  3. data/include/grpc/create_channel_from_endpoint.h +54 -0
  4. data/include/grpc/credentials.h +11 -5
  5. data/include/grpc/event_engine/event_engine.h +74 -17
  6. data/include/grpc/grpc_posix.h +20 -1
  7. data/include/grpc/impl/channel_arg_names.h +2 -4
  8. data/include/grpc/module.modulemap +1 -0
  9. data/include/grpc/support/json.h +24 -0
  10. data/src/core/call/interception_chain.h +7 -11
  11. data/src/core/channelz/channel_trace.cc +213 -115
  12. data/src/core/channelz/channel_trace.h +380 -86
  13. data/src/core/channelz/channelz.cc +270 -181
  14. data/src/core/channelz/channelz.h +168 -55
  15. data/src/core/channelz/channelz_registry.cc +2 -1
  16. data/src/core/channelz/channelz_registry.h +24 -0
  17. data/src/core/channelz/property_list.cc +357 -0
  18. data/src/core/channelz/property_list.h +202 -0
  19. data/src/core/channelz/ztrace_collector.h +3 -2
  20. data/src/core/client_channel/backup_poller.cc +17 -2
  21. data/src/core/client_channel/client_channel.cc +17 -28
  22. data/src/core/client_channel/client_channel_filter.cc +19 -29
  23. data/src/core/client_channel/config_selector.h +8 -2
  24. data/src/core/client_channel/dynamic_filters.cc +5 -6
  25. data/src/core/client_channel/dynamic_filters.h +1 -1
  26. data/src/core/client_channel/global_subchannel_pool.cc +4 -1
  27. data/src/core/client_channel/retry_filter.cc +21 -27
  28. data/src/core/client_channel/retry_filter.h +10 -7
  29. data/src/core/client_channel/retry_filter_legacy_call_data.cc +5 -5
  30. data/src/core/client_channel/retry_filter_legacy_call_data.h +1 -1
  31. data/src/core/client_channel/retry_interceptor.cc +30 -44
  32. data/src/core/client_channel/retry_interceptor.h +18 -17
  33. data/src/core/client_channel/retry_throttle.cc +46 -61
  34. data/src/core/client_channel/retry_throttle.h +17 -39
  35. data/src/core/client_channel/subchannel.cc +43 -19
  36. data/src/core/client_channel/subchannel.h +8 -0
  37. data/src/core/config/config_vars.cc +2 -0
  38. data/src/core/config/core_configuration.cc +1 -0
  39. data/src/core/config/core_configuration.h +11 -0
  40. data/src/core/credentials/call/call_creds_registry.h +125 -0
  41. data/src/core/credentials/call/call_creds_registry_init.cc +91 -0
  42. data/src/core/credentials/call/gcp_service_account_identity/gcp_service_account_identity_credentials.cc +6 -48
  43. data/src/core/credentials/call/jwt_token_file/jwt_token_file_call_credentials.cc +86 -0
  44. data/src/core/credentials/call/jwt_token_file/jwt_token_file_call_credentials.h +74 -0
  45. data/src/core/credentials/call/jwt_util.cc +70 -0
  46. data/src/core/credentials/call/jwt_util.h +32 -0
  47. data/src/core/credentials/transport/channel_creds_registry_init.cc +1 -1
  48. data/src/core/credentials/transport/google_default/google_default_credentials.cc +72 -4
  49. data/src/core/credentials/transport/ssl/ssl_credentials.cc +0 -1
  50. data/src/core/credentials/transport/tls/load_system_roots_supported.cc +1 -0
  51. data/src/core/credentials/transport/xds/xds_credentials.cc +0 -3
  52. data/src/core/ext/filters/gcp_authentication/gcp_authentication_filter.cc +8 -8
  53. data/src/core/ext/filters/gcp_authentication/gcp_authentication_filter.h +16 -16
  54. data/src/core/ext/filters/http/client_authority_filter.cc +2 -4
  55. data/src/core/ext/filters/http/message_compress/compression_filter.h +25 -22
  56. data/src/core/ext/filters/http/server/http_server_filter.h +12 -11
  57. data/src/core/ext/transport/chttp2/client/chttp2_connector.cc +120 -35
  58. data/src/core/ext/transport/chttp2/server/chttp2_server.cc +6 -5
  59. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +162 -115
  60. data/src/core/ext/transport/chttp2/transport/chttp2_transport.h +0 -3
  61. data/src/core/ext/transport/chttp2/transport/decode_huff.cc +1239 -3514
  62. data/src/core/ext/transport/chttp2/transport/decode_huff.h +1008 -1486
  63. data/src/core/ext/transport/chttp2/transport/flow_control.h +22 -17
  64. data/src/core/ext/transport/chttp2/transport/frame.cc +10 -0
  65. data/src/core/ext/transport/chttp2/transport/frame.h +2 -2
  66. data/src/core/ext/transport/chttp2/transport/frame_data.cc +1 -1
  67. data/src/core/ext/transport/chttp2/transport/frame_settings.cc +7 -8
  68. data/src/core/ext/transport/chttp2/transport/frame_window_update.cc +4 -5
  69. data/src/core/ext/transport/chttp2/transport/header_assembler.h +299 -0
  70. data/src/core/ext/transport/chttp2/transport/hpack_parser.cc +1 -1
  71. data/src/core/ext/transport/chttp2/transport/hpack_parser_table.cc +11 -5
  72. data/src/core/ext/transport/chttp2/transport/hpack_parser_table.h +12 -1
  73. data/src/core/ext/transport/chttp2/transport/http2_client_transport.cc +1017 -0
  74. data/src/core/ext/transport/chttp2/transport/http2_client_transport.h +593 -0
  75. data/src/core/ext/transport/chttp2/transport/http2_settings.h +19 -22
  76. data/{third_party/abseil-cpp/absl/strings/cord_buffer.cc → src/core/ext/transport/chttp2/transport/http2_stats_collector.cc} +14 -14
  77. data/src/core/ext/transport/chttp2/transport/http2_stats_collector.h +33 -0
  78. data/src/core/ext/transport/chttp2/transport/http2_status.h +6 -1
  79. data/src/core/ext/transport/chttp2/transport/http2_transport.cc +43 -0
  80. data/src/core/ext/transport/chttp2/transport/http2_transport.h +65 -0
  81. data/src/core/ext/transport/chttp2/transport/http2_ztrace_collector.h +0 -29
  82. data/src/core/ext/transport/chttp2/transport/internal.h +18 -8
  83. data/src/core/ext/transport/chttp2/transport/keepalive.cc +105 -0
  84. data/src/core/ext/transport/chttp2/transport/keepalive.h +138 -0
  85. data/src/core/ext/transport/chttp2/transport/message_assembler.h +185 -0
  86. data/src/core/ext/transport/chttp2/transport/parsing.cc +2 -4
  87. data/src/core/ext/transport/chttp2/transport/ping_callbacks.h +19 -0
  88. data/src/core/ext/transport/chttp2/transport/ping_promise.cc +151 -0
  89. data/src/core/ext/transport/chttp2/transport/ping_promise.h +180 -0
  90. data/src/core/ext/transport/chttp2/transport/ping_rate_policy.cc +5 -9
  91. data/src/core/ext/transport/chttp2/transport/ping_rate_policy.h +11 -0
  92. data/src/core/ext/transport/chttp2/transport/stream_lists.cc +39 -1
  93. data/src/core/ext/transport/chttp2/transport/transport_common.cc +19 -0
  94. data/src/core/ext/transport/chttp2/transport/transport_common.h +27 -0
  95. data/src/core/ext/transport/chttp2/transport/writing.cc +37 -11
  96. data/src/core/ext/upb-gen/src/proto/grpc/channelz/v2/channelz.upb.h +571 -0
  97. data/src/core/ext/upb-gen/src/proto/grpc/channelz/v2/channelz.upb_minitable.c +120 -0
  98. data/src/core/ext/upb-gen/src/proto/grpc/channelz/v2/channelz.upb_minitable.h +36 -0
  99. data/src/core/ext/upb-gen/src/proto/grpc/channelz/v2/promise.upb.h +1272 -0
  100. data/src/core/ext/upb-gen/src/proto/grpc/channelz/v2/promise.upb_minitable.c +312 -0
  101. data/src/core/ext/upb-gen/src/proto/grpc/channelz/v2/promise.upb_minitable.h +50 -0
  102. data/src/core/ext/upb-gen/src/proto/grpc/channelz/v2/property_list.upb.h +984 -0
  103. data/src/core/ext/upb-gen/src/proto/grpc/channelz/v2/property_list.upb_minitable.c +226 -0
  104. data/src/core/ext/upb-gen/src/proto/grpc/channelz/v2/property_list.upb_minitable.h +44 -0
  105. data/src/core/ext/upbdefs-gen/src/proto/grpc/channelz/v2/promise.upbdefs.c +175 -0
  106. data/src/core/ext/upbdefs-gen/src/proto/grpc/channelz/v2/promise.upbdefs.h +82 -0
  107. data/src/core/ext/upbdefs-gen/src/proto/grpc/channelz/v2/property_list.upbdefs.c +135 -0
  108. data/src/core/ext/upbdefs-gen/src/proto/grpc/channelz/v2/property_list.upbdefs.h +67 -0
  109. data/src/core/filter/auth/auth_filters.h +0 -25
  110. data/src/core/filter/auth/client_auth_filter.cc +0 -118
  111. data/src/core/filter/filter_args.h +9 -23
  112. data/src/core/handshaker/handshaker.cc +23 -14
  113. data/src/core/handshaker/handshaker.h +3 -0
  114. data/src/core/handshaker/http_connect/http_connect_handshaker.cc +3 -1
  115. data/src/core/handshaker/security/legacy_secure_endpoint.cc +6 -5
  116. data/src/core/handshaker/security/secure_endpoint.cc +70 -25
  117. data/src/core/handshaker/security/security_handshaker.cc +4 -1
  118. data/src/core/handshaker/tcp_connect/tcp_connect_handshaker.cc +7 -1
  119. data/src/core/lib/channel/channel_args.cc +15 -0
  120. data/src/core/lib/channel/channel_args.h +3 -0
  121. data/src/core/lib/channel/channel_stack.cc +22 -23
  122. data/src/core/lib/channel/channel_stack.h +9 -7
  123. data/src/core/lib/channel/channel_stack_builder_impl.cc +1 -1
  124. data/src/core/lib/channel/channel_stack_builder_impl.h +2 -7
  125. data/src/core/lib/channel/promise_based_filter.h +5 -5
  126. data/src/core/lib/debug/trace_impl.h +0 -1
  127. data/src/core/lib/event_engine/ares_resolver.cc +165 -46
  128. data/src/core/lib/event_engine/ares_resolver.h +48 -2
  129. data/src/core/lib/event_engine/cf_engine/cf_engine.cc +3 -1
  130. data/src/core/lib/event_engine/cf_engine/cf_engine.h +1 -4
  131. data/src/core/lib/event_engine/cf_engine/cfstream_endpoint.h +2 -6
  132. data/src/core/lib/event_engine/endpoint_channel_arg_wrapper.cc +40 -0
  133. data/src/core/lib/event_engine/endpoint_channel_arg_wrapper.h +60 -0
  134. data/src/core/lib/event_engine/event_engine.cc +7 -0
  135. data/src/core/lib/event_engine/extensions/channelz.h +10 -6
  136. data/src/core/lib/event_engine/grpc_polled_fd.h +5 -0
  137. data/src/core/lib/event_engine/posix_engine/ev_epoll1_linux.cc +130 -162
  138. data/src/core/lib/event_engine/posix_engine/ev_epoll1_linux.h +11 -15
  139. data/src/core/lib/event_engine/posix_engine/ev_poll_posix.cc +75 -117
  140. data/src/core/lib/event_engine/posix_engine/ev_poll_posix.h +7 -9
  141. data/src/core/lib/event_engine/posix_engine/event_poller.h +18 -15
  142. data/src/core/lib/event_engine/posix_engine/event_poller_posix_default.cc +0 -18
  143. data/src/core/lib/event_engine/posix_engine/file_descriptor_collection.cc +124 -0
  144. data/src/core/lib/event_engine/posix_engine/file_descriptor_collection.h +243 -0
  145. data/src/core/lib/event_engine/posix_engine/grpc_polled_fd_posix.h +29 -19
  146. data/src/core/lib/event_engine/posix_engine/internal_errqueue.cc +6 -2
  147. data/src/core/lib/event_engine/posix_engine/internal_errqueue.h +6 -1
  148. data/src/core/lib/event_engine/posix_engine/posix_endpoint.cc +145 -92
  149. data/src/core/lib/event_engine/posix_engine/posix_endpoint.h +9 -19
  150. data/src/core/lib/event_engine/posix_engine/posix_engine.cc +333 -116
  151. data/src/core/lib/event_engine/posix_engine/posix_engine.h +61 -18
  152. data/src/core/lib/event_engine/posix_engine/posix_engine_listener.cc +45 -37
  153. data/src/core/lib/event_engine/posix_engine/posix_engine_listener.h +6 -4
  154. data/src/core/lib/event_engine/posix_engine/posix_engine_listener_utils.cc +32 -142
  155. data/src/core/lib/event_engine/posix_engine/posix_engine_listener_utils.h +6 -5
  156. data/src/core/lib/event_engine/posix_engine/posix_interface.h +211 -0
  157. data/src/core/lib/event_engine/posix_engine/posix_interface_posix.cc +1083 -0
  158. data/src/core/lib/event_engine/posix_engine/posix_interface_windows.cc +281 -0
  159. data/src/core/lib/event_engine/posix_engine/posix_write_event_sink.cc +154 -0
  160. data/src/core/lib/event_engine/posix_engine/posix_write_event_sink.h +174 -0
  161. data/src/core/lib/event_engine/posix_engine/tcp_socket_utils.cc +3 -719
  162. data/src/core/lib/event_engine/posix_engine/tcp_socket_utils.h +10 -170
  163. data/src/core/lib/event_engine/posix_engine/timer_manager.cc +33 -22
  164. data/src/core/lib/event_engine/posix_engine/timer_manager.h +13 -11
  165. data/src/core/lib/event_engine/posix_engine/traced_buffer_list.cc +117 -151
  166. data/src/core/lib/event_engine/posix_engine/traced_buffer_list.h +26 -94
  167. data/src/core/lib/event_engine/posix_engine/wakeup_fd_eventfd.cc +26 -25
  168. data/src/core/lib/event_engine/posix_engine/wakeup_fd_eventfd.h +6 -2
  169. data/src/core/lib/event_engine/posix_engine/wakeup_fd_pipe.cc +36 -62
  170. data/src/core/lib/event_engine/posix_engine/wakeup_fd_pipe.h +6 -2
  171. data/src/core/lib/event_engine/posix_engine/wakeup_fd_posix.h +7 -6
  172. data/src/core/lib/event_engine/posix_engine/wakeup_fd_posix_default.cc +12 -6
  173. data/src/core/lib/event_engine/posix_engine/wakeup_fd_posix_default.h +3 -1
  174. data/src/core/lib/event_engine/shim.cc +9 -0
  175. data/src/core/lib/event_engine/shim.h +3 -0
  176. data/src/core/lib/event_engine/thread_pool/thread_pool.h +7 -3
  177. data/src/core/lib/event_engine/thread_pool/thread_pool_factory.cc +0 -17
  178. data/src/core/lib/event_engine/thread_pool/work_stealing_thread_pool.cc +4 -2
  179. data/src/core/lib/event_engine/thread_pool/work_stealing_thread_pool.h +3 -2
  180. data/src/core/lib/event_engine/windows/grpc_polled_fd_windows.cc +4 -0
  181. data/src/core/lib/event_engine/windows/grpc_polled_fd_windows.h +4 -0
  182. data/src/core/lib/event_engine/windows/windows_endpoint.h +2 -6
  183. data/src/core/lib/event_engine/windows/windows_engine.cc +0 -1
  184. data/src/core/lib/event_engine/windows/windows_engine.h +1 -3
  185. data/src/core/lib/event_engine/windows/windows_listener.cc +14 -2
  186. data/src/core/lib/experiments/experiments.cc +45 -93
  187. data/src/core/lib/experiments/experiments.h +21 -51
  188. data/src/core/lib/iomgr/endpoint.cc +4 -3
  189. data/src/core/lib/iomgr/endpoint.h +7 -4
  190. data/src/core/lib/iomgr/endpoint_cfstream.cc +3 -2
  191. data/src/core/lib/iomgr/ev_epoll1_linux.cc +7 -2
  192. data/src/core/lib/iomgr/ev_poll_posix.cc +7 -2
  193. data/src/core/lib/iomgr/event_engine_shims/endpoint.cc +4 -6
  194. data/src/core/lib/iomgr/tcp_posix.cc +12 -6
  195. data/src/core/lib/iomgr/tcp_windows.cc +3 -2
  196. data/src/core/lib/promise/activity.h +1 -0
  197. data/src/core/lib/promise/arena_promise.h +23 -7
  198. data/src/core/lib/promise/detail/promise_factory.h +10 -0
  199. data/src/core/lib/promise/detail/promise_like.h +118 -11
  200. data/src/core/lib/promise/detail/promise_variant.h +50 -0
  201. data/src/core/lib/promise/detail/seq_state.h +687 -548
  202. data/src/core/lib/promise/if.h +20 -0
  203. data/src/core/lib/promise/inter_activity_latch.h +147 -0
  204. data/src/core/lib/promise/inter_activity_mutex.h +547 -0
  205. data/src/core/lib/promise/loop.h +65 -3
  206. data/src/core/lib/promise/map.h +24 -0
  207. data/src/core/lib/promise/match_promise.h +103 -0
  208. data/src/core/lib/promise/mpsc.cc +425 -0
  209. data/src/core/lib/promise/mpsc.h +490 -0
  210. data/src/core/lib/promise/party.cc +50 -1
  211. data/src/core/lib/promise/party.h +66 -1
  212. data/src/core/lib/promise/race.h +31 -0
  213. data/src/core/lib/promise/seq.h +4 -1
  214. data/src/core/lib/promise/status_flag.h +7 -0
  215. data/src/core/lib/promise/try_seq.h +4 -1
  216. data/src/core/lib/promise/wait_set.cc +28 -0
  217. data/src/core/lib/promise/wait_set.h +86 -0
  218. data/src/core/lib/resource_quota/arena.h +19 -0
  219. data/src/core/lib/slice/slice.h +5 -0
  220. data/src/core/lib/surface/channel_create.cc +88 -13
  221. data/src/core/lib/surface/channel_create.h +4 -0
  222. data/src/core/lib/surface/channel_init.cc +164 -47
  223. data/src/core/lib/surface/channel_init.h +64 -1
  224. data/src/core/lib/surface/filter_stack_call.cc +18 -9
  225. data/src/core/lib/surface/init.cc +6 -15
  226. data/src/core/lib/surface/legacy_channel.cc +3 -5
  227. data/src/core/lib/surface/legacy_channel.h +3 -1
  228. data/src/core/lib/surface/version.cc +2 -2
  229. data/src/core/lib/transport/promise_endpoint.cc +110 -0
  230. data/src/core/lib/transport/promise_endpoint.h +307 -0
  231. data/src/core/load_balancing/child_policy_handler.cc +2 -4
  232. data/src/core/load_balancing/delegating_helper.h +2 -3
  233. data/src/core/load_balancing/health_check_client.cc +1 -5
  234. data/src/core/load_balancing/lb_policy.h +1 -3
  235. data/src/core/load_balancing/oob_backend_metric.cc +1 -5
  236. data/src/core/load_balancing/pick_first/pick_first.cc +3 -0
  237. data/src/core/load_balancing/xds/cds.cc +10 -1
  238. data/src/core/plugin_registry/grpc_plugin_registry_extra.cc +2 -0
  239. data/src/core/resolver/xds/xds_config.cc +6 -3
  240. data/src/core/resolver/xds/xds_config.h +9 -4
  241. data/src/core/resolver/xds/xds_dependency_manager.cc +21 -6
  242. data/src/core/resolver/xds/xds_dependency_manager.h +2 -1
  243. data/src/core/resolver/xds/xds_resolver.cc +31 -11
  244. data/src/core/server/server.cc +83 -12
  245. data/src/core/server/server.h +21 -2
  246. data/src/core/server/xds_server_config_fetcher.cc +63 -25
  247. data/src/core/service_config/service_config.h +1 -1
  248. data/src/core/service_config/service_config_impl.h +1 -1
  249. data/src/core/telemetry/context_list_entry.cc +38 -0
  250. data/src/core/telemetry/context_list_entry.h +42 -12
  251. data/src/core/telemetry/stats_data.cc +233 -207
  252. data/src/core/telemetry/stats_data.h +250 -153
  253. data/src/core/telemetry/tcp_tracer.h +1 -1
  254. data/src/core/tsi/alts/zero_copy_frame_protector/alts_zero_copy_grpc_protector.cc +11 -3
  255. data/src/core/tsi/fake_transport_security.cc +17 -0
  256. data/src/core/tsi/ssl_transport_security.cc +2 -0
  257. data/src/core/tsi/transport_security_grpc.cc +8 -0
  258. data/src/core/tsi/transport_security_grpc.h +15 -0
  259. data/src/core/util/backoff.cc +1 -5
  260. data/src/core/util/backoff.h +1 -0
  261. data/src/core/util/down_cast.h +1 -1
  262. data/src/core/util/function_signature.h +15 -1
  263. data/src/core/util/http_client/httpcli.cc +12 -5
  264. data/src/core/util/http_client/httpcli.h +4 -1
  265. data/src/core/util/latent_see.h +8 -5
  266. data/src/core/util/log.cc +4 -0
  267. data/src/core/util/memory_usage.h +268 -0
  268. data/src/core/util/per_cpu.cc +2 -0
  269. data/src/core/util/per_cpu.h +7 -0
  270. data/src/core/util/shared_bit_gen.h +20 -0
  271. data/src/core/util/single_set_ptr.h +2 -2
  272. data/src/core/util/upb_utils.h +42 -0
  273. data/src/core/util/uri.cc +3 -2
  274. data/src/core/util/useful.h +53 -2
  275. data/src/core/util/wait_for_single_owner.cc +31 -0
  276. data/src/core/util/wait_for_single_owner.h +24 -0
  277. data/src/core/xds/grpc/xds_bootstrap_grpc.cc +2 -0
  278. data/src/core/xds/grpc/xds_bootstrap_grpc.h +5 -0
  279. data/src/core/xds/grpc/xds_client_grpc.cc +6 -2
  280. data/src/core/xds/grpc/xds_common_types_parser.cc +138 -50
  281. data/src/core/xds/grpc/xds_common_types_parser.h +12 -0
  282. data/src/core/xds/grpc/xds_http_filter.h +7 -0
  283. data/src/core/xds/grpc/xds_http_gcp_authn_filter.cc +22 -0
  284. data/src/core/xds/grpc/xds_http_gcp_authn_filter.h +3 -0
  285. data/src/core/xds/grpc/xds_route_config_parser.cc +15 -38
  286. data/src/core/xds/grpc/xds_server_grpc.cc +63 -13
  287. data/src/core/xds/grpc/xds_server_grpc.h +10 -2
  288. data/src/core/xds/grpc/xds_server_grpc_interface.h +4 -0
  289. data/src/core/xds/grpc/xds_transport_grpc.cc +18 -0
  290. data/src/core/xds/xds_client/xds_bootstrap.h +2 -0
  291. data/src/core/xds/xds_client/xds_client.cc +26 -5
  292. data/src/ruby/ext/grpc/extconf.rb +2 -0
  293. data/src/ruby/ext/grpc/rb_call.c +1 -8
  294. data/src/ruby/ext/grpc/rb_channel.c +72 -568
  295. data/src/ruby/ext/grpc/rb_channel.h +0 -3
  296. data/src/ruby/ext/grpc/rb_completion_queue.c +26 -14
  297. data/src/ruby/ext/grpc/rb_completion_queue.h +1 -7
  298. data/src/ruby/ext/grpc/rb_grpc.c +9 -5
  299. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +1 -1
  300. data/src/ruby/ext/grpc/rb_loader.c +0 -4
  301. data/src/ruby/ext/grpc/rb_server.c +31 -50
  302. data/src/ruby/lib/grpc/generic/client_stub.rb +4 -4
  303. data/src/ruby/lib/grpc/version.rb +1 -1
  304. data/src/ruby/spec/core_spec.rb +22 -0
  305. data/src/ruby/spec/generic/active_call_spec.rb +1 -1
  306. data/third_party/abseil-cpp/absl/algorithm/container.h +2 -19
  307. data/third_party/abseil-cpp/absl/base/attributes.h +76 -7
  308. data/third_party/abseil-cpp/absl/base/call_once.h +11 -12
  309. data/third_party/abseil-cpp/absl/base/config.h +20 -129
  310. data/third_party/abseil-cpp/absl/base/{internal/fast_type_id.h → fast_type_id.h} +11 -16
  311. data/third_party/abseil-cpp/absl/base/internal/cycleclock.cc +0 -5
  312. data/third_party/abseil-cpp/absl/base/internal/cycleclock_config.h +7 -7
  313. data/third_party/abseil-cpp/absl/base/internal/endian.h +34 -38
  314. data/third_party/abseil-cpp/absl/base/internal/iterator_traits.h +71 -0
  315. data/third_party/abseil-cpp/absl/base/internal/low_level_alloc.cc +6 -5
  316. data/third_party/abseil-cpp/absl/base/internal/{nullability_impl.h → nullability_deprecated.h} +45 -8
  317. data/third_party/abseil-cpp/absl/base/internal/spinlock.cc +0 -9
  318. data/third_party/abseil-cpp/absl/base/internal/spinlock.h +3 -13
  319. data/third_party/abseil-cpp/absl/base/internal/unaligned_access.h +6 -6
  320. data/third_party/abseil-cpp/absl/base/internal/unscaledcycleclock.h +8 -3
  321. data/third_party/abseil-cpp/absl/base/no_destructor.h +11 -32
  322. data/third_party/abseil-cpp/absl/base/nullability.h +84 -72
  323. data/third_party/abseil-cpp/absl/base/options.h +3 -80
  324. data/third_party/abseil-cpp/absl/base/policy_checks.h +7 -7
  325. data/third_party/abseil-cpp/absl/cleanup/cleanup.h +1 -3
  326. data/third_party/abseil-cpp/absl/cleanup/internal/cleanup.h +3 -4
  327. data/third_party/abseil-cpp/absl/container/btree_map.h +4 -2
  328. data/third_party/abseil-cpp/absl/container/btree_set.h +4 -2
  329. data/third_party/abseil-cpp/absl/container/fixed_array.h +7 -14
  330. data/third_party/abseil-cpp/absl/container/flat_hash_map.h +5 -0
  331. data/third_party/abseil-cpp/absl/container/flat_hash_set.h +6 -1
  332. data/third_party/abseil-cpp/absl/container/inlined_vector.h +8 -5
  333. data/third_party/abseil-cpp/absl/container/internal/btree.h +132 -29
  334. data/third_party/abseil-cpp/absl/container/internal/btree_container.h +175 -71
  335. data/third_party/abseil-cpp/absl/container/internal/common.h +43 -0
  336. data/third_party/abseil-cpp/absl/container/internal/common_policy_traits.h +1 -2
  337. data/third_party/abseil-cpp/absl/container/internal/container_memory.h +9 -10
  338. data/third_party/abseil-cpp/absl/container/internal/hash_function_defaults.h +1 -8
  339. data/third_party/abseil-cpp/absl/container/internal/hash_policy_traits.h +0 -4
  340. data/third_party/abseil-cpp/absl/container/internal/hashtable_control_bytes.h +527 -0
  341. data/third_party/abseil-cpp/absl/container/internal/hashtablez_sampler.cc +20 -4
  342. data/third_party/abseil-cpp/absl/container/internal/hashtablez_sampler.h +31 -12
  343. data/third_party/abseil-cpp/absl/container/internal/inlined_vector.h +2 -7
  344. data/third_party/abseil-cpp/absl/container/internal/layout.h +26 -42
  345. data/third_party/abseil-cpp/absl/container/internal/raw_hash_map.h +199 -68
  346. data/third_party/abseil-cpp/absl/container/internal/raw_hash_set.cc +1354 -183
  347. data/third_party/abseil-cpp/absl/container/internal/raw_hash_set.h +881 -1424
  348. data/third_party/abseil-cpp/absl/container/internal/raw_hash_set_resize_impl.h +80 -0
  349. data/third_party/abseil-cpp/absl/crc/crc32c.cc +0 -4
  350. data/third_party/abseil-cpp/absl/crc/crc32c.h +7 -5
  351. data/third_party/abseil-cpp/absl/crc/internal/crc32_x86_arm_combined_simd.h +0 -22
  352. data/third_party/abseil-cpp/absl/crc/internal/crc_x86_arm_combined.cc +45 -74
  353. data/third_party/abseil-cpp/absl/debugging/internal/addresses.h +57 -0
  354. data/third_party/abseil-cpp/absl/debugging/internal/decode_rust_punycode.cc +1 -1
  355. data/third_party/abseil-cpp/absl/debugging/internal/decode_rust_punycode.h +5 -5
  356. data/third_party/abseil-cpp/absl/debugging/internal/demangle.cc +8 -35
  357. data/third_party/abseil-cpp/absl/debugging/internal/demangle_rust.cc +16 -16
  358. data/third_party/abseil-cpp/absl/debugging/internal/stacktrace_aarch64-inl.inc +40 -37
  359. data/third_party/abseil-cpp/absl/debugging/internal/stacktrace_arm-inl.inc +16 -7
  360. data/third_party/abseil-cpp/absl/debugging/internal/stacktrace_emscripten-inl.inc +14 -5
  361. data/third_party/abseil-cpp/absl/debugging/internal/stacktrace_generic-inl.inc +10 -4
  362. data/third_party/abseil-cpp/absl/debugging/internal/stacktrace_powerpc-inl.inc +27 -16
  363. data/third_party/abseil-cpp/absl/debugging/internal/stacktrace_riscv-inl.inc +13 -4
  364. data/third_party/abseil-cpp/absl/debugging/internal/stacktrace_unimplemented-inl.inc +4 -3
  365. data/third_party/abseil-cpp/absl/debugging/internal/stacktrace_win32-inl.inc +15 -28
  366. data/third_party/abseil-cpp/absl/debugging/internal/stacktrace_x86-inl.inc +19 -9
  367. data/third_party/abseil-cpp/absl/debugging/stacktrace.cc +144 -27
  368. data/third_party/abseil-cpp/absl/debugging/stacktrace.h +73 -5
  369. data/third_party/abseil-cpp/absl/debugging/symbolize_elf.inc +19 -9
  370. data/third_party/abseil-cpp/absl/debugging/symbolize_emscripten.inc +3 -2
  371. data/third_party/abseil-cpp/absl/debugging/symbolize_win32.inc +25 -6
  372. data/third_party/abseil-cpp/absl/flags/commandlineflag.h +2 -2
  373. data/third_party/abseil-cpp/absl/flags/flag.h +4 -3
  374. data/third_party/abseil-cpp/absl/flags/internal/commandlineflag.h +2 -2
  375. data/third_party/abseil-cpp/absl/flags/internal/flag.cc +2 -1
  376. data/third_party/abseil-cpp/absl/flags/internal/flag.h +7 -6
  377. data/third_party/abseil-cpp/absl/flags/internal/registry.h +4 -3
  378. data/third_party/abseil-cpp/absl/flags/reflection.cc +2 -3
  379. data/third_party/abseil-cpp/absl/functional/any_invocable.h +8 -10
  380. data/third_party/abseil-cpp/absl/functional/function_ref.h +2 -9
  381. data/third_party/abseil-cpp/absl/functional/internal/any_invocable.h +110 -226
  382. data/third_party/abseil-cpp/absl/functional/internal/front_binder.h +10 -12
  383. data/third_party/abseil-cpp/absl/functional/internal/function_ref.h +2 -5
  384. data/third_party/abseil-cpp/absl/hash/hash.h +18 -0
  385. data/third_party/abseil-cpp/absl/hash/internal/hash.cc +1 -5
  386. data/third_party/abseil-cpp/absl/hash/internal/hash.h +86 -61
  387. data/third_party/abseil-cpp/absl/hash/internal/low_level_hash.cc +25 -68
  388. data/third_party/abseil-cpp/absl/hash/internal/low_level_hash.h +2 -6
  389. data/third_party/abseil-cpp/absl/hash/internal/weakly_mixed_integer.h +38 -0
  390. data/third_party/abseil-cpp/absl/log/check.h +2 -1
  391. data/third_party/abseil-cpp/absl/log/globals.h +4 -5
  392. data/third_party/abseil-cpp/absl/log/internal/append_truncated.h +28 -0
  393. data/third_party/abseil-cpp/absl/log/internal/check_op.cc +22 -22
  394. data/third_party/abseil-cpp/absl/log/internal/check_op.h +65 -62
  395. data/third_party/abseil-cpp/absl/log/internal/conditions.cc +5 -3
  396. data/third_party/abseil-cpp/absl/log/internal/conditions.h +7 -2
  397. data/third_party/abseil-cpp/absl/log/internal/log_message.cc +85 -43
  398. data/third_party/abseil-cpp/absl/log/internal/log_message.h +84 -59
  399. data/third_party/abseil-cpp/absl/log/internal/nullstream.h +1 -0
  400. data/third_party/abseil-cpp/absl/log/internal/proto.cc +3 -2
  401. data/third_party/abseil-cpp/absl/log/internal/proto.h +3 -3
  402. data/third_party/abseil-cpp/absl/log/internal/strip.h +4 -12
  403. data/third_party/abseil-cpp/absl/log/internal/vlog_config.h +8 -6
  404. data/third_party/abseil-cpp/absl/log/internal/voidify.h +10 -4
  405. data/third_party/abseil-cpp/absl/log/log.h +48 -35
  406. data/third_party/abseil-cpp/absl/log/log_sink_registry.h +2 -2
  407. data/third_party/abseil-cpp/absl/meta/type_traits.h +46 -175
  408. data/third_party/abseil-cpp/absl/numeric/bits.h +68 -2
  409. data/third_party/abseil-cpp/absl/numeric/int128.cc +0 -52
  410. data/third_party/abseil-cpp/absl/numeric/internal/bits.h +7 -3
  411. data/third_party/abseil-cpp/absl/profiling/internal/exponential_biased.cc +1 -1
  412. data/third_party/abseil-cpp/absl/random/bit_gen_ref.h +10 -11
  413. data/third_party/abseil-cpp/absl/random/distributions.h +6 -8
  414. data/third_party/abseil-cpp/absl/random/gaussian_distribution.h +1 -1
  415. data/third_party/abseil-cpp/absl/random/internal/distribution_caller.h +5 -6
  416. data/third_party/abseil-cpp/absl/random/internal/{pool_urbg.cc → entropy_pool.cc} +22 -90
  417. data/third_party/abseil-cpp/absl/random/internal/entropy_pool.h +35 -0
  418. data/third_party/abseil-cpp/absl/random/internal/nonsecure_base.h +5 -6
  419. data/third_party/abseil-cpp/absl/random/internal/randen_detect.cc +1 -1
  420. data/third_party/abseil-cpp/absl/random/internal/seed_material.cc +20 -12
  421. data/third_party/abseil-cpp/absl/random/internal/seed_material.h +5 -5
  422. data/third_party/abseil-cpp/absl/random/random.h +88 -53
  423. data/third_party/abseil-cpp/absl/random/seed_sequences.cc +6 -2
  424. data/third_party/abseil-cpp/absl/status/internal/status_internal.cc +3 -4
  425. data/third_party/abseil-cpp/absl/status/internal/status_internal.h +3 -4
  426. data/third_party/abseil-cpp/absl/status/internal/statusor_internal.h +4 -3
  427. data/third_party/abseil-cpp/absl/status/status.cc +4 -8
  428. data/third_party/abseil-cpp/absl/status/status.h +8 -8
  429. data/third_party/abseil-cpp/absl/status/status_payload_printer.h +2 -2
  430. data/third_party/abseil-cpp/absl/status/statusor.cc +2 -2
  431. data/third_party/abseil-cpp/absl/status/statusor.h +6 -6
  432. data/third_party/abseil-cpp/absl/strings/ascii.cc +9 -9
  433. data/third_party/abseil-cpp/absl/strings/ascii.h +18 -18
  434. data/third_party/abseil-cpp/absl/strings/charconv.cc +21 -22
  435. data/third_party/abseil-cpp/absl/strings/charconv.h +5 -5
  436. data/third_party/abseil-cpp/absl/strings/cord.cc +54 -58
  437. data/third_party/abseil-cpp/absl/strings/cord.h +94 -83
  438. data/third_party/abseil-cpp/absl/strings/cord_analysis.cc +11 -11
  439. data/third_party/abseil-cpp/absl/strings/cord_analysis.h +3 -3
  440. data/third_party/abseil-cpp/absl/strings/escaping.cc +130 -149
  441. data/third_party/abseil-cpp/absl/strings/escaping.h +9 -10
  442. data/third_party/abseil-cpp/absl/strings/internal/charconv_bigint.cc +1 -1
  443. data/third_party/abseil-cpp/absl/strings/internal/cord_internal.h +6 -8
  444. data/third_party/abseil-cpp/absl/strings/internal/cord_rep_btree.cc +0 -4
  445. data/third_party/abseil-cpp/absl/strings/internal/cordz_info.cc +0 -4
  446. data/third_party/abseil-cpp/absl/strings/internal/str_format/arg.cc +7 -63
  447. data/third_party/abseil-cpp/absl/strings/internal/str_format/arg.h +1 -11
  448. data/third_party/abseil-cpp/absl/strings/internal/str_format/extension.cc +0 -22
  449. data/third_party/abseil-cpp/absl/strings/internal/str_format/output.cc +5 -3
  450. data/third_party/abseil-cpp/absl/strings/internal/str_format/parser.h +4 -2
  451. data/third_party/abseil-cpp/absl/strings/internal/str_join_internal.h +3 -3
  452. data/third_party/abseil-cpp/absl/strings/internal/string_constant.h +0 -5
  453. data/third_party/abseil-cpp/absl/strings/internal/utf8.cc +96 -1
  454. data/third_party/abseil-cpp/absl/strings/internal/utf8.h +15 -1
  455. data/third_party/abseil-cpp/absl/strings/numbers.cc +53 -32
  456. data/third_party/abseil-cpp/absl/strings/numbers.h +87 -58
  457. data/third_party/abseil-cpp/absl/strings/str_cat.cc +6 -7
  458. data/third_party/abseil-cpp/absl/strings/str_cat.h +32 -32
  459. data/third_party/abseil-cpp/absl/strings/str_format.h +18 -18
  460. data/third_party/abseil-cpp/absl/strings/str_replace.cc +3 -3
  461. data/third_party/abseil-cpp/absl/strings/str_replace.h +6 -6
  462. data/third_party/abseil-cpp/absl/strings/string_view.cc +4 -9
  463. data/third_party/abseil-cpp/absl/strings/string_view.h +27 -32
  464. data/third_party/abseil-cpp/absl/strings/strip.h +4 -4
  465. data/third_party/abseil-cpp/absl/strings/substitute.cc +5 -4
  466. data/third_party/abseil-cpp/absl/strings/substitute.h +66 -64
  467. data/third_party/abseil-cpp/absl/synchronization/internal/futex_waiter.cc +0 -4
  468. data/third_party/abseil-cpp/absl/synchronization/internal/kernel_timeout.cc +0 -5
  469. data/third_party/abseil-cpp/absl/synchronization/internal/pthread_waiter.cc +0 -4
  470. data/third_party/abseil-cpp/absl/synchronization/internal/sem_waiter.cc +0 -4
  471. data/third_party/abseil-cpp/absl/synchronization/internal/stdcpp_waiter.cc +0 -4
  472. data/third_party/abseil-cpp/absl/synchronization/internal/waiter_base.cc +0 -4
  473. data/third_party/abseil-cpp/absl/synchronization/internal/win32_waiter.cc +0 -4
  474. data/third_party/abseil-cpp/absl/synchronization/mutex.cc +1 -1
  475. data/third_party/abseil-cpp/absl/synchronization/mutex.h +97 -69
  476. data/third_party/abseil-cpp/absl/synchronization/notification.h +1 -1
  477. data/third_party/abseil-cpp/absl/time/civil_time.cc +1 -0
  478. data/third_party/abseil-cpp/absl/time/duration.cc +12 -7
  479. data/third_party/abseil-cpp/absl/time/internal/cctz/include/cctz/time_zone.h +1 -1
  480. data/third_party/abseil-cpp/absl/time/internal/cctz/src/time_zone_lookup.cc +90 -111
  481. data/third_party/abseil-cpp/absl/time/time.h +20 -15
  482. data/third_party/abseil-cpp/absl/types/optional.h +7 -747
  483. data/third_party/abseil-cpp/absl/types/span.h +13 -11
  484. data/third_party/abseil-cpp/absl/types/variant.h +5 -784
  485. data/third_party/abseil-cpp/absl/utility/utility.h +10 -185
  486. metadata +72 -20
  487. data/src/core/lib/event_engine/forkable.cc +0 -105
  488. data/src/core/lib/event_engine/forkable.h +0 -67
  489. data/src/core/lib/iomgr/python_util.h +0 -46
  490. data/third_party/abseil-cpp/absl/base/internal/inline_variable.h +0 -108
  491. data/third_party/abseil-cpp/absl/base/internal/invoke.h +0 -241
  492. data/third_party/abseil-cpp/absl/log/log_entry.cc +0 -41
  493. data/third_party/abseil-cpp/absl/random/internal/pool_urbg.h +0 -131
  494. data/third_party/abseil-cpp/absl/types/bad_optional_access.cc +0 -66
  495. data/third_party/abseil-cpp/absl/types/bad_optional_access.h +0 -78
  496. data/third_party/abseil-cpp/absl/types/bad_variant_access.cc +0 -82
  497. data/third_party/abseil-cpp/absl/types/bad_variant_access.h +0 -82
  498. data/third_party/abseil-cpp/absl/types/internal/optional.h +0 -352
  499. data/third_party/abseil-cpp/absl/types/internal/variant.h +0 -1622
@@ -27,9 +27,11 @@
27
27
  #include "absl/base/internal/raw_logging.h"
28
28
  #include "absl/base/optimization.h"
29
29
  #include "absl/container/internal/container_memory.h"
30
+ #include "absl/container/internal/hashtable_control_bytes.h"
30
31
  #include "absl/container/internal/hashtablez_sampler.h"
32
+ #include "absl/container/internal/raw_hash_set_resize_impl.h"
33
+ #include "absl/functional/function_ref.h"
31
34
  #include "absl/hash/hash.h"
32
- #include "absl/numeric/bits.h"
33
35
 
34
36
  namespace absl {
35
37
  ABSL_NAMESPACE_BEGIN
@@ -65,21 +67,31 @@ ABSL_CONST_INIT ABSL_DLL const ctrl_t kSooControl[17] = {
65
67
  static_assert(NumControlBytes(SooCapacity()) <= 17,
66
68
  "kSooControl capacity too small");
67
69
 
68
- #ifdef ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
69
- constexpr size_t Group::kWidth;
70
+ namespace {
71
+
72
+ #ifdef ABSL_SWISSTABLE_ASSERT
73
+ #error ABSL_SWISSTABLE_ASSERT cannot be directly set
74
+ #else
75
+ // We use this macro for assertions that users may see when the table is in an
76
+ // invalid state that sanitizers may help diagnose.
77
+ #define ABSL_SWISSTABLE_ASSERT(CONDITION) \
78
+ assert((CONDITION) && "Try enabling sanitizers.")
70
79
  #endif
71
80
 
72
- namespace {
81
+ [[noreturn]] ABSL_ATTRIBUTE_NOINLINE void HashTableSizeOverflow() {
82
+ ABSL_RAW_LOG(FATAL, "Hash table size overflow");
83
+ }
84
+
85
+ void ValidateMaxSize(size_t size, size_t slot_size) {
86
+ if (IsAboveValidSize(size, slot_size)) {
87
+ HashTableSizeOverflow();
88
+ }
89
+ }
73
90
 
74
91
  // Returns "random" seed.
75
92
  inline size_t RandomSeed() {
76
93
  #ifdef ABSL_HAVE_THREAD_LOCAL
77
94
  static thread_local size_t counter = 0;
78
- // On Linux kernels >= 5.4 the MSAN runtime has a false-positive when
79
- // accessing thread local storage data from loaded libraries
80
- // (https://github.com/google/sanitizers/issues/1265), for this reason counter
81
- // needs to be annotated as initialized.
82
- ABSL_ANNOTATE_MEMORY_IS_INITIALIZED(&counter, sizeof(size_t));
83
95
  size_t value = ++counter;
84
96
  #else // ABSL_HAVE_THREAD_LOCAL
85
97
  static std::atomic<size_t> counter(0);
@@ -88,24 +100,35 @@ inline size_t RandomSeed() {
88
100
  return value ^ static_cast<size_t>(reinterpret_cast<uintptr_t>(&counter));
89
101
  }
90
102
 
91
- bool ShouldRehashForBugDetection(const ctrl_t* ctrl, size_t capacity) {
103
+ bool ShouldRehashForBugDetection(PerTableSeed seed, size_t capacity) {
92
104
  // Note: we can't use the abseil-random library because abseil-random
93
105
  // depends on swisstable. We want to return true with probability
94
106
  // `min(1, RehashProbabilityConstant() / capacity())`. In order to do this,
95
107
  // we probe based on a random hash and see if the offset is less than
96
108
  // RehashProbabilityConstant().
97
- return probe(ctrl, capacity, absl::HashOf(RandomSeed())).offset() <
109
+ return probe(seed, capacity, absl::HashOf(RandomSeed())).offset() <
98
110
  RehashProbabilityConstant();
99
111
  }
100
112
 
101
113
  // Find a non-deterministic hash for single group table.
102
114
  // Last two bits are used to find a position for a newly inserted element after
103
115
  // resize.
104
- // This function is mixing all bits of hash and control pointer to maximize
105
- // entropy.
106
- size_t SingleGroupTableH1(size_t hash, ctrl_t* control) {
107
- return static_cast<size_t>(absl::popcount(
108
- hash ^ static_cast<size_t>(reinterpret_cast<uintptr_t>(control))));
116
+ // This function basically using H2 last bits to save on shift operation.
117
+ size_t SingleGroupTableH1(size_t hash, PerTableSeed seed) {
118
+ return hash ^ seed.seed();
119
+ }
120
+
121
+ // Returns the address of the slot `i` iterations after `slot` assuming each
122
+ // slot has the specified size.
123
+ inline void* NextSlot(void* slot, size_t slot_size, size_t i = 1) {
124
+ return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(slot) +
125
+ slot_size * i);
126
+ }
127
+
128
+ // Returns the address of the slot just before `slot` assuming each slot has the
129
+ // specified size.
130
+ inline void* PrevSlot(void* slot, size_t slot_size) {
131
+ return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(slot) - slot_size);
109
132
  }
110
133
 
111
134
  } // namespace
@@ -121,42 +144,104 @@ GenerationType* EmptyGeneration() {
121
144
  }
122
145
 
123
146
  bool CommonFieldsGenerationInfoEnabled::
124
- should_rehash_for_bug_detection_on_insert(const ctrl_t* ctrl,
147
+ should_rehash_for_bug_detection_on_insert(PerTableSeed seed,
125
148
  size_t capacity) const {
126
149
  if (reserved_growth_ == kReservedGrowthJustRanOut) return true;
127
150
  if (reserved_growth_ > 0) return false;
128
- return ShouldRehashForBugDetection(ctrl, capacity);
151
+ return ShouldRehashForBugDetection(seed, capacity);
129
152
  }
130
153
 
131
154
  bool CommonFieldsGenerationInfoEnabled::should_rehash_for_bug_detection_on_move(
132
- const ctrl_t* ctrl, size_t capacity) const {
133
- return ShouldRehashForBugDetection(ctrl, capacity);
155
+ PerTableSeed seed, size_t capacity) const {
156
+ return ShouldRehashForBugDetection(seed, capacity);
157
+ }
158
+
159
+ namespace {
160
+
161
+ FindInfo find_first_non_full_from_h1(const ctrl_t* ctrl, size_t h1,
162
+ size_t capacity) {
163
+ auto seq = probe(h1, capacity);
164
+ if (IsEmptyOrDeleted(ctrl[seq.offset()])) {
165
+ return {seq.offset(), /*probe_length=*/0};
166
+ }
167
+ while (true) {
168
+ GroupFullEmptyOrDeleted g{ctrl + seq.offset()};
169
+ auto mask = g.MaskEmptyOrDeleted();
170
+ if (mask) {
171
+ return {seq.offset(mask.LowestBitSet()), seq.index()};
172
+ }
173
+ seq.next();
174
+ ABSL_SWISSTABLE_ASSERT(seq.index() <= capacity && "full table!");
175
+ }
134
176
  }
135
177
 
136
- bool ShouldInsertBackwardsForDebug(size_t capacity, size_t hash,
137
- const ctrl_t* ctrl) {
138
- // To avoid problems with weak hashes and single bit tests, we use % 13.
139
- // TODO(kfm,sbenza): revisit after we do unconditional mixing
140
- return !is_small(capacity) && (H1(hash, ctrl) ^ RandomSeed()) % 13 > 6;
178
+ // Whether a table is "small". A small table fits entirely into a probing
179
+ // group, i.e., has a capacity < `Group::kWidth`.
180
+ //
181
+ // In small mode we are able to use the whole capacity. The extra control
182
+ // bytes give us at least one "empty" control byte to stop the iteration.
183
+ // This is important to make 1 a valid capacity.
184
+ //
185
+ // In small mode only the first `capacity` control bytes after the sentinel
186
+ // are valid. The rest contain dummy ctrl_t::kEmpty values that do not
187
+ // represent a real slot.
188
+ constexpr bool is_small(size_t capacity) {
189
+ return capacity < Group::kWidth - 1;
141
190
  }
142
191
 
143
- size_t PrepareInsertAfterSoo(size_t hash, size_t slot_size,
144
- CommonFields& common) {
145
- assert(common.capacity() == NextCapacity(SooCapacity()));
146
- // After resize from capacity 1 to 3, we always have exactly the slot with
147
- // index 1 occupied, so we need to insert either at index 0 or index 2.
148
- assert(HashSetResizeHelper::SooSlotIndex() == 1);
149
- PrepareInsertCommon(common);
150
- const size_t offset = SingleGroupTableH1(hash, common.control()) & 2;
151
- common.growth_info().OverwriteEmptyAsFull();
152
- SetCtrlInSingleGroupTable(common, offset, H2(hash), slot_size);
153
- common.infoz().RecordInsert(hash, /*distance_from_desired=*/0);
154
- return offset;
192
+ template <class Fn>
193
+ void IterateOverFullSlotsImpl(const CommonFields& c, size_t slot_size, Fn cb) {
194
+ const size_t cap = c.capacity();
195
+ const ctrl_t* ctrl = c.control();
196
+ void* slot = c.slot_array();
197
+ if (is_small(cap)) {
198
+ // Mirrored/cloned control bytes in small table are also located in the
199
+ // first group (starting from position 0). We are taking group from position
200
+ // `capacity` in order to avoid duplicates.
201
+
202
+ // Small tables capacity fits into portable group, where
203
+ // GroupPortableImpl::MaskFull is more efficient for the
204
+ // capacity <= GroupPortableImpl::kWidth.
205
+ ABSL_SWISSTABLE_ASSERT(cap <= GroupPortableImpl::kWidth &&
206
+ "unexpectedly large small capacity");
207
+ static_assert(Group::kWidth >= GroupPortableImpl::kWidth,
208
+ "unexpected group width");
209
+ // Group starts from kSentinel slot, so indices in the mask will
210
+ // be increased by 1.
211
+ const auto mask = GroupPortableImpl(ctrl + cap).MaskFull();
212
+ --ctrl;
213
+ slot = PrevSlot(slot, slot_size);
214
+ for (uint32_t i : mask) {
215
+ cb(ctrl + i, SlotAddress(slot, i, slot_size));
216
+ }
217
+ return;
218
+ }
219
+ size_t remaining = c.size();
220
+ ABSL_ATTRIBUTE_UNUSED const size_t original_size_for_assert = remaining;
221
+ while (remaining != 0) {
222
+ for (uint32_t i : GroupFullEmptyOrDeleted(ctrl).MaskFull()) {
223
+ ABSL_SWISSTABLE_ASSERT(IsFull(ctrl[i]) &&
224
+ "hash table was modified unexpectedly");
225
+ cb(ctrl + i, SlotAddress(slot, i, slot_size));
226
+ --remaining;
227
+ }
228
+ ctrl += Group::kWidth;
229
+ slot = NextSlot(slot, slot_size, Group::kWidth);
230
+ ABSL_SWISSTABLE_ASSERT(
231
+ (remaining == 0 || *(ctrl - 1) != ctrl_t::kSentinel) &&
232
+ "hash table was modified unexpectedly");
233
+ }
234
+ // NOTE: erasure of the current element is allowed in callback for
235
+ // absl::erase_if specialization. So we use `>=`.
236
+ ABSL_SWISSTABLE_ASSERT(original_size_for_assert >= c.size() &&
237
+ "hash table was modified unexpectedly");
155
238
  }
156
239
 
240
+ } // namespace
241
+
157
242
  void ConvertDeletedToEmptyAndFullToDeleted(ctrl_t* ctrl, size_t capacity) {
158
- assert(ctrl[capacity] == ctrl_t::kSentinel);
159
- assert(IsValidCapacity(capacity));
243
+ ABSL_SWISSTABLE_ASSERT(ctrl[capacity] == ctrl_t::kSentinel);
244
+ ABSL_SWISSTABLE_ASSERT(IsValidCapacity(capacity));
160
245
  for (ctrl_t* pos = ctrl; pos < ctrl + capacity; pos += Group::kWidth) {
161
246
  Group{pos}.ConvertSpecialToEmptyAndFullToDeleted(pos);
162
247
  }
@@ -164,26 +249,25 @@ void ConvertDeletedToEmptyAndFullToDeleted(ctrl_t* ctrl, size_t capacity) {
164
249
  std::memcpy(ctrl + capacity + 1, ctrl, NumClonedBytes());
165
250
  ctrl[capacity] = ctrl_t::kSentinel;
166
251
  }
167
- // Extern template instantiation for inline function.
168
- template FindInfo find_first_non_full(const CommonFields&, size_t);
169
252
 
170
- FindInfo find_first_non_full_outofline(const CommonFields& common,
171
- size_t hash) {
172
- return find_first_non_full(common, hash);
253
+ FindInfo find_first_non_full(const CommonFields& common, size_t hash) {
254
+ return find_first_non_full_from_h1(common.control(), H1(hash, common.seed()),
255
+ common.capacity());
256
+ }
257
+
258
+ void IterateOverFullSlots(const CommonFields& c, size_t slot_size,
259
+ absl::FunctionRef<void(const ctrl_t*, void*)> cb) {
260
+ IterateOverFullSlotsImpl(c, slot_size, cb);
173
261
  }
174
262
 
175
263
  namespace {
176
264
 
177
- // Returns the address of the slot just after slot assuming each slot has the
178
- // specified size.
179
- static inline void* NextSlot(void* slot, size_t slot_size) {
180
- return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(slot) + slot_size);
265
+ void ResetGrowthLeft(GrowthInfo& growth_info, size_t capacity, size_t size) {
266
+ growth_info.InitGrowthLeftNoDeleted(CapacityToGrowth(capacity) - size);
181
267
  }
182
268
 
183
- // Returns the address of the slot just before slot assuming each slot has the
184
- // specified size.
185
- static inline void* PrevSlot(void* slot, size_t slot_size) {
186
- return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(slot) - slot_size);
269
+ void ResetGrowthLeft(CommonFields& common) {
270
+ ResetGrowthLeft(common.growth_info(), common.capacity(), common.size());
187
271
  }
188
272
 
189
273
  // Finds guaranteed to exists empty slot from the given position.
@@ -196,17 +280,34 @@ size_t FindEmptySlot(size_t start, size_t end, const ctrl_t* ctrl) {
196
280
  return i;
197
281
  }
198
282
  }
199
- assert(false && "no empty slot");
200
- return ~size_t{};
283
+ ABSL_UNREACHABLE();
201
284
  }
202
285
 
203
- void DropDeletesWithoutResize(CommonFields& common,
204
- const PolicyFunctions& policy) {
286
+ // Finds guaranteed to exist full slot starting from the given position.
287
+ // NOTE: this function is only triggered for rehash(0), when we need to
288
+ // go back to SOO state, so we keep it simple.
289
+ size_t FindFirstFullSlot(size_t start, size_t end, const ctrl_t* ctrl) {
290
+ for (size_t i = start; i < end; ++i) {
291
+ if (IsFull(ctrl[i])) {
292
+ return i;
293
+ }
294
+ }
295
+ ABSL_UNREACHABLE();
296
+ }
297
+
298
+ void PrepareInsertCommon(CommonFields& common) {
299
+ common.increment_size();
300
+ common.maybe_increment_generation_on_insert();
301
+ }
302
+
303
+ size_t DropDeletesWithoutResizeAndPrepareInsert(
304
+ CommonFields& common, const PolicyFunctions& __restrict policy,
305
+ size_t new_hash) {
205
306
  void* set = &common;
206
307
  void* slot_array = common.slot_array();
207
308
  const size_t capacity = common.capacity();
208
- assert(IsValidCapacity(capacity));
209
- assert(!is_small(capacity));
309
+ ABSL_SWISSTABLE_ASSERT(IsValidCapacity(capacity));
310
+ ABSL_SWISSTABLE_ASSERT(!is_single_group(capacity));
210
311
  // Algorithm:
211
312
  // - mark all DELETED slots as EMPTY
212
313
  // - mark all FULL slots as DELETED
@@ -227,7 +328,7 @@ void DropDeletesWithoutResize(CommonFields& common,
227
328
  ConvertDeletedToEmptyAndFullToDeleted(ctrl, capacity);
228
329
  const void* hash_fn = policy.hash_fn(common);
229
330
  auto hasher = policy.hash_slot;
230
- auto transfer = policy.transfer;
331
+ auto transfer_n = policy.transfer_n;
231
332
  const size_t slot_size = policy.slot_size;
232
333
 
233
334
  size_t total_probe_length = 0;
@@ -240,7 +341,7 @@ void DropDeletesWithoutResize(CommonFields& common,
240
341
 
241
342
  for (size_t i = 0; i != capacity;
242
343
  ++i, slot_ptr = NextSlot(slot_ptr, slot_size)) {
243
- assert(slot_ptr == SlotAddress(slot_array, i, slot_size));
344
+ ABSL_SWISSTABLE_ASSERT(slot_ptr == SlotAddress(slot_array, i, slot_size));
244
345
  if (IsEmpty(ctrl[i])) {
245
346
  tmp_space_id = i;
246
347
  continue;
@@ -255,13 +356,14 @@ void DropDeletesWithoutResize(CommonFields& common,
255
356
  // If they do, we don't need to move the object as it falls already in the
256
357
  // best probe we can.
257
358
  const size_t probe_offset = probe(common, hash).offset();
359
+ const h2_t h2 = H2(hash);
258
360
  const auto probe_index = [probe_offset, capacity](size_t pos) {
259
361
  return ((pos - probe_offset) & capacity) / Group::kWidth;
260
362
  };
261
363
 
262
364
  // Element doesn't move.
263
365
  if (ABSL_PREDICT_TRUE(probe_index(new_i) == probe_index(i))) {
264
- SetCtrl(common, i, H2(hash), slot_size);
366
+ SetCtrlInLargeTable(common, i, h2, slot_size);
265
367
  continue;
266
368
  }
267
369
 
@@ -270,14 +372,14 @@ void DropDeletesWithoutResize(CommonFields& common,
270
372
  // Transfer element to the empty spot.
271
373
  // SetCtrl poisons/unpoisons the slots so we have to call it at the
272
374
  // right time.
273
- SetCtrl(common, new_i, H2(hash), slot_size);
274
- (*transfer)(set, new_slot_ptr, slot_ptr);
275
- SetCtrl(common, i, ctrl_t::kEmpty, slot_size);
375
+ SetCtrlInLargeTable(common, new_i, h2, slot_size);
376
+ (*transfer_n)(set, new_slot_ptr, slot_ptr, 1);
377
+ SetCtrlInLargeTable(common, i, ctrl_t::kEmpty, slot_size);
276
378
  // Initialize or change empty space id.
277
379
  tmp_space_id = i;
278
380
  } else {
279
- assert(IsDeleted(ctrl[new_i]));
280
- SetCtrl(common, new_i, H2(hash), slot_size);
381
+ ABSL_SWISSTABLE_ASSERT(IsDeleted(ctrl[new_i]));
382
+ SetCtrlInLargeTable(common, new_i, h2, slot_size);
281
383
  // Until we are done rehashing, DELETED marks previously FULL slots.
282
384
 
283
385
  if (tmp_space_id == kUnknownId) {
@@ -287,9 +389,9 @@ void DropDeletesWithoutResize(CommonFields& common,
287
389
  SanitizerUnpoisonMemoryRegion(tmp_space, slot_size);
288
390
 
289
391
  // Swap i and new_i elements.
290
- (*transfer)(set, tmp_space, new_slot_ptr);
291
- (*transfer)(set, new_slot_ptr, slot_ptr);
292
- (*transfer)(set, slot_ptr, tmp_space);
392
+ (*transfer_n)(set, tmp_space, new_slot_ptr, 1);
393
+ (*transfer_n)(set, new_slot_ptr, slot_ptr, 1);
394
+ (*transfer_n)(set, slot_ptr, tmp_space, 1);
293
395
 
294
396
  SanitizerPoisonMemoryRegion(tmp_space, slot_size);
295
397
 
@@ -298,8 +400,14 @@ void DropDeletesWithoutResize(CommonFields& common,
298
400
  slot_ptr = PrevSlot(slot_ptr, slot_size);
299
401
  }
300
402
  }
403
+ // Prepare insert for the new element.
404
+ PrepareInsertCommon(common);
301
405
  ResetGrowthLeft(common);
406
+ FindInfo find_info = find_first_non_full(common, new_hash);
407
+ SetCtrlInLargeTable(common, find_info.offset, H2(new_hash), slot_size);
408
+ common.infoz().RecordInsert(new_hash, find_info.probe_length);
302
409
  common.infoz().RecordRehash(total_probe_length);
410
+ return find_info.offset;
303
411
  }
304
412
 
305
413
  static bool WasNeverFull(CommonFields& c, size_t index) {
@@ -319,10 +427,126 @@ static bool WasNeverFull(CommonFields& c, size_t index) {
319
427
  Group::kWidth;
320
428
  }
321
429
 
430
+ // Updates the control bytes to indicate a completely empty table such that all
431
+ // control bytes are kEmpty except for the kSentinel byte.
432
+ void ResetCtrl(CommonFields& common, size_t slot_size) {
433
+ const size_t capacity = common.capacity();
434
+ ctrl_t* ctrl = common.control();
435
+ static constexpr size_t kTwoGroupCapacity = 2 * Group::kWidth - 1;
436
+ if (ABSL_PREDICT_TRUE(capacity <= kTwoGroupCapacity)) {
437
+ std::memset(ctrl, static_cast<int8_t>(ctrl_t::kEmpty), Group::kWidth);
438
+ std::memset(ctrl + capacity, static_cast<int8_t>(ctrl_t::kEmpty),
439
+ Group::kWidth);
440
+ if (capacity == kTwoGroupCapacity) {
441
+ std::memset(ctrl + Group::kWidth, static_cast<int8_t>(ctrl_t::kEmpty),
442
+ Group::kWidth);
443
+ }
444
+ } else {
445
+ std::memset(ctrl, static_cast<int8_t>(ctrl_t::kEmpty),
446
+ capacity + 1 + NumClonedBytes());
447
+ }
448
+ ctrl[capacity] = ctrl_t::kSentinel;
449
+ SanitizerPoisonMemoryRegion(common.slot_array(), slot_size * capacity);
450
+ }
451
+
452
+ // Initializes control bytes for single element table.
453
+ // Capacity of the table must be 1.
454
+ ABSL_ATTRIBUTE_ALWAYS_INLINE inline void InitializeSingleElementControlBytes(
455
+ uint64_t h2, ctrl_t* new_ctrl) {
456
+ static constexpr uint64_t kEmptyXorSentinel =
457
+ static_cast<uint8_t>(ctrl_t::kEmpty) ^
458
+ static_cast<uint8_t>(ctrl_t::kSentinel);
459
+ static constexpr uint64_t kEmpty64 = static_cast<uint8_t>(ctrl_t::kEmpty);
460
+ // The first 8 bytes, where present slot positions are replaced with 0.
461
+ static constexpr uint64_t kFirstCtrlBytesWithZeroes =
462
+ k8EmptyBytes ^ kEmpty64 ^ (kEmptyXorSentinel << 8) ^ (kEmpty64 << 16);
463
+
464
+ // Fill the original 0th and mirrored 2nd bytes with the hash.
465
+ // Result will look like:
466
+ // HSHEEEEE
467
+ // Where H = h2, E = kEmpty, S = kSentinel.
468
+ const uint64_t first_ctrl_bytes =
469
+ (h2 | kFirstCtrlBytesWithZeroes) | (h2 << 16);
470
+ // Fill last bytes with kEmpty.
471
+ std::memset(new_ctrl + 1, static_cast<int8_t>(ctrl_t::kEmpty), Group::kWidth);
472
+ // Overwrite the first 3 bytes with HSH. Other bytes will not be changed.
473
+ absl::little_endian::Store64(new_ctrl, first_ctrl_bytes);
474
+ }
475
+
476
+ // Initializes control bytes for growing after SOO to the next capacity.
477
+ // `soo_ctrl` is placed in the position `SooSlotIndex()`.
478
+ // `new_hash` is placed in the position `new_offset`.
479
+ // The table must be non-empty SOO.
480
+ ABSL_ATTRIBUTE_ALWAYS_INLINE inline void
481
+ InitializeThreeElementsControlBytesAfterSoo(ctrl_t soo_ctrl, size_t new_hash,
482
+ size_t new_offset,
483
+ ctrl_t* new_ctrl) {
484
+ static constexpr size_t kNewCapacity = NextCapacity(SooCapacity());
485
+ static_assert(kNewCapacity == 3);
486
+ static_assert(is_single_group(kNewCapacity));
487
+ static_assert(SooSlotIndex() == 1);
488
+ ABSL_SWISSTABLE_ASSERT(new_offset == 0 || new_offset == 2);
489
+
490
+ static constexpr uint64_t kEmptyXorSentinel =
491
+ static_cast<uint8_t>(ctrl_t::kEmpty) ^
492
+ static_cast<uint8_t>(ctrl_t::kSentinel);
493
+ static constexpr uint64_t kEmpty64 = static_cast<uint8_t>(ctrl_t::kEmpty);
494
+ static constexpr size_t kMirroredSooSlotIndex =
495
+ SooSlotIndex() + kNewCapacity + 1;
496
+ // The first 8 bytes, where SOO slot original and mirrored positions are
497
+ // replaced with 0.
498
+ // Result will look like: E0ESE0EE
499
+ static constexpr uint64_t kFirstCtrlBytesWithZeroes =
500
+ k8EmptyBytes ^ (kEmpty64 << (8 * SooSlotIndex())) ^
501
+ (kEmptyXorSentinel << (8 * kNewCapacity)) ^
502
+ (kEmpty64 << (8 * kMirroredSooSlotIndex));
503
+
504
+ const uint64_t soo_h2 = static_cast<uint64_t>(soo_ctrl);
505
+ const uint64_t new_h2_xor_empty = static_cast<uint64_t>(
506
+ H2(new_hash) ^ static_cast<uint8_t>(ctrl_t::kEmpty));
507
+ // Fill the original and mirrored bytes for SOO slot.
508
+ // Result will look like:
509
+ // EHESEHEE
510
+ // Where H = soo_h2, E = kEmpty, S = kSentinel.
511
+ uint64_t first_ctrl_bytes =
512
+ ((soo_h2 << (8 * SooSlotIndex())) | kFirstCtrlBytesWithZeroes) |
513
+ (soo_h2 << (8 * kMirroredSooSlotIndex));
514
+ // Replace original and mirrored empty bytes for the new position.
515
+ // Result for new_offset 0 will look like:
516
+ // NHESNHEE
517
+ // Where H = soo_h2, N = H2(new_hash), E = kEmpty, S = kSentinel.
518
+ // Result for new_offset 2 will look like:
519
+ // EHNSEHNE
520
+ first_ctrl_bytes ^= (new_h2_xor_empty << (8 * new_offset));
521
+ size_t new_mirrored_offset = new_offset + kNewCapacity + 1;
522
+ first_ctrl_bytes ^= (new_h2_xor_empty << (8 * new_mirrored_offset));
523
+
524
+ // Fill last bytes with kEmpty.
525
+ std::memset(new_ctrl + kNewCapacity, static_cast<int8_t>(ctrl_t::kEmpty),
526
+ Group::kWidth);
527
+ // Overwrite the first 8 bytes with first_ctrl_bytes.
528
+ absl::little_endian::Store64(new_ctrl, first_ctrl_bytes);
529
+
530
+ // Example for group size 16:
531
+ // new_ctrl after 1st memset = ???EEEEEEEEEEEEEEEE
532
+ // new_offset 0:
533
+ // new_ctrl after 2nd store = NHESNHEEEEEEEEEEEEE
534
+ // new_offset 2:
535
+ // new_ctrl after 2nd store = EHNSEHNEEEEEEEEEEEE
536
+
537
+ // Example for group size 8:
538
+ // new_ctrl after 1st memset = ???EEEEEEEE
539
+ // new_offset 0:
540
+ // new_ctrl after 2nd store = NHESNHEEEEE
541
+ // new_offset 2:
542
+ // new_ctrl after 2nd store = EHNSEHNEEEE
543
+ }
544
+
322
545
  } // namespace
323
546
 
324
547
  void EraseMetaOnly(CommonFields& c, size_t index, size_t slot_size) {
325
- assert(IsFull(c.control()[index]) && "erasing a dangling iterator");
548
+ ABSL_SWISSTABLE_ASSERT(IsFull(c.control()[index]) &&
549
+ "erasing a dangling iterator");
326
550
  c.decrement_size();
327
551
  c.infoz().RecordErase();
328
552
 
@@ -333,14 +557,15 @@ void EraseMetaOnly(CommonFields& c, size_t index, size_t slot_size) {
333
557
  }
334
558
 
335
559
  c.growth_info().OverwriteFullAsDeleted();
336
- SetCtrl(c, index, ctrl_t::kDeleted, slot_size);
560
+ SetCtrlInLargeTable(c, index, ctrl_t::kDeleted, slot_size);
337
561
  }
338
562
 
339
- void ClearBackingArray(CommonFields& c, const PolicyFunctions& policy,
563
+ void ClearBackingArray(CommonFields& c,
564
+ const PolicyFunctions& __restrict policy, void* alloc,
340
565
  bool reuse, bool soo_enabled) {
341
- c.set_size(0);
342
566
  if (reuse) {
343
- assert(!soo_enabled || c.capacity() > SooCapacity());
567
+ c.set_size_to_zero();
568
+ ABSL_SWISSTABLE_ASSERT(!soo_enabled || c.capacity() > SooCapacity());
344
569
  ResetCtrl(c, policy.slot_size);
345
570
  ResetGrowthLeft(c);
346
571
  c.infoz().RecordStorageChanged(0, c.capacity());
@@ -349,17 +574,224 @@ void ClearBackingArray(CommonFields& c, const PolicyFunctions& policy,
349
574
  // infoz.
350
575
  c.infoz().RecordClearedReservation();
351
576
  c.infoz().RecordStorageChanged(0, soo_enabled ? SooCapacity() : 0);
352
- (*policy.dealloc)(c, policy);
577
+ c.infoz().Unregister();
578
+ (*policy.dealloc)(alloc, c.capacity(), c.control(), policy.slot_size,
579
+ policy.slot_align, c.has_infoz());
353
580
  c = soo_enabled ? CommonFields{soo_tag_t{}} : CommonFields{non_soo_tag_t{}};
354
581
  }
355
582
  }
356
583
 
357
- void HashSetResizeHelper::GrowIntoSingleGroupShuffleControlBytes(
358
- ctrl_t* __restrict new_ctrl, size_t new_capacity) const {
359
- assert(is_single_group(new_capacity));
584
+ namespace {
585
+
586
+ enum class ResizeNonSooMode {
587
+ kGuaranteedEmpty,
588
+ kGuaranteedAllocated,
589
+ };
590
+
591
+ // Iterates over full slots in old table, finds new positions for them and
592
+ // transfers the slots.
593
+ // This function is used for reserving or rehashing non-empty tables.
594
+ // This use case is rare so the function is type erased.
595
+ // Returns the total probe length.
596
+ size_t FindNewPositionsAndTransferSlots(
597
+ CommonFields& common, const PolicyFunctions& __restrict policy,
598
+ ctrl_t* old_ctrl, void* old_slots, size_t old_capacity) {
599
+ void* new_slots = common.slot_array();
600
+ const void* hash_fn = policy.hash_fn(common);
601
+ const size_t slot_size = policy.slot_size;
602
+
603
+ const auto insert_slot = [&](void* slot) {
604
+ size_t hash = policy.hash_slot(hash_fn, slot);
605
+ auto target = find_first_non_full(common, hash);
606
+ SetCtrl(common, target.offset, H2(hash), slot_size);
607
+ policy.transfer_n(&common, SlotAddress(new_slots, target.offset, slot_size),
608
+ slot, 1);
609
+ return target.probe_length;
610
+ };
611
+ size_t total_probe_length = 0;
612
+ for (size_t i = 0; i < old_capacity; ++i) {
613
+ if (IsFull(old_ctrl[i])) {
614
+ total_probe_length += insert_slot(old_slots);
615
+ }
616
+ old_slots = NextSlot(old_slots, slot_size);
617
+ }
618
+ return total_probe_length;
619
+ }
620
+
621
+ template <ResizeNonSooMode kMode>
622
+ void ResizeNonSooImpl(CommonFields& common,
623
+ const PolicyFunctions& __restrict policy,
624
+ size_t new_capacity, HashtablezInfoHandle infoz) {
625
+ ABSL_SWISSTABLE_ASSERT(IsValidCapacity(new_capacity));
626
+ ABSL_SWISSTABLE_ASSERT(new_capacity > policy.soo_capacity());
627
+
628
+ const size_t old_capacity = common.capacity();
629
+ [[maybe_unused]] ctrl_t* old_ctrl = common.control();
630
+ [[maybe_unused]] void* old_slots = common.slot_array();
631
+
632
+ const size_t slot_size = policy.slot_size;
633
+ const size_t slot_align = policy.slot_align;
634
+ const bool has_infoz = infoz.IsSampled();
635
+
636
+ common.set_capacity(new_capacity);
637
+ RawHashSetLayout layout(new_capacity, slot_size, slot_align, has_infoz);
638
+ void* alloc = policy.get_char_alloc(common);
639
+ char* mem = static_cast<char*>(policy.alloc(alloc, layout.alloc_size()));
640
+ const GenerationType old_generation = common.generation();
641
+ common.set_generation_ptr(
642
+ reinterpret_cast<GenerationType*>(mem + layout.generation_offset()));
643
+ common.set_generation(NextGeneration(old_generation));
644
+
645
+ ctrl_t* new_ctrl = reinterpret_cast<ctrl_t*>(mem + layout.control_offset());
646
+ common.set_control</*kGenerateSeed=*/true>(new_ctrl);
647
+ common.set_slots(mem + layout.slot_offset());
648
+
649
+ size_t total_probe_length = 0;
650
+ ResetCtrl(common, slot_size);
651
+ ABSL_SWISSTABLE_ASSERT(kMode != ResizeNonSooMode::kGuaranteedEmpty ||
652
+ old_capacity == policy.soo_capacity());
653
+ ABSL_SWISSTABLE_ASSERT(kMode != ResizeNonSooMode::kGuaranteedAllocated ||
654
+ old_capacity > 0);
655
+ if constexpr (kMode == ResizeNonSooMode::kGuaranteedAllocated) {
656
+ total_probe_length = FindNewPositionsAndTransferSlots(
657
+ common, policy, old_ctrl, old_slots, old_capacity);
658
+ (*policy.dealloc)(alloc, old_capacity, old_ctrl, slot_size, slot_align,
659
+ has_infoz);
660
+ ResetGrowthLeft(GetGrowthInfoFromControl(new_ctrl), new_capacity,
661
+ common.size());
662
+ } else {
663
+ GetGrowthInfoFromControl(new_ctrl).InitGrowthLeftNoDeleted(
664
+ CapacityToGrowth(new_capacity));
665
+ }
666
+
667
+ if (has_infoz) {
668
+ common.set_has_infoz();
669
+ infoz.RecordStorageChanged(common.size(), new_capacity);
670
+ infoz.RecordRehash(total_probe_length);
671
+ common.set_infoz(infoz);
672
+ }
673
+ }
674
+
675
+ void ResizeEmptyNonAllocatedTableImpl(CommonFields& common,
676
+ const PolicyFunctions& __restrict policy,
677
+ size_t new_capacity, bool force_infoz) {
678
+ ABSL_SWISSTABLE_ASSERT(IsValidCapacity(new_capacity));
679
+ ABSL_SWISSTABLE_ASSERT(new_capacity > policy.soo_capacity());
680
+ ABSL_SWISSTABLE_ASSERT(!force_infoz || policy.soo_enabled);
681
+ ABSL_SWISSTABLE_ASSERT(common.capacity() <= policy.soo_capacity());
682
+ ABSL_SWISSTABLE_ASSERT(common.empty());
683
+ const size_t slot_size = policy.slot_size;
684
+ HashtablezInfoHandle infoz;
685
+ const bool should_sample =
686
+ policy.is_hashtablez_eligible && (force_infoz || ShouldSampleNextTable());
687
+ if (ABSL_PREDICT_FALSE(should_sample)) {
688
+ infoz = ForcedTrySample(slot_size, policy.key_size, policy.value_size,
689
+ policy.soo_capacity());
690
+ }
691
+ ResizeNonSooImpl<ResizeNonSooMode::kGuaranteedEmpty>(common, policy,
692
+ new_capacity, infoz);
693
+ }
694
+
695
+ // If the table was SOO, initializes new control bytes and transfers slot.
696
+ // After transferring the slot, sets control and slots in CommonFields.
697
+ // It is rare to resize an SOO table with one element to a large size.
698
+ // Requires: `c` contains SOO data.
699
+ void InsertOldSooSlotAndInitializeControlBytes(
700
+ CommonFields& c, const PolicyFunctions& __restrict policy, size_t hash,
701
+ ctrl_t* new_ctrl, void* new_slots) {
702
+ ABSL_SWISSTABLE_ASSERT(c.size() == policy.soo_capacity());
703
+ ABSL_SWISSTABLE_ASSERT(policy.soo_enabled);
704
+ size_t new_capacity = c.capacity();
705
+
706
+ c.generate_new_seed();
707
+ size_t offset = probe(c.seed(), new_capacity, hash).offset();
708
+ offset = offset == new_capacity ? 0 : offset;
709
+ SanitizerPoisonMemoryRegion(new_slots, policy.slot_size * new_capacity);
710
+ void* target_slot = SlotAddress(new_slots, offset, policy.slot_size);
711
+ SanitizerUnpoisonMemoryRegion(target_slot, policy.slot_size);
712
+ policy.transfer_n(&c, target_slot, c.soo_data(), 1);
713
+ c.set_control</*kGenerateSeed=*/false>(new_ctrl);
714
+ c.set_slots(new_slots);
715
+ ResetCtrl(c, policy.slot_size);
716
+ SetCtrl(c, offset, H2(hash), policy.slot_size);
717
+ }
718
+
719
+ enum class ResizeFullSooTableSamplingMode {
720
+ kNoSampling,
721
+ // Force sampling. If the table was still not sampled, do not resize.
722
+ kForceSampleNoResizeIfUnsampled,
723
+ };
724
+
725
+ void AssertSoo([[maybe_unused]] CommonFields& common,
726
+ [[maybe_unused]] const PolicyFunctions& policy) {
727
+ ABSL_SWISSTABLE_ASSERT(policy.soo_enabled);
728
+ ABSL_SWISSTABLE_ASSERT(common.capacity() == policy.soo_capacity());
729
+ }
730
+ void AssertFullSoo([[maybe_unused]] CommonFields& common,
731
+ [[maybe_unused]] const PolicyFunctions& policy) {
732
+ AssertSoo(common, policy);
733
+ ABSL_SWISSTABLE_ASSERT(common.size() == policy.soo_capacity());
734
+ }
735
+
736
+ void ResizeFullSooTable(CommonFields& common,
737
+ const PolicyFunctions& __restrict policy,
738
+ size_t new_capacity,
739
+ ResizeFullSooTableSamplingMode sampling_mode) {
740
+ AssertFullSoo(common, policy);
741
+ const size_t slot_size = policy.slot_size;
742
+ const size_t slot_align = policy.slot_align;
743
+
744
+ HashtablezInfoHandle infoz;
745
+ if (sampling_mode ==
746
+ ResizeFullSooTableSamplingMode::kForceSampleNoResizeIfUnsampled) {
747
+ if (ABSL_PREDICT_FALSE(policy.is_hashtablez_eligible)) {
748
+ infoz = ForcedTrySample(slot_size, policy.key_size, policy.value_size,
749
+ policy.soo_capacity());
750
+ }
751
+
752
+ if (!infoz.IsSampled()) {
753
+ return;
754
+ }
755
+ }
756
+
757
+ const bool has_infoz = infoz.IsSampled();
758
+
759
+ common.set_capacity(new_capacity);
760
+
761
+ RawHashSetLayout layout(new_capacity, slot_size, slot_align, has_infoz);
762
+ void* alloc = policy.get_char_alloc(common);
763
+ char* mem = static_cast<char*>(policy.alloc(alloc, layout.alloc_size()));
764
+ const GenerationType old_generation = common.generation();
765
+ common.set_generation_ptr(
766
+ reinterpret_cast<GenerationType*>(mem + layout.generation_offset()));
767
+ common.set_generation(NextGeneration(old_generation));
768
+
769
+ // We do not set control and slots in CommonFields yet to avoid overriding
770
+ // SOO data.
771
+ ctrl_t* new_ctrl = reinterpret_cast<ctrl_t*>(mem + layout.control_offset());
772
+ void* new_slots = mem + layout.slot_offset();
773
+
774
+ const size_t soo_slot_hash =
775
+ policy.hash_slot(policy.hash_fn(common), common.soo_data());
776
+
777
+ InsertOldSooSlotAndInitializeControlBytes(common, policy, soo_slot_hash,
778
+ new_ctrl, new_slots);
779
+ ResetGrowthLeft(common);
780
+ if (has_infoz) {
781
+ common.set_has_infoz();
782
+ common.set_infoz(infoz);
783
+ infoz.RecordStorageChanged(common.size(), new_capacity);
784
+ }
785
+ }
786
+
787
+ void GrowIntoSingleGroupShuffleControlBytes(ctrl_t* __restrict old_ctrl,
788
+ size_t old_capacity,
789
+ ctrl_t* __restrict new_ctrl,
790
+ size_t new_capacity) {
791
+ ABSL_SWISSTABLE_ASSERT(is_single_group(new_capacity));
360
792
  constexpr size_t kHalfWidth = Group::kWidth / 2;
361
- ABSL_ASSUME(old_capacity_ < kHalfWidth);
362
- ABSL_ASSUME(old_capacity_ > 0);
793
+ ABSL_ASSUME(old_capacity < kHalfWidth);
794
+ ABSL_ASSUME(old_capacity > 0);
363
795
  static_assert(Group::kWidth == 8 || Group::kWidth == 16,
364
796
  "Group size is not supported.");
365
797
 
@@ -373,8 +805,7 @@ void HashSetResizeHelper::GrowIntoSingleGroupShuffleControlBytes(
373
805
  // Example:
374
806
  // old_ctrl = 012S012EEEEEEEEE...
375
807
  // copied_bytes = S012EEEE
376
- uint64_t copied_bytes =
377
- absl::little_endian::Load64(old_ctrl() + old_capacity_);
808
+ uint64_t copied_bytes = absl::little_endian::Load64(old_ctrl + old_capacity);
378
809
 
379
810
  // We change the sentinel byte to kEmpty before storing to both the start of
380
811
  // the new_ctrl, and past the end of the new_ctrl later for the new cloned
@@ -395,7 +826,8 @@ void HashSetResizeHelper::GrowIntoSingleGroupShuffleControlBytes(
395
826
 
396
827
  if (Group::kWidth == 8) {
397
828
  // With group size 8, we can grow with two write operations.
398
- assert(old_capacity_ < 8 && "old_capacity_ is too large for group size 8");
829
+ ABSL_SWISSTABLE_ASSERT(old_capacity < 8 &&
830
+ "old_capacity is too large for group size 8");
399
831
  absl::little_endian::Store64(new_ctrl, copied_bytes);
400
832
 
401
833
  static constexpr uint64_t kSentinal64 =
@@ -421,7 +853,7 @@ void HashSetResizeHelper::GrowIntoSingleGroupShuffleControlBytes(
421
853
  return;
422
854
  }
423
855
 
424
- assert(Group::kWidth == 16);
856
+ ABSL_SWISSTABLE_ASSERT(Group::kWidth == 16);
425
857
 
426
858
  // Fill the second half of the main control bytes with kEmpty.
427
859
  // For small capacity that may write into mirrored control bytes.
@@ -463,7 +895,6 @@ void HashSetResizeHelper::GrowIntoSingleGroupShuffleControlBytes(
463
895
  // >!
464
896
  // new_ctrl after 2nd store = E012EEESE012EEEEEEEEEEE
465
897
 
466
-
467
898
  // Example for growth capacity 7->15:
468
899
  // old_ctrl = 0123456S0123456EEEEEEEE
469
900
  // new_ctrl at the end = E0123456EEEEEEESE0123456EEEEEEE
@@ -478,58 +909,428 @@ void HashSetResizeHelper::GrowIntoSingleGroupShuffleControlBytes(
478
909
  // new_ctrl after 2nd store = E0123456EEEEEEESE0123456EEEEEEE
479
910
  }
480
911
 
481
- void HashSetResizeHelper::InitControlBytesAfterSoo(ctrl_t* new_ctrl, ctrl_t h2,
482
- size_t new_capacity) {
483
- assert(is_single_group(new_capacity));
484
- std::memset(new_ctrl, static_cast<int8_t>(ctrl_t::kEmpty),
485
- NumControlBytes(new_capacity));
486
- assert(HashSetResizeHelper::SooSlotIndex() == 1);
487
- // This allows us to avoid branching on had_soo_slot_.
488
- assert(had_soo_slot_ || h2 == ctrl_t::kEmpty);
489
- new_ctrl[1] = new_ctrl[new_capacity + 2] = h2;
912
+ // Size of the buffer we allocate on stack for storing probed elements in
913
+ // GrowToNextCapacity algorithm.
914
+ constexpr size_t kProbedElementsBufferSize = 512;
915
+
916
+ // Decodes information about probed elements from contiguous memory.
917
+ // Finds new position for each element and transfers it to the new slots.
918
+ // Returns the total probe length.
919
+ template <typename ProbedItem>
920
+ ABSL_ATTRIBUTE_NOINLINE size_t DecodeAndInsertImpl(
921
+ CommonFields& c, const PolicyFunctions& __restrict policy,
922
+ const ProbedItem* start, const ProbedItem* end, void* old_slots) {
923
+ const size_t new_capacity = c.capacity();
924
+
925
+ void* new_slots = c.slot_array();
926
+ ctrl_t* new_ctrl = c.control();
927
+ size_t total_probe_length = 0;
928
+
929
+ const size_t slot_size = policy.slot_size;
930
+ auto transfer_n = policy.transfer_n;
931
+
932
+ for (; start < end; ++start) {
933
+ const FindInfo target = find_first_non_full_from_h1(
934
+ new_ctrl, static_cast<size_t>(start->h1), new_capacity);
935
+ total_probe_length += target.probe_length;
936
+ const size_t old_index = static_cast<size_t>(start->source_offset);
937
+ const size_t new_i = target.offset;
938
+ ABSL_SWISSTABLE_ASSERT(old_index < new_capacity / 2);
939
+ ABSL_SWISSTABLE_ASSERT(new_i < new_capacity);
940
+ ABSL_SWISSTABLE_ASSERT(IsEmpty(new_ctrl[new_i]));
941
+ void* src_slot = SlotAddress(old_slots, old_index, slot_size);
942
+ void* dst_slot = SlotAddress(new_slots, new_i, slot_size);
943
+ SanitizerUnpoisonMemoryRegion(dst_slot, slot_size);
944
+ transfer_n(&c, dst_slot, src_slot, 1);
945
+ SetCtrlInLargeTable(c, new_i, static_cast<h2_t>(start->h2), slot_size);
946
+ }
947
+ return total_probe_length;
948
+ }
949
+
950
+ // Sentinel value for the start of marked elements.
951
+ // Signals that there are no marked elements.
952
+ constexpr size_t kNoMarkedElementsSentinel = ~size_t{};
953
+
954
+ // Process probed elements that did not fit into available buffers.
955
+ // We marked them in control bytes as kSentinel.
956
+ // Hash recomputation and full probing is done here.
957
+ // This use case should be extremely rare.
958
+ ABSL_ATTRIBUTE_NOINLINE size_t ProcessProbedMarkedElements(
959
+ CommonFields& c, const PolicyFunctions& __restrict policy, ctrl_t* old_ctrl,
960
+ void* old_slots, size_t start) {
961
+ size_t old_capacity = PreviousCapacity(c.capacity());
962
+ const size_t slot_size = policy.slot_size;
963
+ void* new_slots = c.slot_array();
964
+ size_t total_probe_length = 0;
965
+ const void* hash_fn = policy.hash_fn(c);
966
+ auto hash_slot = policy.hash_slot;
967
+ auto transfer_n = policy.transfer_n;
968
+ for (size_t old_index = start; old_index < old_capacity; ++old_index) {
969
+ if (old_ctrl[old_index] != ctrl_t::kSentinel) {
970
+ continue;
971
+ }
972
+ void* src_slot = SlotAddress(old_slots, old_index, slot_size);
973
+ const size_t hash = hash_slot(hash_fn, src_slot);
974
+ const FindInfo target = find_first_non_full(c, hash);
975
+ total_probe_length += target.probe_length;
976
+ const size_t new_i = target.offset;
977
+ void* dst_slot = SlotAddress(new_slots, new_i, slot_size);
978
+ SetCtrlInLargeTable(c, new_i, H2(hash), slot_size);
979
+ transfer_n(&c, dst_slot, src_slot, 1);
980
+ }
981
+ return total_probe_length;
982
+ }
983
+
984
+ // The largest old capacity for which it is guaranteed that all probed elements
985
+ // fit in ProbedItemEncoder's local buffer.
986
+ // For such tables, `encode_probed_element` is trivial.
987
+ constexpr size_t kMaxLocalBufferOldCapacity =
988
+ kProbedElementsBufferSize / sizeof(ProbedItem4Bytes) - 1;
989
+ static_assert(IsValidCapacity(kMaxLocalBufferOldCapacity));
990
+ constexpr size_t kMaxLocalBufferNewCapacity =
991
+ NextCapacity(kMaxLocalBufferOldCapacity);
992
+ static_assert(kMaxLocalBufferNewCapacity <= ProbedItem4Bytes::kMaxNewCapacity);
993
+ static_assert(NextCapacity(kMaxLocalBufferNewCapacity) <=
994
+ ProbedItem4Bytes::kMaxNewCapacity);
995
+
996
+ // Initializes mirrored control bytes after
997
+ // transfer_unprobed_elements_to_next_capacity.
998
+ void InitializeMirroredControlBytes(ctrl_t* new_ctrl, size_t new_capacity) {
999
+ std::memcpy(new_ctrl + new_capacity,
1000
+ // We own GrowthInfo just before control bytes. So it is ok
1001
+ // to read one byte from it.
1002
+ new_ctrl - 1, Group::kWidth);
490
1003
  new_ctrl[new_capacity] = ctrl_t::kSentinel;
491
1004
  }
492
1005
 
493
- void HashSetResizeHelper::GrowIntoSingleGroupShuffleTransferableSlots(
494
- void* new_slots, size_t slot_size) const {
495
- ABSL_ASSUME(old_capacity_ > 0);
496
- SanitizerUnpoisonMemoryRegion(old_slots(), slot_size * old_capacity_);
497
- std::memcpy(SlotAddress(new_slots, 1, slot_size), old_slots(),
498
- slot_size * old_capacity_);
1006
+ // Encodes probed elements into available memory.
1007
+ // At first, a local (on stack) buffer is used. The size of the buffer is
1008
+ // kProbedElementsBufferSize bytes.
1009
+ // When the local buffer is full, we switch to `control_` buffer. We are allowed
1010
+ // to overwrite `control_` buffer till the `source_offset` byte. In case we have
1011
+ // no space in `control_` buffer, we fallback to a naive algorithm for all the
1012
+ // rest of the probed elements. We mark elements as kSentinel in control bytes
1013
+ // and later process them fully. See ProcessMarkedElements for details. It
1014
+ // should be extremely rare.
1015
+ template <typename ProbedItemType,
1016
+ // If true, we only use the local buffer and never switch to the
1017
+ // control buffer.
1018
+ bool kGuaranteedFitToBuffer = false>
1019
+ class ProbedItemEncoder {
1020
+ public:
1021
+ using ProbedItem = ProbedItemType;
1022
+ explicit ProbedItemEncoder(ctrl_t* control) : control_(control) {}
1023
+
1024
+ // Encode item into the best available location.
1025
+ void EncodeItem(ProbedItem item) {
1026
+ if (ABSL_PREDICT_FALSE(!kGuaranteedFitToBuffer && pos_ >= end_)) {
1027
+ return ProcessEncodeWithOverflow(item);
1028
+ }
1029
+ ABSL_SWISSTABLE_ASSERT(pos_ < end_);
1030
+ *pos_ = item;
1031
+ ++pos_;
1032
+ }
1033
+
1034
+ // Decodes information about probed elements from all available sources.
1035
+ // Finds new position for each element and transfers it to the new slots.
1036
+ // Returns the total probe length.
1037
+ size_t DecodeAndInsertToTable(CommonFields& common,
1038
+ const PolicyFunctions& __restrict policy,
1039
+ void* old_slots) const {
1040
+ if (pos_ == buffer_) {
1041
+ return 0;
1042
+ }
1043
+ if constexpr (kGuaranteedFitToBuffer) {
1044
+ return DecodeAndInsertImpl(common, policy, buffer_, pos_, old_slots);
1045
+ }
1046
+ size_t total_probe_length = DecodeAndInsertImpl(
1047
+ common, policy, buffer_,
1048
+ local_buffer_full_ ? buffer_ + kBufferSize : pos_, old_slots);
1049
+ if (!local_buffer_full_) {
1050
+ return total_probe_length;
1051
+ }
1052
+ total_probe_length +=
1053
+ DecodeAndInsertToTableOverflow(common, policy, old_slots);
1054
+ return total_probe_length;
1055
+ }
1056
+
1057
+ private:
1058
+ static ProbedItem* AlignToNextItem(void* ptr) {
1059
+ return reinterpret_cast<ProbedItem*>(AlignUpTo(
1060
+ reinterpret_cast<uintptr_t>(ptr), alignof(ProbedItem)));
1061
+ }
1062
+
1063
+ ProbedItem* OverflowBufferStart() const {
1064
+ // We reuse GrowthInfo memory as well.
1065
+ return AlignToNextItem(control_ - ControlOffset(/*has_infoz=*/false));
1066
+ }
1067
+
1068
+ // Encodes item when previously allocated buffer is full.
1069
+ // At first that happens when local buffer is full.
1070
+ // We switch from the local buffer to the control buffer.
1071
+ // Every time this function is called, the available buffer is extended till
1072
+ // `item.source_offset` byte in the control buffer.
1073
+ // After the buffer is extended, this function wouldn't be called till the
1074
+ // buffer is exhausted.
1075
+ //
1076
+ // If there's no space in the control buffer, we fallback to naive algorithm
1077
+ // and mark probed elements as kSentinel in the control buffer. In this case,
1078
+ // we will call this function for every subsequent probed element.
1079
+ ABSL_ATTRIBUTE_NOINLINE void ProcessEncodeWithOverflow(ProbedItem item) {
1080
+ if (!local_buffer_full_) {
1081
+ local_buffer_full_ = true;
1082
+ pos_ = OverflowBufferStart();
1083
+ }
1084
+ const size_t source_offset = static_cast<size_t>(item.source_offset);
1085
+ // We are in fallback mode so we can't reuse control buffer anymore.
1086
+ // Probed elements are marked as kSentinel in the control buffer.
1087
+ if (ABSL_PREDICT_FALSE(marked_elements_starting_position_ !=
1088
+ kNoMarkedElementsSentinel)) {
1089
+ control_[source_offset] = ctrl_t::kSentinel;
1090
+ return;
1091
+ }
1092
+ // Refresh the end pointer to the new available position.
1093
+ // Invariant: if pos < end, then we have at least sizeof(ProbedItem) bytes
1094
+ // to write.
1095
+ end_ = control_ + source_offset + 1 - sizeof(ProbedItem);
1096
+ if (ABSL_PREDICT_TRUE(pos_ < end_)) {
1097
+ *pos_ = item;
1098
+ ++pos_;
1099
+ return;
1100
+ }
1101
+ control_[source_offset] = ctrl_t::kSentinel;
1102
+ marked_elements_starting_position_ = source_offset;
1103
+ // Now we will always fall down to `ProcessEncodeWithOverflow`.
1104
+ ABSL_SWISSTABLE_ASSERT(pos_ >= end_);
1105
+ }
1106
+
1107
+ // Decodes information about probed elements from control buffer and processes
1108
+ // marked elements.
1109
+ // Finds new position for each element and transfers it to the new slots.
1110
+ // Returns the total probe length.
1111
+ ABSL_ATTRIBUTE_NOINLINE size_t DecodeAndInsertToTableOverflow(
1112
+ CommonFields& common, const PolicyFunctions& __restrict policy,
1113
+ void* old_slots) const {
1114
+ ABSL_SWISSTABLE_ASSERT(local_buffer_full_ &&
1115
+ "must not be called when local buffer is not full");
1116
+ size_t total_probe_length = DecodeAndInsertImpl(
1117
+ common, policy, OverflowBufferStart(), pos_, old_slots);
1118
+ if (ABSL_PREDICT_TRUE(marked_elements_starting_position_ ==
1119
+ kNoMarkedElementsSentinel)) {
1120
+ return total_probe_length;
1121
+ }
1122
+ total_probe_length +=
1123
+ ProcessProbedMarkedElements(common, policy, control_, old_slots,
1124
+ marked_elements_starting_position_);
1125
+ return total_probe_length;
1126
+ }
1127
+
1128
+ static constexpr size_t kBufferSize =
1129
+ kProbedElementsBufferSize / sizeof(ProbedItem);
1130
+ ProbedItem buffer_[kBufferSize];
1131
+ // If local_buffer_full_ is false, then pos_/end_ are in the local buffer,
1132
+ // otherwise, they're in the overflow buffer.
1133
+ ProbedItem* pos_ = buffer_;
1134
+ const void* end_ = buffer_ + kBufferSize;
1135
+ ctrl_t* const control_;
1136
+ size_t marked_elements_starting_position_ = kNoMarkedElementsSentinel;
1137
+ bool local_buffer_full_ = false;
1138
+ };
1139
+
1140
+ // Grows to next capacity with specified encoder type.
1141
+ // Encoder is used to store probed elements that are processed later.
1142
+ // Different encoder is used depending on the capacity of the table.
1143
+ // Returns total probe length.
1144
+ template <typename Encoder>
1145
+ size_t GrowToNextCapacity(CommonFields& common,
1146
+ const PolicyFunctions& __restrict policy,
1147
+ ctrl_t* old_ctrl, void* old_slots) {
1148
+ using ProbedItem = typename Encoder::ProbedItem;
1149
+ ABSL_SWISSTABLE_ASSERT(common.capacity() <= ProbedItem::kMaxNewCapacity);
1150
+ Encoder encoder(old_ctrl);
1151
+ policy.transfer_unprobed_elements_to_next_capacity(
1152
+ common, old_ctrl, old_slots, &encoder,
1153
+ [](void* probed_storage, h2_t h2, size_t source_offset, size_t h1) {
1154
+ auto encoder_ptr = static_cast<Encoder*>(probed_storage);
1155
+ encoder_ptr->EncodeItem(ProbedItem(h2, source_offset, h1));
1156
+ });
1157
+ InitializeMirroredControlBytes(common.control(), common.capacity());
1158
+ return encoder.DecodeAndInsertToTable(common, policy, old_slots);
499
1159
  }
500
1160
 
501
- void HashSetResizeHelper::GrowSizeIntoSingleGroupTransferable(
502
- CommonFields& c, size_t slot_size) {
503
- assert(old_capacity_ < Group::kWidth / 2);
504
- assert(is_single_group(c.capacity()));
505
- assert(IsGrowingIntoSingleGroupApplicable(old_capacity_, c.capacity()));
1161
+ // Grows to next capacity for relatively small tables so that even if all
1162
+ // elements are probed, we don't need to overflow the local buffer.
1163
+ // Returns total probe length.
1164
+ size_t GrowToNextCapacityThatFitsInLocalBuffer(
1165
+ CommonFields& common, const PolicyFunctions& __restrict policy,
1166
+ ctrl_t* old_ctrl, void* old_slots) {
1167
+ ABSL_SWISSTABLE_ASSERT(common.capacity() <= kMaxLocalBufferNewCapacity);
1168
+ return GrowToNextCapacity<
1169
+ ProbedItemEncoder<ProbedItem4Bytes, /*kGuaranteedFitToBuffer=*/true>>(
1170
+ common, policy, old_ctrl, old_slots);
1171
+ }
506
1172
 
507
- GrowIntoSingleGroupShuffleControlBytes(c.control(), c.capacity());
508
- GrowIntoSingleGroupShuffleTransferableSlots(c.slot_array(), slot_size);
1173
+ // Grows to next capacity with different encodings. Returns total probe length.
1174
+ // These functions are useful to simplify profile analysis.
1175
+ size_t GrowToNextCapacity4BytesEncoder(CommonFields& common,
1176
+ const PolicyFunctions& __restrict policy,
1177
+ ctrl_t* old_ctrl, void* old_slots) {
1178
+ return GrowToNextCapacity<ProbedItemEncoder<ProbedItem4Bytes>>(
1179
+ common, policy, old_ctrl, old_slots);
1180
+ }
1181
+ size_t GrowToNextCapacity8BytesEncoder(CommonFields& common,
1182
+ const PolicyFunctions& __restrict policy,
1183
+ ctrl_t* old_ctrl, void* old_slots) {
1184
+ return GrowToNextCapacity<ProbedItemEncoder<ProbedItem8Bytes>>(
1185
+ common, policy, old_ctrl, old_slots);
1186
+ }
1187
+ size_t GrowToNextCapacity16BytesEncoder(
1188
+ CommonFields& common, const PolicyFunctions& __restrict policy,
1189
+ ctrl_t* old_ctrl, void* old_slots) {
1190
+ return GrowToNextCapacity<ProbedItemEncoder<ProbedItem16Bytes>>(
1191
+ common, policy, old_ctrl, old_slots);
1192
+ }
509
1193
 
510
- // We poison since GrowIntoSingleGroupShuffleTransferableSlots
511
- // may leave empty slots unpoisoned.
512
- PoisonSingleGroupEmptySlots(c, slot_size);
1194
+ // Grows to next capacity for tables with relatively large capacity so that we
1195
+ // can't guarantee that all probed elements fit in the local buffer. Returns
1196
+ // total probe length.
1197
+ size_t GrowToNextCapacityOverflowLocalBuffer(
1198
+ CommonFields& common, const PolicyFunctions& __restrict policy,
1199
+ ctrl_t* old_ctrl, void* old_slots) {
1200
+ const size_t new_capacity = common.capacity();
1201
+ if (ABSL_PREDICT_TRUE(new_capacity <= ProbedItem4Bytes::kMaxNewCapacity)) {
1202
+ return GrowToNextCapacity4BytesEncoder(common, policy, old_ctrl, old_slots);
1203
+ }
1204
+ if (ABSL_PREDICT_TRUE(new_capacity <= ProbedItem8Bytes::kMaxNewCapacity)) {
1205
+ return GrowToNextCapacity8BytesEncoder(common, policy, old_ctrl, old_slots);
1206
+ }
1207
+ // 16 bytes encoding supports the maximum swisstable capacity.
1208
+ return GrowToNextCapacity16BytesEncoder(common, policy, old_ctrl, old_slots);
513
1209
  }
514
1210
 
515
- void HashSetResizeHelper::TransferSlotAfterSoo(CommonFields& c,
516
- size_t slot_size) {
517
- assert(was_soo_);
518
- assert(had_soo_slot_);
519
- assert(is_single_group(c.capacity()));
520
- std::memcpy(SlotAddress(c.slot_array(), SooSlotIndex(), slot_size),
521
- old_soo_data(), slot_size);
522
- PoisonSingleGroupEmptySlots(c, slot_size);
1211
+ // Dispatches to the appropriate `GrowToNextCapacity*` function based on the
1212
+ // capacity of the table. Returns total probe length.
1213
+ ABSL_ATTRIBUTE_NOINLINE
1214
+ size_t GrowToNextCapacityDispatch(CommonFields& common,
1215
+ const PolicyFunctions& __restrict policy,
1216
+ ctrl_t* old_ctrl, void* old_slots) {
1217
+ const size_t new_capacity = common.capacity();
1218
+ if (ABSL_PREDICT_TRUE(new_capacity <= kMaxLocalBufferNewCapacity)) {
1219
+ return GrowToNextCapacityThatFitsInLocalBuffer(common, policy, old_ctrl,
1220
+ old_slots);
1221
+ } else {
1222
+ return GrowToNextCapacityOverflowLocalBuffer(common, policy, old_ctrl,
1223
+ old_slots);
1224
+ }
523
1225
  }
524
1226
 
525
- namespace {
1227
+ // Grows to next capacity and prepares insert for the given new_hash.
1228
+ // Returns the offset of the new element.
1229
+ size_t GrowToNextCapacityAndPrepareInsert(
1230
+ CommonFields& common, const PolicyFunctions& __restrict policy,
1231
+ size_t new_hash) {
1232
+ ABSL_SWISSTABLE_ASSERT(common.growth_left() == 0);
1233
+ const size_t old_capacity = common.capacity();
1234
+ ABSL_SWISSTABLE_ASSERT(old_capacity == 0 ||
1235
+ old_capacity > policy.soo_capacity());
1236
+
1237
+ const size_t new_capacity = NextCapacity(old_capacity);
1238
+ ABSL_SWISSTABLE_ASSERT(IsValidCapacity(new_capacity));
1239
+ ABSL_SWISSTABLE_ASSERT(new_capacity > policy.soo_capacity());
1240
+
1241
+ ctrl_t* old_ctrl = common.control();
1242
+ void* old_slots = common.slot_array();
1243
+
1244
+ common.set_capacity(new_capacity);
1245
+ const size_t slot_size = policy.slot_size;
1246
+ const size_t slot_align = policy.slot_align;
1247
+ HashtablezInfoHandle infoz;
1248
+ if (old_capacity > 0) {
1249
+ infoz = common.infoz();
1250
+ } else {
1251
+ const bool should_sample =
1252
+ policy.is_hashtablez_eligible && ShouldSampleNextTable();
1253
+ if (ABSL_PREDICT_FALSE(should_sample)) {
1254
+ infoz = ForcedTrySample(slot_size, policy.key_size, policy.value_size,
1255
+ policy.soo_capacity());
1256
+ }
1257
+ }
1258
+ const bool has_infoz = infoz.IsSampled();
1259
+
1260
+ RawHashSetLayout layout(new_capacity, slot_size, slot_align, has_infoz);
1261
+ void* alloc = policy.get_char_alloc(common);
1262
+ char* mem = static_cast<char*>(policy.alloc(alloc, layout.alloc_size()));
1263
+ const GenerationType old_generation = common.generation();
1264
+ common.set_generation_ptr(
1265
+ reinterpret_cast<GenerationType*>(mem + layout.generation_offset()));
1266
+ common.set_generation(NextGeneration(old_generation));
1267
+
1268
+ ctrl_t* new_ctrl = reinterpret_cast<ctrl_t*>(mem + layout.control_offset());
1269
+ void* new_slots = mem + layout.slot_offset();
1270
+ common.set_control</*kGenerateSeed=*/false>(new_ctrl);
1271
+ common.set_slots(new_slots);
1272
+ SanitizerPoisonMemoryRegion(new_slots, new_capacity * slot_size);
1273
+
1274
+ h2_t new_h2 = H2(new_hash);
1275
+ size_t total_probe_length = 0;
1276
+ FindInfo find_info;
1277
+ if (old_capacity == 0) {
1278
+ static_assert(NextCapacity(0) == 1);
1279
+ InitializeSingleElementControlBytes(new_h2, new_ctrl);
1280
+ common.generate_new_seed();
1281
+ find_info = FindInfo{0, 0};
1282
+ SanitizerUnpoisonMemoryRegion(new_slots, slot_size);
1283
+ } else {
1284
+ if (ABSL_PREDICT_TRUE(is_single_group(new_capacity))) {
1285
+ GrowIntoSingleGroupShuffleControlBytes(old_ctrl, old_capacity, new_ctrl,
1286
+ new_capacity);
1287
+ // Single group tables have all slots full on resize. So we can transfer
1288
+ // all slots without checking the control bytes.
1289
+ ABSL_SWISSTABLE_ASSERT(common.size() == old_capacity);
1290
+ auto* target = NextSlot(new_slots, slot_size);
1291
+ SanitizerUnpoisonMemoryRegion(target, old_capacity * slot_size);
1292
+ policy.transfer_n(&common, target, old_slots, old_capacity);
1293
+ // We put the new element either at the beginning or at the end of the
1294
+ // table with approximately equal probability.
1295
+ size_t offset = SingleGroupTableH1(new_hash, common.seed()) & 1
1296
+ ? 0
1297
+ : new_capacity - 1;
1298
+
1299
+ ABSL_SWISSTABLE_ASSERT(IsEmpty(new_ctrl[offset]));
1300
+ SetCtrlInSingleGroupTable(common, offset, new_h2, policy.slot_size);
1301
+ find_info = FindInfo{offset, 0};
1302
+ } else {
1303
+ total_probe_length =
1304
+ GrowToNextCapacityDispatch(common, policy, old_ctrl, old_slots);
1305
+ find_info = find_first_non_full(common, new_hash);
1306
+ SetCtrlInLargeTable(common, find_info.offset, new_h2, policy.slot_size);
1307
+ }
1308
+ ABSL_SWISSTABLE_ASSERT(old_capacity > policy.soo_capacity());
1309
+ (*policy.dealloc)(alloc, old_capacity, old_ctrl, slot_size, slot_align,
1310
+ has_infoz);
1311
+ }
1312
+ PrepareInsertCommon(common);
1313
+ ResetGrowthLeft(GetGrowthInfoFromControl(new_ctrl), new_capacity,
1314
+ common.size());
1315
+
1316
+ if (ABSL_PREDICT_FALSE(has_infoz)) {
1317
+ common.set_has_infoz();
1318
+ infoz.RecordStorageChanged(common.size() - 1, new_capacity);
1319
+ infoz.RecordRehash(total_probe_length);
1320
+ infoz.RecordInsert(new_hash, find_info.probe_length);
1321
+ common.set_infoz(infoz);
1322
+ }
1323
+ return find_info.offset;
1324
+ }
526
1325
 
527
1326
  // Called whenever the table needs to vacate empty slots either by removing
528
- // tombstones via rehash or growth.
1327
+ // tombstones via rehash or growth to next capacity.
529
1328
  ABSL_ATTRIBUTE_NOINLINE
530
- FindInfo FindInsertPositionWithGrowthOrRehash(CommonFields& common, size_t hash,
531
- const PolicyFunctions& policy) {
1329
+ size_t RehashOrGrowToNextCapacityAndPrepareInsert(
1330
+ CommonFields& common, const PolicyFunctions& __restrict policy,
1331
+ size_t new_hash) {
532
1332
  const size_t cap = common.capacity();
1333
+ ABSL_ASSUME(cap > 0);
533
1334
  if (cap > Group::kWidth &&
534
1335
  // Do these calculations in 64-bit to avoid overflow.
535
1336
  common.size() * uint64_t{32} <= cap * uint64_t{25}) {
@@ -574,97 +1375,467 @@ FindInfo FindInsertPositionWithGrowthOrRehash(CommonFields& common, size_t hash,
574
1375
  // 762 | 149836 0.37 13 | 148559 0.74 190
575
1376
  // 807 | 149736 0.39 14 | 151107 0.39 14
576
1377
  // 852 | 150204 0.42 15 | 151019 0.42 15
577
- DropDeletesWithoutResize(common, policy);
1378
+ return DropDeletesWithoutResizeAndPrepareInsert(common, policy, new_hash);
578
1379
  } else {
579
1380
  // Otherwise grow the container.
580
- policy.resize(common, NextCapacity(cap), HashtablezInfoHandle{});
1381
+ return GrowToNextCapacityAndPrepareInsert(common, policy, new_hash);
1382
+ }
1383
+ }
1384
+
1385
+ // Slow path for PrepareInsertNonSoo that is called when the table has deleted
1386
+ // slots or need to be resized or rehashed.
1387
+ size_t PrepareInsertNonSooSlow(CommonFields& common,
1388
+ const PolicyFunctions& __restrict policy,
1389
+ size_t hash) {
1390
+ const GrowthInfo growth_info = common.growth_info();
1391
+ ABSL_SWISSTABLE_ASSERT(!growth_info.HasNoDeletedAndGrowthLeft());
1392
+ if (ABSL_PREDICT_TRUE(growth_info.HasNoGrowthLeftAndNoDeleted())) {
1393
+ // Table without deleted slots (>95% cases) that needs to be resized.
1394
+ ABSL_SWISSTABLE_ASSERT(growth_info.HasNoDeleted() &&
1395
+ growth_info.GetGrowthLeft() == 0);
1396
+ return GrowToNextCapacityAndPrepareInsert(common, policy, hash);
1397
+ }
1398
+ if (ABSL_PREDICT_FALSE(growth_info.HasNoGrowthLeftAssumingMayHaveDeleted())) {
1399
+ // Table with deleted slots that needs to be rehashed or resized.
1400
+ return RehashOrGrowToNextCapacityAndPrepareInsert(common, policy, hash);
1401
+ }
1402
+ // Table with deleted slots that has space for the inserting element.
1403
+ FindInfo target = find_first_non_full(common, hash);
1404
+ PrepareInsertCommon(common);
1405
+ common.growth_info().OverwriteControlAsFull(common.control()[target.offset]);
1406
+ SetCtrlInLargeTable(common, target.offset, H2(hash), policy.slot_size);
1407
+ common.infoz().RecordInsert(hash, target.probe_length);
1408
+ return target.offset;
1409
+ }
1410
+
1411
+ // Resizes empty non-allocated SOO table to NextCapacity(SooCapacity()),
1412
+ // forces the table to be sampled and prepares the insert.
1413
+ // SOO tables need to switch from SOO to heap in order to store the infoz.
1414
+ // Requires:
1415
+ // 1. `c.capacity() == SooCapacity()`.
1416
+ // 2. `c.empty()`.
1417
+ ABSL_ATTRIBUTE_NOINLINE size_t
1418
+ GrowEmptySooTableToNextCapacityForceSamplingAndPrepareInsert(
1419
+ CommonFields& common, const PolicyFunctions& __restrict policy,
1420
+ size_t new_hash) {
1421
+ ResizeEmptyNonAllocatedTableImpl(common, policy, NextCapacity(SooCapacity()),
1422
+ /*force_infoz=*/true);
1423
+ PrepareInsertCommon(common);
1424
+ common.growth_info().OverwriteEmptyAsFull();
1425
+ SetCtrlInSingleGroupTable(common, SooSlotIndex(), H2(new_hash),
1426
+ policy.slot_size);
1427
+ common.infoz().RecordInsert(new_hash, /*distance_from_desired=*/0);
1428
+ return SooSlotIndex();
1429
+ }
1430
+
1431
+ // Resizes empty non-allocated table to the capacity to fit new_size elements.
1432
+ // Requires:
1433
+ // 1. `c.capacity() == policy.soo_capacity()`.
1434
+ // 2. `c.empty()`.
1435
+ // 3. `new_size > policy.soo_capacity()`.
1436
+ // The table will be attempted to be sampled.
1437
+ void ReserveEmptyNonAllocatedTableToFitNewSize(
1438
+ CommonFields& common, const PolicyFunctions& __restrict policy,
1439
+ size_t new_size) {
1440
+ ValidateMaxSize(new_size, policy.slot_size);
1441
+ ABSL_ASSUME(new_size > 0);
1442
+ ResizeEmptyNonAllocatedTableImpl(common, policy, SizeToCapacity(new_size),
1443
+ /*force_infoz=*/false);
1444
+ // This is after resize, to ensure that we have completed the allocation
1445
+ // and have potentially sampled the hashtable.
1446
+ common.infoz().RecordReservation(new_size);
1447
+ }
1448
+
1449
+ // Type erased version of raw_hash_set::reserve for tables that have an
1450
+ // allocated backing array.
1451
+ //
1452
+ // Requires:
1453
+ // 1. `c.capacity() > policy.soo_capacity()` OR `!c.empty()`.
1454
+ // Reserving already allocated tables is considered to be a rare case.
1455
+ ABSL_ATTRIBUTE_NOINLINE void ReserveAllocatedTable(
1456
+ CommonFields& common, const PolicyFunctions& __restrict policy,
1457
+ size_t new_size) {
1458
+ const size_t cap = common.capacity();
1459
+ ValidateMaxSize(new_size, policy.slot_size);
1460
+ ABSL_ASSUME(new_size > 0);
1461
+ const size_t new_capacity = SizeToCapacity(new_size);
1462
+ if (cap == policy.soo_capacity()) {
1463
+ ABSL_SWISSTABLE_ASSERT(!common.empty());
1464
+ ResizeFullSooTable(common, policy, new_capacity,
1465
+ ResizeFullSooTableSamplingMode::kNoSampling);
1466
+ } else {
1467
+ ABSL_SWISSTABLE_ASSERT(cap > policy.soo_capacity());
1468
+ // TODO(b/382423690): consider using GrowToNextCapacity, when applicable.
1469
+ ResizeAllocatedTableWithSeedChange(common, policy, new_capacity);
581
1470
  }
582
- // This function is typically called with tables containing deleted slots.
583
- // The table will be big and `FindFirstNonFullAfterResize` will always
584
- // fallback to `find_first_non_full`. So using `find_first_non_full` directly.
585
- return find_first_non_full(common, hash);
1471
+ common.infoz().RecordReservation(new_size);
586
1472
  }
587
1473
 
588
1474
  } // namespace
589
1475
 
590
- const void* GetHashRefForEmptyHasher(const CommonFields& common) {
1476
+ void* GetRefForEmptyClass(CommonFields& common) {
591
1477
  // Empty base optimization typically make the empty base class address to be
592
1478
  // the same as the first address of the derived class object.
593
- // But we generally assume that for empty hasher we can return any valid
1479
+ // But we generally assume that for empty classes we can return any valid
594
1480
  // pointer.
595
1481
  return &common;
596
1482
  }
597
1483
 
598
- FindInfo HashSetResizeHelper::FindFirstNonFullAfterResize(const CommonFields& c,
599
- size_t old_capacity,
600
- size_t hash) {
601
- size_t new_capacity = c.capacity();
602
- if (!IsGrowingIntoSingleGroupApplicable(old_capacity, new_capacity)) {
603
- return find_first_non_full(c, hash);
1484
+ void ResizeAllocatedTableWithSeedChange(
1485
+ CommonFields& common, const PolicyFunctions& __restrict policy,
1486
+ size_t new_capacity) {
1487
+ ResizeNonSooImpl<ResizeNonSooMode::kGuaranteedAllocated>(
1488
+ common, policy, new_capacity, common.infoz());
1489
+ }
1490
+
1491
+ void ReserveEmptyNonAllocatedTableToFitBucketCount(
1492
+ CommonFields& common, const PolicyFunctions& __restrict policy,
1493
+ size_t bucket_count) {
1494
+ size_t new_capacity = NormalizeCapacity(bucket_count);
1495
+ ValidateMaxSize(CapacityToGrowth(new_capacity), policy.slot_size);
1496
+ ResizeEmptyNonAllocatedTableImpl(common, policy, new_capacity,
1497
+ /*force_infoz=*/false);
1498
+ }
1499
+
1500
+ // Resizes a full SOO table to the NextCapacity(SooCapacity()).
1501
+ template <size_t SooSlotMemcpySize, bool TransferUsesMemcpy>
1502
+ size_t GrowSooTableToNextCapacityAndPrepareInsert(
1503
+ CommonFields& common, const PolicyFunctions& __restrict policy,
1504
+ size_t new_hash, ctrl_t soo_slot_ctrl) {
1505
+ AssertSoo(common, policy);
1506
+ if (ABSL_PREDICT_FALSE(soo_slot_ctrl == ctrl_t::kEmpty)) {
1507
+ // The table is empty, it is only used for forced sampling of SOO tables.
1508
+ return GrowEmptySooTableToNextCapacityForceSamplingAndPrepareInsert(
1509
+ common, policy, new_hash);
604
1510
  }
1511
+ ABSL_SWISSTABLE_ASSERT(common.size() == policy.soo_capacity());
1512
+ static constexpr size_t kNewCapacity = NextCapacity(SooCapacity());
1513
+ const size_t slot_size = policy.slot_size;
1514
+ const size_t slot_align = policy.slot_align;
1515
+ common.set_capacity(kNewCapacity);
1516
+
1517
+ // Since the table is not empty, it will not be sampled.
1518
+ // The decision to sample was already made during the first insertion.
1519
+ RawHashSetLayout layout(kNewCapacity, slot_size, slot_align,
1520
+ /*has_infoz=*/false);
1521
+ void* alloc = policy.get_char_alloc(common);
1522
+ char* mem = static_cast<char*>(policy.alloc(alloc, layout.alloc_size()));
1523
+ const GenerationType old_generation = common.generation();
1524
+ common.set_generation_ptr(
1525
+ reinterpret_cast<GenerationType*>(mem + layout.generation_offset()));
1526
+ common.set_generation(NextGeneration(old_generation));
1527
+
1528
+ // We do not set control and slots in CommonFields yet to avoid overriding
1529
+ // SOO data.
1530
+ ctrl_t* new_ctrl = reinterpret_cast<ctrl_t*>(mem + layout.control_offset());
1531
+ void* new_slots = mem + layout.slot_offset();
605
1532
 
606
- // We put the new element either at the beginning or at the end of the table
607
- // with approximately equal probability.
608
- size_t offset =
609
- SingleGroupTableH1(hash, c.control()) & 1 ? 0 : new_capacity - 1;
1533
+ PrepareInsertCommon(common);
1534
+ ABSL_SWISSTABLE_ASSERT(common.size() == 2);
1535
+ GetGrowthInfoFromControl(new_ctrl).InitGrowthLeftNoDeleted(kNewCapacity - 2);
1536
+ common.generate_new_seed();
610
1537
 
611
- assert(IsEmpty(c.control()[offset]));
612
- return FindInfo{offset, 0};
1538
+ // After resize from capacity 1 to 3, we always have exactly the slot with
1539
+ // index 1 occupied, so we need to insert either at index 0 or index 2.
1540
+ static_assert(SooSlotIndex() == 1);
1541
+ const size_t offset = SingleGroupTableH1(new_hash, common.seed()) & 2;
1542
+ InitializeThreeElementsControlBytesAfterSoo(soo_slot_ctrl, new_hash, offset,
1543
+ new_ctrl);
1544
+
1545
+ SanitizerPoisonMemoryRegion(new_slots, slot_size * kNewCapacity);
1546
+ void* target_slot = SlotAddress(new_slots, SooSlotIndex(), slot_size);
1547
+ SanitizerUnpoisonMemoryRegion(target_slot, slot_size);
1548
+ if constexpr (TransferUsesMemcpy) {
1549
+ // Target slot is placed at index 1, but capacity is at
1550
+ // minimum 3. So we are allowed to copy at least twice as much
1551
+ // memory.
1552
+ static_assert(SooSlotIndex() == 1);
1553
+ static_assert(SooSlotMemcpySize > 0);
1554
+ static_assert(SooSlotMemcpySize <= MaxSooSlotSize());
1555
+ ABSL_SWISSTABLE_ASSERT(SooSlotMemcpySize <= 2 * slot_size);
1556
+ ABSL_SWISSTABLE_ASSERT(SooSlotMemcpySize >= slot_size);
1557
+ void* next_slot = SlotAddress(target_slot, 1, slot_size);
1558
+ SanitizerUnpoisonMemoryRegion(next_slot, SooSlotMemcpySize - slot_size);
1559
+ std::memcpy(target_slot, common.soo_data(), SooSlotMemcpySize);
1560
+ SanitizerPoisonMemoryRegion(next_slot, SooSlotMemcpySize - slot_size);
1561
+ } else {
1562
+ static_assert(SooSlotMemcpySize == 0);
1563
+ policy.transfer_n(&common, target_slot, common.soo_data(), 1);
1564
+ }
1565
+ // Seed was already generated above.
1566
+ common.set_control</*kGenerateSeed=*/false>(new_ctrl);
1567
+ common.set_slots(new_slots);
1568
+
1569
+ common.infoz().RecordInsert(new_hash, /*distance_from_desired=*/0);
1570
+ SanitizerUnpoisonMemoryRegion(SlotAddress(new_slots, offset, slot_size),
1571
+ slot_size);
1572
+ return offset;
613
1573
  }
614
1574
 
615
- size_t PrepareInsertNonSoo(CommonFields& common, size_t hash, FindInfo target,
616
- const PolicyFunctions& policy) {
617
- // When there are no deleted slots in the table
618
- // and growth_left is positive, we can insert at the first
619
- // empty slot in the probe sequence (target).
620
- const bool use_target_hint =
621
- // Optimization is disabled when generations are enabled.
622
- // We have to rehash even sparse tables randomly in such mode.
623
- !SwisstableGenerationsEnabled() &&
624
- common.growth_info().HasNoDeletedAndGrowthLeft();
625
- if (ABSL_PREDICT_FALSE(!use_target_hint)) {
626
- // Notes about optimized mode when generations are disabled:
627
- // We do not enter this branch if table has no deleted slots
628
- // and growth_left is positive.
629
- // We enter this branch in the following cases listed in decreasing
630
- // frequency:
631
- // 1. Table without deleted slots (>95% cases) that needs to be resized.
632
- // 2. Table with deleted slots that has space for the inserting element.
633
- // 3. Table with deleted slots that needs to be rehashed or resized.
634
- if (ABSL_PREDICT_TRUE(common.growth_info().HasNoGrowthLeftAndNoDeleted())) {
635
- const size_t old_capacity = common.capacity();
636
- policy.resize(common, NextCapacity(old_capacity), HashtablezInfoHandle{});
637
- target = HashSetResizeHelper::FindFirstNonFullAfterResize(
638
- common, old_capacity, hash);
639
- } else {
640
- // Note: the table may have no deleted slots here when generations
641
- // are enabled.
642
- const bool rehash_for_bug_detection =
643
- common.should_rehash_for_bug_detection_on_insert();
644
- if (rehash_for_bug_detection) {
645
- // Move to a different heap allocation in order to detect bugs.
646
- const size_t cap = common.capacity();
647
- policy.resize(common,
648
- common.growth_left() > 0 ? cap : NextCapacity(cap),
649
- HashtablezInfoHandle{});
1575
+ void GrowFullSooTableToNextCapacityForceSampling(
1576
+ CommonFields& common, const PolicyFunctions& __restrict policy) {
1577
+ AssertFullSoo(common, policy);
1578
+ ResizeFullSooTable(
1579
+ common, policy, NextCapacity(SooCapacity()),
1580
+ ResizeFullSooTableSamplingMode::kForceSampleNoResizeIfUnsampled);
1581
+ }
1582
+
1583
+ void Rehash(CommonFields& common, const PolicyFunctions& __restrict policy,
1584
+ size_t n) {
1585
+ const size_t cap = common.capacity();
1586
+
1587
+ auto clear_backing_array = [&]() {
1588
+ ClearBackingArray(common, policy, policy.get_char_alloc(common),
1589
+ /*reuse=*/false, policy.soo_enabled);
1590
+ };
1591
+
1592
+ const size_t slot_size = policy.slot_size;
1593
+
1594
+ if (n == 0) {
1595
+ if (cap <= policy.soo_capacity()) return;
1596
+ if (common.empty()) {
1597
+ clear_backing_array();
1598
+ return;
1599
+ }
1600
+ if (common.size() <= policy.soo_capacity()) {
1601
+ // When the table is already sampled, we keep it sampled.
1602
+ if (common.infoz().IsSampled()) {
1603
+ static constexpr size_t kInitialSampledCapacity =
1604
+ NextCapacity(SooCapacity());
1605
+ if (cap > kInitialSampledCapacity) {
1606
+ ResizeAllocatedTableWithSeedChange(common, policy,
1607
+ kInitialSampledCapacity);
1608
+ }
1609
+ // This asserts that we didn't lose sampling coverage in `resize`.
1610
+ ABSL_SWISSTABLE_ASSERT(common.infoz().IsSampled());
1611
+ return;
650
1612
  }
651
- if (ABSL_PREDICT_TRUE(common.growth_left() > 0)) {
652
- target = find_first_non_full(common, hash);
1613
+ ABSL_SWISSTABLE_ASSERT(slot_size <= sizeof(HeapOrSoo));
1614
+ ABSL_SWISSTABLE_ASSERT(policy.slot_align <= alignof(HeapOrSoo));
1615
+ HeapOrSoo tmp_slot(uninitialized_tag_t{});
1616
+ size_t begin_offset = FindFirstFullSlot(0, cap, common.control());
1617
+ policy.transfer_n(
1618
+ &common, &tmp_slot,
1619
+ SlotAddress(common.slot_array(), begin_offset, slot_size), 1);
1620
+ clear_backing_array();
1621
+ policy.transfer_n(&common, common.soo_data(), &tmp_slot, 1);
1622
+ common.set_full_soo();
1623
+ return;
1624
+ }
1625
+ }
1626
+
1627
+ ValidateMaxSize(n, policy.slot_size);
1628
+ // bitor is a faster way of doing `max` here. We will round up to the next
1629
+ // power-of-2-minus-1, so bitor is good enough.
1630
+ const size_t new_capacity =
1631
+ NormalizeCapacity(n | SizeToCapacity(common.size()));
1632
+ // n == 0 unconditionally rehashes as per the standard.
1633
+ if (n == 0 || new_capacity > cap) {
1634
+ if (cap == policy.soo_capacity()) {
1635
+ if (common.empty()) {
1636
+ ResizeEmptyNonAllocatedTableImpl(common, policy, new_capacity,
1637
+ /*force_infoz=*/false);
653
1638
  } else {
654
- target = FindInsertPositionWithGrowthOrRehash(common, hash, policy);
1639
+ ResizeFullSooTable(common, policy, new_capacity,
1640
+ ResizeFullSooTableSamplingMode::kNoSampling);
655
1641
  }
1642
+ } else {
1643
+ ResizeAllocatedTableWithSeedChange(common, policy, new_capacity);
1644
+ }
1645
+ // This is after resize, to ensure that we have completed the allocation
1646
+ // and have potentially sampled the hashtable.
1647
+ common.infoz().RecordReservation(n);
1648
+ }
1649
+ }
1650
+
1651
+ void Copy(CommonFields& common, const PolicyFunctions& __restrict policy,
1652
+ const CommonFields& other,
1653
+ absl::FunctionRef<void(void*, const void*)> copy_fn) {
1654
+ const size_t size = other.size();
1655
+ ABSL_SWISSTABLE_ASSERT(size > 0);
1656
+ const size_t soo_capacity = policy.soo_capacity();
1657
+ const size_t slot_size = policy.slot_size;
1658
+ if (size <= soo_capacity) {
1659
+ ABSL_SWISSTABLE_ASSERT(size == 1);
1660
+ common.set_full_soo();
1661
+ const void* other_slot =
1662
+ other.capacity() <= soo_capacity
1663
+ ? other.soo_data()
1664
+ : SlotAddress(
1665
+ other.slot_array(),
1666
+ FindFirstFullSlot(0, other.capacity(), other.control()),
1667
+ slot_size);
1668
+ copy_fn(common.soo_data(), other_slot);
1669
+
1670
+ if (policy.is_hashtablez_eligible && ShouldSampleNextTable()) {
1671
+ GrowFullSooTableToNextCapacityForceSampling(common, policy);
656
1672
  }
1673
+ return;
1674
+ }
1675
+
1676
+ ReserveTableToFitNewSize(common, policy, size);
1677
+ auto infoz = common.infoz();
1678
+ ABSL_SWISSTABLE_ASSERT(other.capacity() > soo_capacity);
1679
+ const size_t cap = common.capacity();
1680
+ ABSL_SWISSTABLE_ASSERT(cap > soo_capacity);
1681
+ // Note about single group tables:
1682
+ // 1. It is correct to have any order of elements.
1683
+ // 2. Order has to be non deterministic.
1684
+ // 3. We are assigning elements with arbitrary `shift` starting from
1685
+ // `capacity + shift` position.
1686
+ // 4. `shift` must be coprime with `capacity + 1` in order to be able to use
1687
+ // modular arithmetic to traverse all positions, instead of cycling
1688
+ // through a subset of positions. Odd numbers are coprime with any
1689
+ // `capacity + 1` (2^N).
1690
+ size_t offset = cap;
1691
+ const size_t shift = is_single_group(cap) ? (common.seed().seed() | 1) : 0;
1692
+ const void* hash_fn = policy.hash_fn(common);
1693
+ auto hasher = policy.hash_slot;
1694
+ IterateOverFullSlotsImpl(
1695
+ other, slot_size, [&](const ctrl_t* that_ctrl, void* that_slot) {
1696
+ if (shift == 0) {
1697
+ // Big tables case. Position must be searched via probing.
1698
+ // The table is guaranteed to be empty, so we can do faster than
1699
+ // a full `insert`.
1700
+ const size_t hash = (*hasher)(hash_fn, that_slot);
1701
+ FindInfo target = find_first_non_full(common, hash);
1702
+ infoz.RecordInsert(hash, target.probe_length);
1703
+ offset = target.offset;
1704
+ } else {
1705
+ // Small tables case. Next position is computed via shift.
1706
+ offset = (offset + shift) & cap;
1707
+ }
1708
+ const h2_t h2 = static_cast<h2_t>(*that_ctrl);
1709
+ // We rely on the hash not changing for small tables.
1710
+ ABSL_SWISSTABLE_ASSERT(
1711
+ H2((*hasher)(hash_fn, that_slot)) == h2 &&
1712
+ "hash function value changed unexpectedly during the copy");
1713
+ SetCtrl(common, offset, h2, slot_size);
1714
+ copy_fn(SlotAddress(common.slot_array(), offset, slot_size), that_slot);
1715
+ common.maybe_increment_generation_on_insert();
1716
+ });
1717
+ if (shift != 0) {
1718
+ // On small table copy we do not record individual inserts.
1719
+ // RecordInsert requires hash, but it is unknown for small tables.
1720
+ infoz.RecordStorageChanged(size, cap);
1721
+ }
1722
+ common.increment_size(size);
1723
+ common.growth_info().OverwriteManyEmptyAsFull(size);
1724
+ }
1725
+
1726
+ void ReserveTableToFitNewSize(CommonFields& common,
1727
+ const PolicyFunctions& __restrict policy,
1728
+ size_t new_size) {
1729
+ common.reset_reserved_growth(new_size);
1730
+ common.set_reservation_size(new_size);
1731
+ ABSL_SWISSTABLE_ASSERT(new_size > policy.soo_capacity());
1732
+ const size_t cap = common.capacity();
1733
+ if (ABSL_PREDICT_TRUE(common.empty() && cap <= policy.soo_capacity())) {
1734
+ return ReserveEmptyNonAllocatedTableToFitNewSize(common, policy, new_size);
1735
+ }
1736
+
1737
+ ABSL_SWISSTABLE_ASSERT(!common.empty() || cap > policy.soo_capacity());
1738
+ ABSL_SWISSTABLE_ASSERT(cap > 0);
1739
+ const size_t max_size_before_growth =
1740
+ cap <= policy.soo_capacity() ? policy.soo_capacity()
1741
+ : common.size() + common.growth_left();
1742
+ if (new_size <= max_size_before_growth) {
1743
+ return;
1744
+ }
1745
+ ReserveAllocatedTable(common, policy, new_size);
1746
+ }
1747
+
1748
+ size_t PrepareInsertNonSoo(CommonFields& common,
1749
+ const PolicyFunctions& __restrict policy,
1750
+ size_t hash, FindInfo target) {
1751
+ const bool rehash_for_bug_detection =
1752
+ common.should_rehash_for_bug_detection_on_insert() &&
1753
+ // Required to allow use of ResizeAllocatedTable.
1754
+ common.capacity() > 0;
1755
+ if (rehash_for_bug_detection) {
1756
+ // Move to a different heap allocation in order to detect bugs.
1757
+ const size_t cap = common.capacity();
1758
+ ResizeAllocatedTableWithSeedChange(
1759
+ common, policy, common.growth_left() > 0 ? cap : NextCapacity(cap));
1760
+ target = find_first_non_full(common, hash);
1761
+ }
1762
+
1763
+ const GrowthInfo growth_info = common.growth_info();
1764
+ // When there are no deleted slots in the table
1765
+ // and growth_left is positive, we can insert at the first
1766
+ // empty slot in the probe sequence (target).
1767
+ if (ABSL_PREDICT_FALSE(!growth_info.HasNoDeletedAndGrowthLeft())) {
1768
+ return PrepareInsertNonSooSlow(common, policy, hash);
657
1769
  }
658
1770
  PrepareInsertCommon(common);
659
- common.growth_info().OverwriteControlAsFull(common.control()[target.offset]);
1771
+ common.growth_info().OverwriteEmptyAsFull();
660
1772
  SetCtrl(common, target.offset, H2(hash), policy.slot_size);
661
1773
  common.infoz().RecordInsert(hash, target.probe_length);
662
1774
  return target.offset;
663
1775
  }
664
1776
 
665
- void HashTableSizeOverflow() {
666
- ABSL_RAW_LOG(FATAL, "Hash table size overflow");
1777
+ namespace {
1778
+ // Returns true if the following is true
1779
+ // 1. OptimalMemcpySizeForSooSlotTransfer(left) >
1780
+ // OptimalMemcpySizeForSooSlotTransfer(left - 1)
1781
+ // 2. OptimalMemcpySizeForSooSlotTransfer(left) are equal for all i in [left,
1782
+ // right].
1783
+ // This function is used to verify that we have all the possible template
1784
+ // instantiations for GrowFullSooTableToNextCapacity.
1785
+ // With this verification the problem may be detected at compile time instead of
1786
+ // link time.
1787
+ constexpr bool VerifyOptimalMemcpySizeForSooSlotTransferRange(size_t left,
1788
+ size_t right) {
1789
+ size_t optimal_size_for_range = OptimalMemcpySizeForSooSlotTransfer(left);
1790
+ if (optimal_size_for_range <= OptimalMemcpySizeForSooSlotTransfer(left - 1)) {
1791
+ return false;
1792
+ }
1793
+ for (size_t i = left + 1; i <= right; ++i) {
1794
+ if (OptimalMemcpySizeForSooSlotTransfer(i) != optimal_size_for_range) {
1795
+ return false;
1796
+ }
1797
+ }
1798
+ return true;
667
1799
  }
1800
+ } // namespace
1801
+
1802
+ // Extern template instantiation for inline function.
1803
+ template size_t TryFindNewIndexWithoutProbing(size_t h1, size_t old_index,
1804
+ size_t old_capacity,
1805
+ ctrl_t* new_ctrl,
1806
+ size_t new_capacity);
1807
+
1808
+ // We need to instantiate ALL possible template combinations because we define
1809
+ // the function in the cc file.
1810
+ template size_t GrowSooTableToNextCapacityAndPrepareInsert<0, false>(
1811
+ CommonFields&, const PolicyFunctions&, size_t, ctrl_t);
1812
+ template size_t GrowSooTableToNextCapacityAndPrepareInsert<
1813
+ OptimalMemcpySizeForSooSlotTransfer(1), true>(CommonFields&,
1814
+ const PolicyFunctions&,
1815
+ size_t, ctrl_t);
1816
+
1817
+ static_assert(VerifyOptimalMemcpySizeForSooSlotTransferRange(2, 3));
1818
+ template size_t GrowSooTableToNextCapacityAndPrepareInsert<
1819
+ OptimalMemcpySizeForSooSlotTransfer(3), true>(CommonFields&,
1820
+ const PolicyFunctions&,
1821
+ size_t, ctrl_t);
1822
+
1823
+ static_assert(VerifyOptimalMemcpySizeForSooSlotTransferRange(4, 8));
1824
+ template size_t GrowSooTableToNextCapacityAndPrepareInsert<
1825
+ OptimalMemcpySizeForSooSlotTransfer(8), true>(CommonFields&,
1826
+ const PolicyFunctions&,
1827
+ size_t, ctrl_t);
1828
+
1829
+ #if UINTPTR_MAX == UINT32_MAX
1830
+ static_assert(MaxSooSlotSize() == 8);
1831
+ #else
1832
+ static_assert(VerifyOptimalMemcpySizeForSooSlotTransferRange(9, 16));
1833
+ template size_t GrowSooTableToNextCapacityAndPrepareInsert<
1834
+ OptimalMemcpySizeForSooSlotTransfer(16), true>(CommonFields&,
1835
+ const PolicyFunctions&,
1836
+ size_t, ctrl_t);
1837
+ static_assert(MaxSooSlotSize() == 16);
1838
+ #endif
668
1839
 
669
1840
  } // namespace container_internal
670
1841
  ABSL_NAMESPACE_END