# Kwargs... and allocations

**URL:** <https://discourse.julialang.org/t/kwargs-and-allocations/129183>\
**Category:** Performance\
**Tags:** allocations\
**Created:** [May 20, 2025, 5:19pm UTC](https://discourse.julialang.org/t/kwargs-and-allocations/129183 "2025-05-20T17:19:23Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Allan\_Baker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/allan_baker/32/42645_2.png) [@Allan\_Baker](https://discourse.julialang.org/u/Allan_Baker)\
**Post date:** [May 20, 2025, 5:19pm UTC](https://discourse.julialang.org/t/kwargs-and-allocations/129183/1 "2025-05-20T17:19:24Z")

</div>

I recently was trying to overload some real-time code (meaning I don’t want any GC or allocations).  
I needed to add in some kwargs to the help with overloading some intermediate functions.

This causes allocations because of the named tuples. I ended up resorting to a custom settings structure to handle the job of the kwargs. Didn’t know if there was a better way to do this kind of thing when wanting the flexibility of the defaults of the kwargs but not the allocations and GC.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [May 20, 2025, 5:25pm UTC](https://discourse.julialang.org/t/kwargs-and-allocations/129183/2 "2025-05-20T17:25:44Z")

</div>

Just avoid keyword arguments if you need allocation free code. While this is a small inconvenience, best embrace it. I always pass a settings struct to the constructor of most of my modeling and control components. Sometimes even two:

```julia
@with_kw mutable struct Winch @deftype Float64
    wcs::WCSettings
    set::Settings
    wm::AsyncMachine = AsyncMachine(set)
    v_set = 0 # input
    force = 0 # input
    acc = 0 # output
    speed = 0 # output; reel-out speed; only state of this model
end

```

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [May 20, 2025, 5:30pm UTC](https://discourse.julialang.org/t/kwargs-and-allocations/129183/3 "2025-05-20T17:30:29Z")

</div>

Can you give an example that leads to these allocations?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 20, 2025, 5:43pm UTC](https://discourse.julialang.org/t/kwargs-and-allocations/129183/4 "2025-05-20T17:43:48Z")

</div>

What do you mean, allocations from kwargs? That’s news to me.

---

<div class="post-metadata">

**Author:** ![Allan\_Baker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/allan_baker/32/42645_2.png) [@Allan\_Baker](https://discourse.julialang.org/u/Allan_Baker)\
**Post date:** [May 20, 2025, 5:45pm UTC](https://discourse.julialang.org/t/kwargs-and-allocations/129183/5 "2025-05-20T17:45:09Z")

</div>

Not currently as the example I have is quite complex. I haven’t whittled it down to a MWE. I find debugging allocations to be incredibly tedious in general in julia.

Right now for example, I have a script that I can include which sets up a module with the exact same code I am running in an @testset runtests.jl section. I have removed the kwargs and am using the settings function as suggested by ufechner7 and also ChatGPT earlier today (which is what I have recoded.)

It does not allocate when I run my tester function in the standalone module. The same code inside @testset allocates. There are some const ref pointers I use to share memory to avoid allocations. ChatGPT thinks those could be an issue. But I’ve followed its suggestions. I’m in version 1.11.5 julia.

If anyone has any guesses on where I can look without a MWE, thank you. If it goes on for much longer, I’ll try to figure out a MWE.

To check where things are going, I have to go into the profiler and hope I can understand through pprof what might be happening. Without the code failing the same way… it is very challenging.

Anything the julia community to help in debugging these allocation free code sections, would be great!

---

<div class="post-metadata">

**Author:** ![Allan\_Baker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/allan_baker/32/42645_2.png) [@Allan\_Baker](https://discourse.julialang.org/u/Allan_Baker)\
**Post date:** [May 20, 2025, 5:46pm UTC](https://discourse.julialang.org/t/kwargs-and-allocations/129183/6 "2025-05-20T17:46:21Z")

</div>

apparently the splatting and slurping into the Named Tuples can cause allocations. At least that is what ChatGPT claims.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 20, 2025, 5:47pm UTC](https://discourse.julialang.org/t/kwargs-and-allocations/129183/7 "2025-05-20T17:47:36Z")

</div>

I wouldn’t necessarily trust that. I mean, I’m sure it can happen, but it should be avoidable.

---

<div class="post-metadata">

**Author:** ![Allan\_Baker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/allan_baker/32/42645_2.png) [@Allan\_Baker](https://discourse.julialang.org/u/Allan_Baker)\
**Post date:** [May 20, 2025, 5:48pm UTC](https://discourse.julialang.org/t/kwargs-and-allocations/129183/8 "2025-05-20T17:48:35Z")

</div>

I’m wondering if I’ve introduced some sort of type instability. Checking that now.

---

<div class="post-metadata">

**Author:** ![danielwe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielwe/32/35657_2.png) [@danielwe](https://discourse.julialang.org/u/danielwe)\
**Post date:** [May 20, 2025, 5:55pm UTC](https://discourse.julialang.org/t/kwargs-and-allocations/129183/9 "2025-05-20T17:55:44Z")

</div>

> [@Allan\_Baker](#):
>
> apparently the splatting and slurping into the Named Tuples can cause allocations

Are you splatting and slurping back and forth between namedtuples and kwargs yourself, or is the claim that the implementation of kwargs involves problematic splatting and slurping under the hood?

---

<div class="post-metadata">

**Author:** ![Allan\_Baker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/allan_baker/32/42645_2.png) [@Allan\_Baker](https://discourse.julialang.org/u/Allan_Baker)\
**Post date:** [May 20, 2025, 6:01pm UTC](https://discourse.julialang.org/t/kwargs-and-allocations/129183/10 "2025-05-20T18:01:03Z")

</div>

> [@danielwe](#):
>
> kwargs

I’m doing the splatting and slurping on overloaded functions so I can just pass the kwargs down to the next level. These intermediate functions are just setting up the underlying function.

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [May 20, 2025, 6:02pm UTC](https://discourse.julialang.org/t/kwargs-and-allocations/129183/11 "2025-05-20T18:02:33Z")

</div>

Can you maybe describe what data types are in the kwargs? IIRC it’s possible that if you are passing `Function`s or `DataType`s that maybe there could be issues.

Even just some pseudocode that also explains your issue further could be useful while you work on an MWE.

---

<div class="post-metadata">

**Author:** ![danielwe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielwe/32/35657_2.png) [@danielwe](https://discourse.julialang.org/u/danielwe)\
**Post date:** [May 20, 2025, 6:03pm UTC](https://discourse.julialang.org/t/kwargs-and-allocations/129183/12 "2025-05-20T18:03:20Z")

</div>

I see. It’s easy to end up with type instabilities in that kind of code. Would have to see an example to propose a fix/workaround.

---

<div class="post-metadata">

**Author:** ![Allan\_Baker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/allan_baker/32/42645_2.png) [@Allan\_Baker](https://discourse.julialang.org/u/Allan_Baker)\
**Post date:** [May 20, 2025, 6:18pm UTC](https://discourse.julialang.org/t/kwargs-and-allocations/129183/13 "2025-05-20T18:18:29Z")

</div>

One unusual type I pass in is an abstract type that is basically there to force a dispatch choice on the underlying function.

```julia
abstract type AbstractInterpolationMode end

abstract type InterpMode1 <: AbstractInterpolationMode end
abstract type InterpMode2 <: AbstractInterpolationMode end

```

This was one of the Kwargs that was passed to the top level function, and it defaulted to one of those.

Then there was a function call that looked something like this:

```julia
interpolationHelper!(::Type{InterpMode1}, o, t, p, f; kwargs...) = interpolateMode1!(o, t, p, f; kwargs...)

interpolationHelper!(::Type{InterpMode2}, o, t, p, f; kwargs...) = interpolateMode2!(o, t, p, f; kwargs...)

```

Some of the kwargs would be mutable structures containing mutable vectors, etc. that could be reused in calculations. If the kwarg wasn’t given, then the memory would be allocated instead, but it could be passed in to avoid garbage collection.

Does that make sense.

I’ve moved all of those off into a settings structure, still battling with allocation checks in the test functions. I think it is an interaction with the @test macro though and these “ref” memory pointers.

I’m attempting to build a dummy module with the ref pointers and the testing functions so the “@test” macro just calls that module and checks for the allocations. When I did this by hand it worked.

There is another top level function that had the InterpType where InterpType\<:AbstractInterpolationMode as a kwargs…

That function interpolationHelper! with it passed in as the first argument. Then that InterpType isn’t used in the call to interpolationHelper!

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [May 20, 2025, 6:20pm UTC](https://discourse.julialang.org/t/kwargs-and-allocations/129183/14 "2025-05-20T18:20:07Z")

</div>

Aha, you generally want to pass values, not types. The specialization heuristics won’t specialize on types unless you explicitly capture a parametric `::Type{T}`.

---

<div class="post-metadata">

**Author:** ![Allan\_Baker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/allan_baker/32/42645_2.png) [@Allan\_Baker](https://discourse.julialang.org/u/Allan_Baker)\
**Post date:** [May 20, 2025, 6:24pm UTC](https://discourse.julialang.org/t/kwargs-and-allocations/129183/15 "2025-05-20T18:24:39Z")

</div>

The top function looks like this.

```julia
function dummyTop(obj::mytype{T,N}, var2::xyzVecT{T}, var2::anotherthing{T}, var3::T;
            useNearest::Bool=false,
            interpolationMode::Type{IntType}=DEFAULTINTERPOLATION) where {T<:Number, N<:Number, IntType<:AbstractInterpolationMode}

```

The T is a Float64 and the N is an Int64.

---

<div class="post-metadata">

**Author:** ![Allan\_Baker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/allan_baker/32/42645_2.png) [@Allan\_Baker](https://discourse.julialang.org/u/Allan_Baker)\
**Post date:** [May 20, 2025, 6:25pm UTC](https://discourse.julialang.org/t/kwargs-and-allocations/129183/16 "2025-05-20T18:25:58Z")

</div>

My attempt at backwards compatibility is becoming a nightmare. 😑

---

<div class="post-metadata">

**Author:** ![Allan\_Baker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/allan_baker/32/42645_2.png) [@Allan\_Baker](https://discourse.julialang.org/u/Allan_Baker)\
**Post date:** [May 20, 2025, 6:27pm UTC](https://discourse.julialang.org/t/kwargs-and-allocations/129183/17 "2025-05-20T18:27:03Z")

</div>

The reusable version dummyTop! has another kwarg…

reusableMemory::InterpolationMemory{T}=InterpolationMemory(obj)

---

<div class="post-metadata">

**Author:** ![Allan\_Baker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/allan_baker/32/42645_2.png) [@Allan\_Baker](https://discourse.julialang.org/u/Allan_Baker)\
**Post date:** [May 20, 2025, 6:34pm UTC](https://discourse.julialang.org/t/kwargs-and-allocations/129183/18 "2025-05-20T18:34:07Z")

</div>

Ok. Since my settings Structure had just as much trouble passing an @allocated check in my @testset as the kwargs until I pulled it off into its own module, I’m wondering if the kwargs version would have passed if I had pulled it out into its own module as well.

I’ll test that after lunch and comment back here if I was testing an interaction between const var=Ref{…} problems in my testing modules.

I have the settings version passing though.

_In general, anything we can make debugging where allocations happen easier would be great!_

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [May 20, 2025, 6:51pm UTC](https://discourse.julialang.org/t/kwargs-and-allocations/129183/19 "2025-05-20T18:51:32Z")

</div>

> [@Allan\_Baker](#):
>
> _In general, anything we can make debugging where allocations happen easier would be great!_

Are you aware of AllocCheck.jl?

---

<div class="post-metadata">

**Author:** ![danielwe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielwe/32/35657_2.png) [@danielwe](https://discourse.julialang.org/u/danielwe)\
**Post date:** [May 20, 2025, 6:57pm UTC](https://discourse.julialang.org/t/kwargs-and-allocations/129183/20 "2025-05-20T18:57:38Z")

</div>

> [@Allan\_Baker](#):
>
> ```julia
> function dummyTop(obj::mytype{T,N}, var2::xyzVecT{T}, var2::anotherthing{T}, var3::T;
> useNearest::Bool=false,
> interpolationMode::Type{IntType}=DEFAULTINTERPOLATION) where {T<:Number, N<:Number, IntType<:AbstractInterpolationMode}
> 
> ```

I think @mbauman called it: Make sure the `interpolationMode` kwarg has the parametrized `::Type{IntType}` annotation in every method in the call chain. That means you can’t use splatting and slurping to pass this argument through to an inner call. Instead of

```julia
function outer(x; kwargs...)
    y = 2x
    return inner(y; kwargs...)
end

```

you need

```julia
function outer(x; interpolationMode::Type{IT}, otherkwargs...) where {IT}
    y = 2x
    return inner(y; interpolationMode, otherkwargs...)
end

```

The same applies to any other kwarg that takes a type.

[Next page](https://discourse.julialang.org/t/kwargs-and-allocations/129183.md?page=2)
