Skip to content

add blazingly fast instance_cache implementation for K2 - #1683

Merged
denisichh merged 37 commits into
masterfrom
dzubarev/k2/add-instance-cache
Sep 15, 2026
Merged

denisichh merged 37 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
@denisichh
denisichh force-pushed the dzubarev/k2/add-instance-cache branch from 454d684 to 80eabdd Compare August 28, 2026 09:43
@denisichh
denisichh force-pushed the dzubarev/k2/add-instance-cache branch from 4ebc228 to 81cee3f Compare September 8, 2026 09:16
Comment thread compiler/code-gen/declarations.cpp
Comment thread compiler/code-gen/declarations.cpp Outdated
Comment thread runtime-common/core/class-instance/class-instance-decl.inl Outdated
Comment thread runtime-common/core/class-instance/class-instance-decl.inl Outdated
Comment thread compiler/code-gen/declarations.cpp Outdated
Comment thread runtime-common/core/class-instance/class-instance-decl.inl Outdated
@denisichh
denisichh force-pushed the dzubarev/k2/add-instance-cache branch from 6f6d523 to a82bd97 Compare September 11, 2026 12:09
Comment thread runtime-light/stdlib/instance-cache/instance-cache-functions.h Outdated
Comment thread runtime-light/stdlib/instance-cache/instance-cache-functions.h Outdated
Comment thread runtime-light/stdlib/instance-cache/instance-cache-functions.h Outdated
Comment thread runtime-light/stdlib/instance-cache/instance-cache-functions.h
Comment thread runtime-light/stdlib/instance-cache/instance-cache-functions.h Outdated
@denisichh
denisichh force-pushed the dzubarev/k2/add-instance-cache branch from 7958a63 to 725a207 Compare September 15, 2026 14:48

@PetrShumilov PetrShumilov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

@denisichh
denisichh merged commit 96693f1 into master Sep 15, 2026
8 checks passed
@denisichh
denisichh deleted the dzubarev/k2/add-instance-cache branch September 15, 2026 15:35
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.

3 participants