Skip to content

Conversation

@e11sy
Copy link
Contributor

@e11sy e11sy commented Oct 16, 2025

Problem

We need to cache large json objects to decrease number db calls and to reduce cpu and memory usage

Solution

Use lru cache in band with hashed objects as a keys

Why lru?

  • very scalable solution (instead of widely used memoization libs), it allows to control size of the cache, ttl for keys
  • we need lru to avoid redundant db calls and heavy weight ops for most recently used set of args (least recently used would be removed from the cache)

Why crypto module is changed?

  • i am using blake2b512 cause it is prefered for large json objects hashing and it is presented in default js crypto lib see

Todo

  • probably lodash.memoize lib could be replaced with this implementation
  • tests
  • lint

Risks

  • probably hashing of large jsons could be the bottleneck (we could spend more resources for hashing than for db calls) — higher cpu usage
  • higher memo usage (cause we store ~50 large objects for each memoized method of the worker)

all of the risks could be solved by the configuration of the cache max elements (i've set to 50 just for example, should be descussed), maybe we can use maxSize of the lru-cache and allow dynamically decide whenever to add a new object to the cache or not

also ttl value should be discussed, but it is placed in env

@neSpecc neSpecc requested a review from Copilot October 19, 2025 22:37
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces LRU-based memoization for the JavaScript worker to reduce database calls and improve performance when processing source maps and function context extraction. The implementation uses a custom decorator with configurable cache size and TTL.

  • Adds a new @memoize decorator with LRU cache support and hashing strategies
  • Updates crypto utility to support Blake2b hashing for large JSON objects
  • Applies memoization to loadSourceMapFile and getFunctionContext methods

Reviewed Changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 4 comments.

File Description
lib/memoize/index.ts New LRU-based memoization decorator with hash and concat key strategies
lib/utils/crypto.ts Enhanced hash function with Blake2b support and configurable digest formats
workers/javascript/src/index.ts Applied memoization to source map loading and function context methods
workers/javascript/tests/index.test.ts Added comprehensive tests for memoization behavior and GridFS mocking

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

neSpecc
neSpecc previously approved these changes Oct 29, 2025
@neSpecc neSpecc self-requested a review October 29, 2025 22:20
@e11sy e11sy merged commit fe4b341 into master Nov 3, 2025
3 of 5 checks passed
@e11sy e11sy deleted the imp/memoize-js-worker branch November 3, 2025 14:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants