Skip to content

Conversation

@wlewNV
Copy link
Contributor

@wlewNV wlewNV commented Jan 28, 2026

Fixes #758

[Work-in-Progress]
PLACEHOLDER DESCRIPITON

@wlewNV
Copy link
Contributor Author

wlewNV commented Jan 29, 2026

Here is a small benchmark I ran with the change:

import timeit, statistics
from slangpy.slangpy import unpack_arg  # C++ version

# Test objects - typical args that don't have get_this
test_args = [42, 3.14, 'hello', [1, 2, 3], {'a': 1, 'b': 2}, (1, 2)]

def bench():
    for arg in test_args:
        unpack_arg(arg)

for _ in range(1000): bench()  # warmup
times = [(timeit.timeit(bench, number=10000) / 10000) * 1e9 for _ in range(5000)]
print(f'Mean: {statistics.mean(times):.2f} ns, Stdev: {statistics.stdev(times):.2f} ns')

For 5000 iterations × 10k calls each, 6 args per call, I get the following results:

  • main (uncached nb::hasattr): 2136.67 ns ± 20.69 ns
  • perf_win_2 (cached type lookup): 578.03 ns ± 10.30 ns

We are seeing a ~73% improvement (~1559 ns saved per call)

@ccummingsNV
Copy link
Contributor

ccummingsNV commented Jan 29, 2026

Hi - that's a good start, but the optimization we have in mind is a bit more in depth.

The idea here is to eliminate the need for get_this and update_this altogether, by making a base type from which a user can inherit. we could have the get_this/update_this checks right at the end as a fallback, but we want to avoid ever paying this cost really - any microseconds is too many microseconds

In your test case, I'd have expected you to see a saving in the case of a dictionary. The other types should bail out before hitting the get_this/update_this checks.

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.

Small Performance Win: Replace get_this / update_this probing

2 participants