Re: GC.addRange in pure function
On Sunday, 7 February 2021 at 14:13:18 UTC, vitamin wrote: Why using 'new' is allowed in pure functions but calling GC.addRange or GC.removeRange isn't allowed? pure is broken. Just don't [use it]
Re: Unfold string array
On Sunday, 5 January 2020 at 22:39:37 UTC, Teo wrote: On Sunday, 5 January 2020 at 13:37:58 UTC, JN wrote: [...] Thanks for the input. I just realized that I was not precise enough in my description. Apologies for that. My intention is to use std.algorithm, if possible. I read the documentation and tried using many functions like "joiner", "each", "fold", "group", "filter", etc. from std.algorithm.iteration. In my opinion, I need to construct a predicate like (a, b) => if b.startsWith() {join(a, b, " ")} Unfortunately, I cannot figure out which combination of functions will give me the desired result. Of course, if I am unable to find an elegant way to do it, I am going to iterate over the array in a way similar to what you suggested. https://ideone.com/tvpreP
Re: [windows] Can't delete a closed file?
Lol, you don't have to load and unload the curl dll. std.net.curl have its own lazy libcurl loader. But i'm not sure if it tries to find the dll in the temp directory. If it is the case, then it simply doesn't unload the dll when you have called some function from it.
Re: Can't cast from void*
On Tuesday, 6 February 2018 at 08:29:05 UTC, Kagamin wrote: On Monday, 5 February 2018 at 15:33:02 UTC, Steven Schveighoffer wrote: Is there a more pragmatic use case why this should be possible? Maybe for least surprise. The error message almost convinced me that such cast is impossible, only because of my memory that this cast used to be possible kept me trying. Is 5 not good because it's not big enough? Fill a bugreport.
Re: Does to!(string)(char[]) do any memory allocation on conversion?
On Monday, 25 December 2017 at 14:37:01 UTC, Mengu wrote: On Monday, 25 December 2017 at 14:12:32 UTC, Marc wrote: Does to!(string)(char[]) do any memory allocation on conversion or is this similar to a cast or what else? yes, it is allocating memory. you can test such cases with @nogc [0]. you can get a char[] via .dup of a string and then you can cast(string) if you don't want to allocate any memory. [0] https://dlang.org/spec/attribute.html#nogc dup allocates memory too
Re: Does to!(string)(char[]) do any memory allocation on conversion?
On Monday, 25 December 2017 at 14:12:32 UTC, Marc wrote: Does to!(string)(char[]) do any memory allocation on conversion or is this similar to a cast or what else? It is translated to idup. So yes, it allocates memory.
Re: No of threads
On Wednesday, 20 December 2017 at 13:41:06 UTC, Vino wrote: On Tuesday, 19 December 2017 at 18:42:01 UTC, Ali Çehreli wrote: On 12/19/2017 02:24 AM, Vino wrote: > Hi All, > >Request your help in clarifying the below. As per the document > > foreach (d; taskPool.parallel(xxx)) : The total number of threads that > will be created is total CPU -1 ( 2 processor with 6 core : 11 threads) > > foreach (d; taskPool.parallel(xxx,1)) : The total number of threads that > will be created is total CPU -1 ( 2 processor with 6 core : 12 threads) That parameter is workUnitSize, meaning the number of elements each thread will process per work unit. So, when you set it to 100, each thread will work on 100 elements before they go pick more elements to work on. Experiment with different values to find out which is faster for your work load. If each element takes very short amount of time to work on, you need larger values because you don't want to stop a happy thread that's chugging along on elements. It really depends on each program, so try different values. > foreach (d; taskPool.parallel(xxx,20)) : As in Windows 2008 whatever > value is set for the parallel the total number of threads does not > increase more than 12. taskPool is just for convenience. You need to create your own TaskPool if you want more threads: import std.parallelism; import core.thread; import std.range; void main() { auto t = new TaskPool(20); foreach (d; t.parallel(100.iota)) { // ... } Thread.sleep(5.seconds); t.finish(); } Now there are 20 + 1 (main) threads. Ali Hi Ali, Thank you very much, below are the observations, our program is used to calculate the size of the folders, and we don't see any improvements in the execution speed from the below test, are we missing something. Basically we expected the total execution time for the test 2 , as the time taken to calculate the size of the biggest folder + few additional mins, the biggest folder size is of 604 GB. Memory usage is just 12 MB, whereas the server has 65 GB and hardly 30% - 40% is used at any given point in time, so there is no memory constrain. Test 1: foreach (d; taskPool.parallel(dFiles[],1)) auto SdFiles = Array!ulong(dirEntries(d, SpanMode.depth).map!(a => a.size).fold!((a,b) => a + b) (x))[].filter!(a => a > Size); Execution Time is 26 mins with 11+1 (main) threads and 1 element per thread Test 2: auto TL = dFiles.length; auto TP = new TaskPool(TL); foreach (d; TP.parallel(dFiles[],1)) auto SdFiles = Array!ulong(dirEntries(d, SpanMode.depth).map!(a => a.size).fold!((a,b) => a + b) (x))[].filter!(a => a > Size); Thread.sleep(5.seconds); TP.finish(); Execution Time is 27 mins with 153+1 (main) threads and 1 element per thread From, Vino.B GC collect stops the worlds so there's no gain.
Re: Tried C++ to D. Wrong result.
On Tuesday, 28 November 2017 at 06:46:18 UTC, Dmitry wrote: On Monday, 27 November 2017 at 19:01:28 UTC, Ali Çehreli wrote: P.S. I think you have an unnecessary 'ref' on the D version because a slice is already a reference to elements: Fixed, thank you. https://pastebin.com/xJXPBh0n Converted it and it works as expected.
Re: reduce condition nesting
On Thursday, 23 November 2017 at 14:16:25 UTC, Andrea Fontana wrote: On Thursday, 23 November 2017 at 13:47:37 UTC, Adam D. Ruppe wrote: On Thursday, 23 November 2017 at 05:19:27 UTC, Andrey wrote: for instance in kotlin it can be replace with this: when { c1 -> foo(), c2 -> bar(), c3 -> ... else -> someDefault() } The `switch` statement covers some of these cases too. Anyway you can create something like this: https://run.dlang.io/is/7pbVXT Syntax #4 // Syntax #4 when ( c1, { writeln("first"); }, c2, { writeln("second"); }, { writeln("default"); } ); :)
Re: minElement on array of const objects
On Monday, 13 November 2017 at 10:20:51 UTC, Aurelien Fredouelle wrote: Hi all, It seems that it is not possible to use minElement on an array of const objects: class A { int val; } const(A) doStuff(const(A)[] v) { import std.algorithm.searching : minElement; return v.minElement!"a.val"; } This gets the following compiler error: std/algorithm/searching.d(1256,28): Error: cannot implicitly convert expression (front(r)) of type const(A) to app.A std/algorithm/searching.d(1286,35): Error: cannot implicitly convert expression (r[i]) of type const(A) to app.A std/algorithm/searching.d(1258,36): Error: template instance std.algorithm.searching.extremum!("a.val", "a < b", const(A)[], A) error instantiating std/algorithm/searching.d(3345,24):instantiated from here: extremum!("a.val", "a < b", const(A)[]) source/app.d(11,11):instantiated from here: minElement!("a.val", const(A)[]) Is there a reason why this is not allowed? Shouldn't minElement be able to return a const(A) in this situation? Thanks, Aurelien It should not until it uses recursion to find min element. const(Class) res = ...; ... return res; Algorithm tries to assign to res variable and fails because in D const(Class) means both const object and const reference to it. So we cannot assign another reference to const(Class) variable.
Re: Temporary objects as function parameters or when-is-this-shit-going-to-end?
On Friday, 13 October 2017 at 11:21:48 UTC, Biotronic wrote: On Friday, 13 October 2017 at 10:35:56 UTC, Jack Applegame wrote: Compiler creates struct on the stack and silently (without postblitting and destruction old object) moves it to another address. Is it normal? I don't think so. It is. Structs have no identity, and the compiler/GC/whatever is free to copy and/or move them about as it sees fit (as long as there is ostensibly only one - no duplicate constructor/destructor calls, no desynching of state). That's why the documentation[1] says not to have internal pointers in structs. WAT??? Compiler creates struct on the stack copies it without postblitting and destructs both objects. Now this looks like a real bug. There should be a this(this) call in there. Can I donate to the D Foundation and that my donations would be aimed at fixing exactly these bugs? BountySource[2] lets you do basically exactly that. [1]: https://dlang.org/spec/garbage.html, "Do not have pointers in a struct instance that point back to the same instance." [2]: https://www.bountysource.com/ What are the advantages of this weird behavior ? Also if the object is finally moved then why to call ctor not on the moved object ? [1] states that i cannot save the pointer inside the struct on the same struct(because GC can move objects in the memory, but in the example there's no gc as objects are on the stack), but what if i put to some global variable ? It should work as expected, not being partly moved. Postblit should be called as well as dtor of original object. 1 is a definitely a bug.
Re: Format g bug ?
Sorry, messed up numbers Expected: 3.11 3.11 3.1 3.1 Seems g outputs one digit less
Format g bug ?
import std.stdio; void main() { writefln(`%.2g`, 3.11); writefln(`%.2f`, 3.11); writefln(`%.1g`, 3.11); writefln(`%.1f`, 3.11); } 3.1 3.11 3 3.1 But expected 3.1 3.11 3.1 3.11
Re: lambda function with "capture by value"
On Saturday, 5 August 2017 at 19:19:06 UTC, Simon Bürger wrote: On Saturday, 5 August 2017 at 18:54:22 UTC, ikod wrote: Maybe std.functional.partial can help you. Nope. int i = 1; alias dg = partial!(writeln, i); i = 2; dg(); still prints '2' as it should because 'partial' takes 'i' as a symbol, which is - for this purpose - kinda like "by reference". Anyway, I solved my problem already a while ago by replacing delegates with custom struct's that implement the call-operator. I started this thread just out of curiosity, because as I see it, the purpose of lambdas is pretty much to remove the need for such custom constructions. This one works void delegate()[3] dgs; for(int i = 0; i < 3; ++i) { (k){ dgs[k] = {writefln("%s", k); }; }(i); } dgs.each!(a => a());
Re: Getting enum from value
On Saturday, 5 August 2017 at 15:42:53 UTC, Rene Zwanenburg wrote: On Saturday, 5 August 2017 at 15:33:57 UTC, Matthew Remmel wrote: Any ideas? You can use to! in std.conv: import std.stdio; import std.conv; enum Foo { A = "A", B = "B" } void main() { writeln("A".to!Foo); } Are you fools ? Did you ever read the post ? I think this is a minimal solution: enum Foo { A = "AV", B = "BV", C = "CV", } Foo K = [ EnumMembers!Foo ].find!(a => a == `BV`)[0];
Re: Why free and realloc seem to include .
On Thursday, 3 August 2017 at 14:03:56 UTC, Michael wrote: So this might be a bit of a stupid question, but looking at the DMD source code (dmodule.d in particular) I see the following code: if (srcfile._ref == 0) .free(srcfile.buffer); srcfile.buffer = null; srcfile.len = 0; and I was just wondering why certain functions seem to be called using the dot operator on its own, unattached to some object. This is probably a naive question but I haven't seen this in my limited experience using D and I was just wondering why this is. I have only really seen this relating to D's manual memory management. But in the same file, I see examples like this: FileName.free(n); so what is the case when you should use .free() and why not just free()? Thanks. Dot is equal to C++'s :: operator to access a global namespace. Aka ::free(ptr);
Re: Static array * scalar is not working for me
On Sunday, 30 July 2017 at 08:18:07 UTC, Danni Coy wrote: The following code is not working for me float[3] f; f[] = abs(f)[] * -1.0f; where abs is a function that returns a float[3]; it complains that f should be attached to some memory. Is it a bug or am I missing something? This is unimplemented currently and no one cares about it. You can do: f[] = abs(f)[]; f[] *= -1;
Re: Profiling after exit()
On Friday, 28 July 2017 at 08:06:33 UTC, Eugene Wissner wrote: On Friday, 28 July 2017 at 06:32:59 UTC, Jacob Carlborg wrote: On 2017-07-27 16:30, Eugene Wissner wrote: I have a multi-threaded application, whose threads normally run forever. But I need to profile this program, so I compile the code with -profile, send a SIGTERM and call exit(0) from my signal handler to exit the program. The problem is that I get the profiling information only from the main thread, but not from the other ones. Is there a way to get the profiling information from all threads before terminating the program? Maybe some way to finish the threads gracefully? or manully call "write trace.log"-function for a thread? As others have mentioned, you should in general avoid calling "exit" in a D program. There's a C function called "atexit" that allows to register a callback that is called after calling "exit". You could perhaps join the threads there. I don't know if that helps with the profiling though. Unfortunately I can't join threads, because the program wouldn't exit then, the threads run forever normally. I thought maybe there is some way to kill a thread gracefully in linux, so it can write its profiling information; or another way to get profiling. Thanks anyway. There's no "gracefully" way to kill a thread. If your thread cannot join, then you're doing something wrong
Re: Profiling after exit()
Also there was an issue that profiling doesn't work with multi-threaded apps and leads to a crash. Don't know if it is fixed.
Re: Profiling after exit()
Exit is not "normal exit" for D programs so, do not use it. Your threads should stop at some point to make able the app exit successfully. There's a "join" method. You can use it with your "done" variable.
Re: Why can't typeof() be used in member method?
On Wednesday, 26 July 2017 at 19:06:24 UTC, Andre Pany wrote: On Wednesday, 26 July 2017 at 17:04:59 UTC, Adam D. Ruppe wrote: On Wednesday, 26 July 2017 at 16:50:35 UTC, Andre Pany wrote: [...] FYI, you shouldn't use .stringof here. Just use `T` instead of `T.stringof`. [...] Thank you so much! Kind regards André There's another method for such a check. mixin(`static if (!is(typeof(T.`~p.name~`
Re: How can I serialize a struct into a file in the style of C?
Hi ! I have a dub package that doing this. https://github.com/Temtaime/tt-utils/blob/master/source/tt/binary/tests.d Have a look at the tests. Currently it has no documentation, but feel free to ask questions
Re: Duplicated functions not reported?
On Sunday, 16 April 2017 at 15:54:16 UTC, Stefan Koch wrote: On Sunday, 16 April 2017 at 10:56:37 UTC, Era Scarecrow wrote: On Saturday, 15 April 2017 at 11:10:01 UTC, Stefan Koch wrote: It would requires an O(n^2) check per declaration. Even it is never used. which would make imports that much more expensive. Seems wrong to me... If you made a list/array of all the functions (based purely on signatures) then sorted them, then any duplicates would be adjacent. Scanning that list would be O(n-1). This assumes it's done after all functions are scanned and identified, doing it earlier is a waste of time and energy. sorting has O(n^2) worst case complexity. Therefore totaling to O(n^2) worst case again. Why this difficulty ? Function[args][name] funcs; AA lookup is O(1).
Re: newbie problem with nothrow
On Monday, 31 October 2016 at 16:55:51 UTC, WhatMeWorry wrote: Is there a way to turn off nothrow or work around it? Because to me it looks like nothrow prevents me from doing anything useful. extern(C) void onKeyEvent(GLFWwindow* window, int key, int scancode, int action, int modifier) nothrow { if(queue.roomInQueue()) { auto event = new Event; event.type = EventType.keyboard; event.keyboard.key = cast(Key) key; // etc. } Error: function 'event_handler.CircularQueue.roomInQueue' is not nothrow Error: function 'event_handler.onKeyEvent' is nothrow yet may throw The compiler wouldn't let me just remove "nothrow" from the function. I tried a kludge where I had this function just pass all its parameters to another throwable function, but this caused errors as well. So I'm stuck. Anyone know how to proceed. Thanks. Wrap a body of the function to try {} catch {} and it'll work.
Re: Is there Typeof template ?
On Friday, 28 October 2016 at 18:39:36 UTC, Ali Çehreli wrote: On 10/28/2016 11:25 AM, Jonathan M Davis via Digitalmars-d-learn wrote: >> void main() { >> @(`str`, 123) uint k; >> foreach (a; __traits(getAttributes, k)) { >> pragma(msg, typeof(a)); >> } >> } > I don't know if Typeof is actually needed for what the OP is trying to do, > but if you wanted to apply typeof using something like std.meta.staticMap, > then you'd need something like Typeof. I see. Just to add something that I've just remembered, it is possible to apply typeof to __traits(getAttributes) as well: foreach (T; typeof(__traits(getAttributes, k))) { pragma(msg, T); } Now we get a list of types: string int Ali I wanna use it with staticMap.
Re: Is there Typeof template ?
On Friday, 28 October 2016 at 12:44:20 UTC, Adam D. Ruppe wrote: On Friday, 28 October 2016 at 10:52:05 UTC, Temtaime wrote: Are there something or should I create a PR to phobos? Why would you want that? I have UDAs with values à la @(`str`, 123) uint k; And i want to know a type of a value.
Is there Typeof template ?
Hi ! Tried to find alias Typeof(alias A) = typeof(A); or something, but failed. Are there something or should I create a PR to phobos? Thanks
Address of an element of AA
Hi ! I can't find this in specs. If i add an element to AA: aa[10] = 123; Will [10] be always the same (of course i don't remove that key) ? Thanks for a reply. I think specs should be enhanced.
Destructor order
Hi ! I wonder if i can rely on this code : http://dpaste.dzfl.pl/745cc5b1cdfb There's two questions: 1) Is dtors always called in reverse order ? 2) Is all the dtors always called when i call destroy ? Thanks for a reply !
Re: Maximum number of threads
Offtop: i think if number of threads > number of real cores, than there's something wrong with your design. Maybe fibers suit better ?
Re: using memset withing a pure function
There's a problem with « dst[0 .. n] = val; ». It should be « dst[0 .. n][] = val; »
Re: Associative array literal. Why doesn't it compile?
It's because it's implemented in DMD only partly. There's a bug report associated with it.
Re: How disruptive is the GC?
I'm writing a game engine in D. Try to minimize allocations and that's will be OK. I'm using delegates and all the phobos stuff. I allocate only in few places at every frame. So i can reach 1K fps on a complicated scene. GC is not a problem. DMD optimizes so ugly that all the math is very, very slow. DMD gives me about 200 fps, when with LDC i can reach 1k.
Re: Converting uint[] slice to string for the purpose of hashing?
All types are hashable and for your own structs and classes you can redefine opHash
Re: Escape a string ?
Sorry, i meant it gives b == fooo\\nbar I'm writing an interpreter and it should dump original string from memory. Also i wonder if there's a function to convert aaa\\nbb to aaa\nbb (i.e. to unescape)
Escape a string ?
Hi ! I wonder how to escape a string in phobos ? For example auto a = [ fooo\nbar ]; auto b = format(%(%s%), a); gives b: fooo\nbar Is there any other function to escape string? I'm looking for something that doesn't require to make an array at first. For example is there string escape(string s) in phobos ? I've searched and have not found it.
Re: Why the DMD Backend?
It's only words. If we speak about LDC it can compile fast in debug mode with performance average to DMD's backend but with much great performance in release mode thanks to vectorization and other techniques. Also LDC thanks to LLVM supports X86, X86-64, PowerPC, PowerPC-64, ARM, Thumb, SPARC, Alpha, CellSPU, MIPS, MSP430, SystemZ, and XCore platforms. And what about DMD? Only x86 and x86-64. Just link LLVM statically with LDC and LDC will work out of the box as DMD. No problems at both Windows and Linux.
Re: Why the DMD Backend?
Setting up LLVM infrastructure is only needed when you is a LDC developer. I think for ordinary users it's not their business.
Re: static array alignment
align doesn't work in DMD. There's bugreport for a long time.
Re: D1: Windows DWORD conversion in D
DWORD is an uint.
Re: Appender.put return value
Offtop It's better to return this and have return type ref auto i think.
Re: Test if member is static variable
Also second question is what are better to use, current template recursion-based code or rewrite it to CTFE ?
Test if member is static variable
Hi ! http://dpaste.dzfl.pl/e21082716396 Is there a way to optimize static if(is(typeof(__traits(getMember, T, name).offsetof)) == false is(FunctionTypeOf!(__traits(getMember, T, name)) == function) == false __traits(compiles, __traits(getMember, T, name))) ? I think there shoult be __traits(isStaticVariable), but there's not. Thanks !
Inherit of attributes
Hi ! http://dpaste.dzfl.pl/2fa3dd2ea834 Why S.init not pure ? Is it expected behavior or bug ? Thanks!
Re: Inherit of attributes
Hi, MrSmith ! Yes, i know that, but my question isn't about it. I want to type `pure:` at module's beginning and have all function(in classes, too) declared as pure. I think `pure:` should do it. But it doesn't.
Re: Inherit of attributes
Hi ! Thanks for reply. Why so ? And why @nogc not transitive too ?