Skip to content

add blazingly fast instance_cache implementation for K2 - #1683

Open
denisichh wants to merge 19 commits into
masterfrom
dzubarev/k2/add-instance-cache
Open

add blazingly fast instance_cache implementation for K2#1683
denisichh wants to merge 19 commits into
masterfrom
dzubarev/k2/add-instance-cache

Conversation

@denisichh

Copy link
Copy Markdown
Contributor

Rewrites instance_cache_store/fetch/update_ttl/delete in the K2 runtime: instead of msgpack-serializing and shipping instances to a separate cache process over RPC, instances are now deep-copied directly into shared memory via the platform's k2::alloc_shared_memory/publish_shared_memory/get_shared_memory.

Storage layout: class_name_hash(u64) | class_instance shell | inner data. On fetch, the block is reinterpreted in place rather than deserialized.

Key pieces

  • New "construct-in-place" API on core types (class_instance::clone_in/alloc, array::allocation/copy_in, string::copy_in) -- lets objects be constructed directly into a caller-provided memory span instead of the script memory pool.
  • New shared CRTP visitor base kphp::visitors::instance_deep_basic_visitor (runtime-common/stdlib/visitors/instance deep-basic-visitor.h), factored out of the legacy runtime's copy processor so both runtimes share the same traversal logic. Legacy's InstanceReferencesCountingVisitor/InstanceDeepCopyVisitor/InstanceDeepDestroyVisitor now build on it too.
  • Two new K2 visitors: instance_deep_copy_visitor (copies an instance graph into a caller-provided block, pinning copies with ExtraRefCnt::for_instance_cache) and instance_deep_estimate_size_visitor (computes the exact size needed upfront so the shared-memory block can be allocated once).
  • Alignment: added virtual_builtin_alignof() (dispatches to the dynamic type's alignment for polymorphic clones) plus alignment() helpers on class_instance/array/string, and a padding-aware align_for_chunk(size, align) used with std::align() when carving memory from a pool.
  • Removed is_serializable/@kphp-serializable compiler checks for instance-cache calls.

@denisichh denisichh self-assigned this Aug 27, 2026
@denisichh denisichh added runtime Feature related to runtime compiler Feature related to compiler k2 Affects compiler or runtime in K2 mode kphp Affects compiler or runtime in default mode (not K2) labels Aug 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler Feature related to compiler k2 Affects compiler or runtime in K2 mode kphp Affects compiler or runtime in default mode (not K2) runtime Feature related to runtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant