June 12

On Tuesday, 11 June 2024 at 17:15:07 UTC, Vinod K Chandran wrote:

>

On Tuesday, 11 June 2024 at 16:54:44 UTC, Steven Schveighoffer wrote:

>

Two reasons.

  1. I am writting a dll to use in Python. So I am assuming that

Btw are you going to use PyD or doing everything manually from scratch?

June 12

On Tuesday, 11 June 2024 at 17:15:07 UTC, Vinod K Chandran wrote:

>

Hi Steve,
Two reasons.

  1. I am writting a dll to use in Python. So I am assuming that manual memory management is better for this project. It will give finer control to me.
  2. To squeeze out the last bit of performance from D.

On the second point, I would be interested in hearing what you find out. In general, I have not had any luck with speeding things up inside loops using manual memory management. The only that's worked is avoiding allocations and reusing already allocated memory.

You're splitting things into GC-allocated memory and manually managed memory. There's also SafeRefCounted, which handles the malloc and free for you.

June 12

A SafeRefCounted example with main marked @nogc:

import std;
import core.stdc.stdlib;

struct Foo {
  double[] data;
  double * ptr;
  alias data this;

  @nogc this(int n) {
    ptr = cast(double*) malloc(n*double.sizeof);
    data = ptr[0..n];
    printf("Data has been allocated\n");
  }

  @nogc ~this() {
    free(ptr);
    printf("Data has been freed\n");
  }
}

@nogc void main() {
  auto foo = SafeRefCounted!Foo(3);
  foo[0..3] = 1.5;
  printf("%f %f %f\n", foo[0], foo[1], foo[2]);
}
June 12

On Wednesday, 12 June 2024 at 01:35:26 UTC, monkyyy wrote:

>

rather then worring about the gc, just have 95% of data on the stack

How's that even possible ? AFAIK, we need heap allocated memory in order to make GUI lib as a DLL. So creating things in heap and modify it, that's the nature of my project.

June 12

On Wednesday, 12 June 2024 at 09:44:05 UTC, DrDread wrote:

>

also just slap @nogc on your main function to avoid accidential allocations.

Thanks for the suggestion. Let me check that idea.

June 12

On Wednesday, 12 June 2024 at 10:16:26 UTC, Sergey wrote:

>

Btw are you going to use PyD or doing everything manually from scratch?

Does PyD active now ? I didn't tested it. My approach is using "ctypes" library with my dll. Ctypes is the fastes FFI in my experience. I tested Cython, Pybind11 and CFFI. But None can beat the speed of ctypes. Currently the fastest experiments were the dlls created in Odin & C3. Both are non-GC languages.

June 12

On Wednesday, 12 June 2024 at 15:21:22 UTC, bachmeier wrote:

>

You're splitting things into GC-allocated memory and manually managed memory. There's also SafeRefCounted, which handles the malloc and free for you.

Thanks, I have read about the possibilities of "using malloc and free from D" in some other post. I think I should need to check that.

June 12

On Wednesday, 12 June 2024 at 15:33:39 UTC, bachmeier wrote:

>

A SafeRefCounted example with main marked @nogc:

Thanks for the sample. It looks tempting! Let me check that.

June 12

On Wednesday, 12 June 2024 at 15:33:39 UTC, bachmeier wrote:

>

A SafeRefCounted example with main marked @nogc:

import std;
import core.stdc.stdlib;

struct Foo {
  double[] data;
  double * ptr;
  alias data this;

  @nogc this(int n) {
    ptr = cast(double*) malloc(n*double.sizeof);
    data = ptr[0..n];
    printf("Data has been allocated\n");
  }
 }

Why not just use ptr ? Why did you data with ptr ?

June 12

On Wednesday, 12 June 2024 at 18:36:26 UTC, Vinod K Chandran wrote:

>

On Wednesday, 12 June 2024 at 15:33:39 UTC, bachmeier wrote:

>

A SafeRefCounted example with main marked @nogc:

import std;
import core.stdc.stdlib;

struct Foo {
  double[] data;
  double * ptr;
  alias data this;

  @nogc this(int n) {
    ptr = cast(double*) malloc(n*double.sizeof);
    data = ptr[0..n];
    printf("Data has been allocated\n");
  }
 }

Why not just use ptr ? Why did you data with ptr ?

Try foo[10] = 1.5 and foo.ptr[10] = 1.5. The first correctly throws an out of bounds error. The second gives Segmentation fault (core dumped).