How to implement immutable ring buffer?

2015-05-27 Thread drug via Digitalmars-d-learn

Could somebody share his thoughts on the subject?
Would it be efficient? Is it possible to avoid memory copying to provide 
immutability? To avoid cache missing ring buffer should be like array, 
not list, so it's possible that the whole buffer should be moved. Is it 
neccessary to be real immutable, may be some form of fake immutabitiy is 
enough?
I'd like to try immutability in my app, and ring buffer is what I don't 
know how to use in the realm of immutability.


Thanks


Re: problem with gc?

2015-05-27 Thread Anonymouse via Digitalmars-d-learn

On Wednesday, 27 May 2015 at 08:42:01 UTC, zhmt wrote:
When I enable the --profle, get something like this, it doesnt 
give me too much help:

[...]


Tried callgrind and kcachegrind? If nothing else it's better at 
illustrating the same output, assuming you have graphviz/dot 
installed.


Given --dump-instr you can also get instruction-level profiling 
if you're really wondering what's happening.


Re: problem with gc?

2015-05-27 Thread zhmt via Digitalmars-d-learn

On Wednesday, 27 May 2015 at 09:39:42 UTC, Anonymouse wrote:

On Wednesday, 27 May 2015 at 08:42:01 UTC, zhmt wrote:
When I enable the --profle, get something like this, it doesnt 
give me too much help:

[...]


Tried callgrind and kcachegrind? If nothing else it's better at 
illustrating the same output, assuming you have graphviz/dot 
installed.


Given --dump-instr you can also get instruction-level profiling 
if you're really wondering what's happening.


@Anonymouse

Thank u very much, I have tried this:

 valgrind  --tool=callgrind ./gamelibdtest

callgrind_annotate callgrind.out.29234


 Ir  file:function

124,918,002  ???:_D9invariant12_d_invariantFC6ObjectZv 
[/mnt/hgfs/mydoc/dworkspace/bin/gamelibdtest]
 27,235,809  ???:_D2gc2gc2GC6mallocMFNbmkPmxC8TypeInfoZPv 
[/mnt/hgfs/mydoc/dworkspace/bin/gamelibdtest]
 22,484,758  
/mnt/hgfs/mydoc/dworkspace/gamelibdtest//usr/include/dmd/phobos/std/format.d:_D3std6format74__T14formatUnsignedTS3std6format16__T7sformatTaTiZ7sformatFAaxAaiZ4SinkTaZ14formatUnsignedFNaNfS3std6format16__T7sformatTaTiZ7sformatFAaxAaiZ4SinkmKS3std6format18__T10FormatSpecTaZ10FormatSpeckbZv 
[/mnt/hgfs/mydoc/dworkspace/bin/gamelibdtest]
 19,171,341  ???:_D2gc2gc3Gcx10smallAllocMFNbhKmkZPv 
[/mnt/hgfs/mydoc/dworkspace/bin/gamelibdtest]
 16,334,730  ???:pthread_mutex_lock 
[/usr/lib64/libpthread-2.17.so]
 14,519,760  ???:pthread_mutex_unlock 
[/usr/lib64/libpthread-2.17.so]
 14,503,272  
/mnt/hgfs/mydoc/dworkspace/gamelibdtest/../gamelibd/source/gamelibd/net/linux/epollapi.d:_D8gamelibd3net5linux8epollapi8bitExistFiiZb 
[/mnt/hgfs/mydoc/dworkspace/bin/gamelibdtest]
 13,521,278  
/mnt/hgfs/mydoc/dworkspace/gamelibdtest/../gamelibd/source/gamelibd/net/linux/TcpLinuxConn.d:_D8gamelibd3net5linux12TcpLinuxConn12TcpLinuxConn8readSomeMFAhZi'2 
[/mnt/hgfs/mydoc/dworkspace/bin/gamelibdtest]
 12,695,373  ???:_D2gc2gc3Gcx10smallAllocMFNbhKmkZ8tryAllocMFNbZb 
[/mnt/hgfs/mydoc/dworkspace/bin/gamelibdtest]
 12,558,147  
/mnt/hgfs/mydoc/dworkspace/gamelibdtest/../gamelibd/source/gamelibd/net/linux/linuxconn.d:_D8gamelibd3net5linux9linuxconn11handleEventFAS8gamelibd3net5linux8epollapi11epoll_eventZv 
[/mnt/hgfs/mydoc/dworkspace/bin/gamelibdtest]
 12,091,173  
???:_D4core6thread5Fiber39__T4callVE4core6thread5Fiber7Rethrowi1Z4callMFZC6object9Throwable 
[/mnt/hgfs/mydoc/dworkspace/bin/gamelibdtest]
 11,153,058  
/mnt/hgfs/mydoc/dworkspace/gamelibdtest//usr/include/dmd/phobos/std/format.d:_D3std6format18__T10FormatSpecTaZ10FormatSpec75__T17writeUpToNextSpecTS3std6format16__T7sformatTaTiZ7sformatFAaxAaiZ4SinkZ17writeUpToNextSpecMFNaNfS3std6format16__T7sformatTaTiZ7sformatFAaxAaiZ4SinkZb 
[/mnt/hgfs/mydoc/dworkspace/bin/gamelibdtest]
 10,877,409  
/mnt/hgfs/mydoc/dworkspace/gamelibdtest/../gamelibd/source/gamelibd/net/linux/epollapi.d:_D8gamelibd3net5linux8epollapi8changeEvFiiPviZv 
[/mnt/hgfs/mydoc/dworkspace/bin/gamelibdtest]
 10,676,006  
/mnt/hgfs/mydoc/dworkspace/gamelibdtest/../gamelibd/source/gamelibd/net/linux/TcpLinuxConn.d:_D8gamelibd3net5linux12TcpLinuxConn12TcpLinuxConn9writeSomeMFAhZi 
[/mnt/hgfs/mydoc/dworkspace/bin/gamelibdtest]
 10,279,096  ???:pthread_getspecific 
[/usr/lib64/libpthread-2.17.so]

  9,741,852  ???:__GI_memset [/usr/lib64/libc-2.17.so]
  9,645,888  ???:_d_arraycopy 
[/mnt/hgfs/mydoc/dworkspace/bin/gamelibdtest]
  9,430,244  ???:_D2gc2gc3Gcx5sweepMFNbZm 
[/mnt/hgfs/mydoc/dworkspace/bin/gamelibdtest]
  8,866,869  
???:_D4core6atomic50__T11atomicStoreVE4core6atomic11MemoryOrderi0TbTbZ11atomicStoreFNaNbNiKObbZv 
[/mnt/hgfs/mydoc/dworkspace/bin/gamelibdtest]
  8,661,662  
/mnt/hgfs/mydoc/dworkspace/gamelibdtest/../gamelibd/source/gamelibd/net/linux/TcpLinuxConn.d:_D8gamelibd3net5linux12TcpLinuxConn12TcpLinuxConn5writeMFAhZi 
[/mnt/hgfs/mydoc/dworkspace/bin/gamelibdtest]


Re: problem with gc?

2015-05-27 Thread zhmt via Digitalmars-d-learn
When I enable the --profle, get something like this, it doesnt 
give me too much help:


 Timer Is 3579545 Ticks/Sec, Times are in Microsecs 



  Num  TreeFuncPer
  CallsTimeTimeCall

1298756  4996649885  49875773773840 int 
gamelibd.net.linux.TcpLinuxConn.TcpLinuxConn.writeSome(ubyte[])
30358909 14353678347  4433441425 146 void 
gamelibd.net.linux.linuxconn.selectAndProcessNetEvents(ulong)
30359249  1670090296  1582462941  52 long 
gamelibd.util.utcNow()
1298744   874889280   858583339 661 
_D8gamelibd3net5linux12TcpLinuxConn12TcpLinuxConn8readSomeMFAhZ9__lambda2MFZv
1300549   25997   803742026 618 int 
gamelibd.net.linux.TcpLinuxConn.TcpLinuxConn.readSome(ubyte[])
2597506   756203332   702092233 270 void 
gamelibd.net.linux.epollapi.changeEv(int, int, void*, int)
30358909  9920075665   285890739   9 void 
gamelibd.net.linux.linuxconn.handleEvent(gamelibd.net.linux.epollapi.epoll_event[])
1298744  5131800498   244989946 188 void 
gamelibd.net.linux.TcpLinuxConn.TcpLinuxConn.doRead(gamelibd.net.linux.epollapi.epoll_event*)
1298768   0   223952199 172 void 
gamelibd.net.linux.IoEventHandler.IoEventHandler.autoReaderFiberSetting(void 
delegate())
1298756  5207337081   210684924 162 int 
gamelibd.net.linux.TcpLinuxConn.TcpLinuxConn.write(ubyte[])
30358953   162443804   162443804   5 @property bool 
gamelibd.util.LinkedList!(gamelibd.net.exceptionsafefiber.ExceptionSafeFiber).LinkedList.isEmpty()
 648485   989705094   139972636 215 int 
gamelibd.net.linux.TcpLinuxConn.TcpLinuxConn.read(ubyte[])
1298919  1008643391   124807374  96 void 
gamelibd.net.exceptionsafefiber.ExceptionSafeFiber.resume()
303590728843187288431872   2 pure nothrow 
@nogc @safe gamelibd.net.exceptionsafefiber.TimerTask 
std.container.rbtree.__T12RedBlackTreeTC8gamelibd3net18exceptionsafefiber9TimerTaskVAyaa25_612e737461727454696d65203c20622e737461727454696d65Vbi1Z.RedBlackTree.front()
1298750  157489732887384898  67 void 
gamelibd.net.linux.IoEventHandler.IoEventHandler.tryResumeReaderFiber()
303590727732591577325915   2 pure nothrow 
@property @nogc @safe ulong 
std.container.rbtree.__T12RedBlackTreeTC8gamelibd3net18exceptionsafefiber9TimerTaskVAyaa25_612e737461727454696d65203c20622e737461727454696d65Vbi1Z.RedBlackTree.length()
45448316156034761560347  13 pure @safe bool 
std.exception.enforce!(bool).enforce(bool, lazy object.Throwable)
25975005190564251905642  19 void 
gamelibd.net.linux.TcpLinuxConn.TcpLinuxConn.throwExceptionIfErrAfterOp(long)
12989514939205649392056  38 
gamelibd.net.exceptionsafefiber.ExceptionSafeFiber 
gamelibd.net.exceptionsafefiber.ExceptionSafeFiber.getThis()
25975188087718146297298  17 
gamelibd.mem.Ptr!(gamelibd.net.provider.Conn).Ptr.getinout(ref 
@property inout(gamelibd.net.provider.Conn) function())
25976935172573037629038  14 
gamelibd.mem.Ptr!(gamelibd.net.exceptionsafefiber.ExceptionSafeFiber).Ptr.getinout(ref 
@property 
inout(gamelibd.net.exceptionsafefiber.ExceptionSafeFiber) 
function())
1298756   47994165534717468  26 void 
gamelibd.net.linux.IoEventHandler.IoEventHandler.enableRead()


Re: How to implement immutable ring buffer?

2015-05-27 Thread drug via Digitalmars-d-learn

On 27.05.2015 11:04, thedeemon wrote:

This whole idea sounds self-contradictory.
Ring buffer is a mutable-array-based implementation of something, for
example of a queue. You can ask about immutable implementations of a
queue, but that would be another question, not involving a ring buffer.

What do you want to do with this buffer, what do you need it for?
In general I need to have the last n instances of some objects queue. 
Now it implemented using ring buffer. Probably, you're right about 
immutable queue, it is more suitable definition here.


Re: problem with gc?

2015-05-27 Thread zhmt via Digitalmars-d-learn

@jklp



And also you could try to surround the whole block with 
`GC.disable` and `GC.enable`. This would help to determine if 
the GC is involved:


---
Ptr!Conn conn = connect(127.0.0.1,8881);
GC.disable;
ubyte[100] buf;
string str;
for(int i=0; iN; i++)
{
str = format(%s,i);
conn.write(cast(ubyte[]) str);
conn.read(buf[0..str.length]);
n++;
}
GC.enable;
conn.close();
---


I have tried this, and throughput falls down as well too. Does 
this mean the gc is not involved? I am confused.


Re: How to implement immutable ring buffer?

2015-05-27 Thread Daniel Kozák via Digitalmars-d-learn

On Wed, 27 May 2015 09:20:52 +
drug via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote:

 Could somebody share his thoughts on the subject?
 Would it be efficient? Is it possible to avoid memory copying to
 provide immutability? To avoid cache missing ring buffer should be
 like array, not list, so it's possible that the whole buffer should
 be moved. Is it neccessary to be real immutable, may be some form of
 fake immutabitiy is enough?
 I'd like to try immutability in my app, and ring buffer is what I
 don't know how to use in the realm of immutability.
 
 Thanks

Maybe http://dlang.org/phobos/std_typecons.html#.Rebindable could help
you.


Re: problem with gc?

2015-05-27 Thread Anonymouse via Digitalmars-d-learn

On Wednesday, 27 May 2015 at 10:24:59 UTC, zhmt wrote:

@Anonymouse

Thank u very much, I have tried this:

 valgrind  --tool=callgrind ./gamelibdtest

callgrind_annotate callgrind.out.29234


 Ir  file:function

[...]


Without touching on the actual profile (which I'll leave to those 
better knowledgeable than I), open those callgrind.out.pid.* 
files in KCachegrind to get callgraphs.


See https://imgur.com/AZEutCE,XXIDBvB,qehWaHg,bpTxS,3YfcJdh for 
some examples.


Re: How to implement immutable ring buffer?

2015-05-27 Thread thedeemon via Digitalmars-d-learn

This whole idea sounds self-contradictory.
Ring buffer is a mutable-array-based implementation of something, 
for example of a queue. You can ask about immutable 
implementations of a queue, but that would be another question, 
not involving a ring buffer.


What do you want to do with this buffer, what do you need it for?


Re: problem with gc?

2015-05-27 Thread zhmt via Digitalmars-d-learn




What happened when the code changes a little? Who will give an 
explaination,Thanks a lot?


what happend if you use sformat instead?

Ptr!Conn conn = connect(127.0.0.1,8881);
ubyte[100] buf;
char[100] buf2;
for(int i=0; iN; i++)
{
auto str = sformat(buf2, %s,i);
conn.write((cast(ubyte*)str.ptr)[0..str.length]);
conn.read(buf[0..str.length]);
n++;
}
conn.close();


@Daniel Kozák
Thanks for reply.

I have tried sformat, but it doesnt work: throughput falls down, 
and the cpu keeps busy.


Re: problem with gc?

2015-05-27 Thread zhmt via Digitalmars-d-learn
If I pass a timeout with 1ms to epoll_wait,the cpu will not be 
busy when throughput falls down.


Re: problem with gc?

2015-05-27 Thread zhmt via Digitalmars-d-learn

It seems that  dlang library is not so effient?


Re: How to implement immutable ring buffer?

2015-05-27 Thread drug via Digitalmars-d-learn

On 27.05.2015 11:10, Daniel Kozák via Digitalmars-d-learn wrote:


On Wed, 27 May 2015 09:20:52 +
drug via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote:


Could somebody share his thoughts on the subject?
Would it be efficient? Is it possible to avoid memory copying to
provide immutability? To avoid cache missing ring buffer should be
like array, not list, so it's possible that the whole buffer should
be moved. Is it neccessary to be real immutable, may be some form of
fake immutabitiy is enough?
I'd like to try immutability in my app, and ring buffer is what I
don't know how to use in the realm of immutability.

Thanks


Maybe http://dlang.org/phobos/std_typecons.html#.Rebindable could help
you.

I guess it won't be efficient, because it would be ring buffer of (in 
some form) pointers. It's suitable if elements of the buffer are 
processed independently each other. In other case cache misses are 
provided, I think.


Re: problem with gc?

2015-05-27 Thread Steven Schveighoffer via Digitalmars-d-learn

On 5/27/15 2:42 AM, zhmt wrote:

When I enable the --profle, get something like this, it doesnt give me
too much help:



I don't see any GC function here, I don't think you are are focusing on 
the right portion of the code. Seems like the gamelib library is 
consuming all the time. You may want to check with the author of that 
code for help.


-Steve


Null argument and function resolution

2015-05-27 Thread Andrea Fontana via Digitalmars-d-learn

Check this example:
http://dpaste.dzfl.pl/53f85bae4382

Calling with null, both c-tor match.
Is there a way to solve this?

Something like:

this(in ubyte* data) if( ??? )
{

}


Andrea


Re: problem with gc?

2015-05-27 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 27 May 2015 at 10:27:08 UTC, zhmt wrote:

It seems that  dlang library is not so effient?


The gamelibd could be doing a lot more than just echoing... it 
sounds to me that your socket might be blocking and epoll is busy 
looping waiting for it to become available again.


Re: Prevent slices from referencing old array after realocation?

2015-05-27 Thread Steven Schveighoffer via Digitalmars-d-learn

On 5/27/15 1:55 AM, Gr8Ape wrote:

I'm writing a program that handles lots of slices of larger
arrays. I need the data of each of them all together for one
operation, but otherwise it's more convenient to handle the
elements in small groups. I could just store a pointer to my
array, an offset, and a length but since I'm using D I'd much
prefer to use slices...

So I was looking through the documentation on slices and found
out that if the array has to realocate then slices pointing to
that array will keep on pointing to that block of memory, rather
than the new location. Given that I'll be working with arrays
that can vary wildly in size and that giving all of them the
maximum capacity so they never realocate would be extremely
inefficent, I need a workaround.

So I'm wondering if there's a way to tell all the slices of an
underlying array to now point to the new underlying array upon
reallocation? Or would I have to iterate through and reassign
every slice?


Slices can't do this, because they have no idea about each other. So 
when you append to one slice, you can't have it update all other 
relevant slices.


However, you could create a type that does this.

-Steve


problem with custom predicate

2015-05-27 Thread via Digitalmars-d-learn
I am trying to use a Container class with a custom predicate, but 
the following code does not compile. Any hints on how to do it?


import std.container;

class C
{
int[] prio;
RedBlackTree!(int, (a,b)=prio[a]prio[b]) tree;
}

I think I understand the reason why this does not work (the 
lambda cannot access the 'this' to get to 'prio'), but I can't 
think of a way to make it work. Any Ideas?





Re: Null argument and function resolution

2015-05-27 Thread Adam D. Ruppe via Digitalmars-d-learn

Two options:

1) add an overload that takes typeof(null)

this(typeof(null)) { /* handles the null literal specially */ }

2) Cast null to a specific type when making it:

new YourClass(cast(ubyte*) null);


Re: problem with custom predicate

2015-05-27 Thread Steven Schveighoffer via Digitalmars-d-learn

On 5/27/15 9:59 AM, Meta wrote:


I thought unaryFun *does* work with all callables, including
structs/classes with opCall?


It (binaryFun actually) declares an alias. But you need an actual 
instance to use in this case. You simply can't declare that in a type 
definition, and it won't be set up properly in the constructor even if 
you could. You need an instance in order to use an alias to that instance.


What likely would happen is if the functor is a callable type, and that 
type is not a functor itself (i.e. doesn't define static opCall), is 
that RedBlackTree would then require an instance of that type as part of 
the constructor.


-Steve


Re: problem with gc?

2015-05-27 Thread via Digitalmars-d-learn

On Wednesday, 27 May 2015 at 05:48:13 UTC, zhmt wrote:




The code you posted is the client code, but the issue seems to be 
on the server side.

Can you post the server code and also the timing code?




Re: Prevent slices from referencing old array after realocation?

2015-05-27 Thread via Digitalmars-d-learn

On Wednesday, 27 May 2015 at 07:55:29 UTC, Gr8Ape wrote:




You can keep an index and a count instead of a slice.
If you know your data well, you can exploit this knowledge, and 
this has the potential to save you many bytes per slice when 
dealing with large data sets.
Depending on your access patterns, it also has the potential to 
be fairly more cpu-efficient.


Re: problem with custom predicate

2015-05-27 Thread Steven Schveighoffer via Digitalmars-d-learn
On 5/27/15 9:11 AM, Simon =?UTF-8?B?QsO8cmdlciI=?= 
simon.buer...@rwth-aachen.de wrote:

On Wednesday, 27 May 2015 at 14:58:39 UTC, drug wrote:


Do you want to dynamically change priority?

Actually yes. In my actual code I am not using a RedBlackTree but my own
Container (a heap with the possibility to modify elements inside), which
is notified when prio changes so it can do a (local) reordering.


It would be a very bad idea to change prio if the RBTree is constructed 
already (and if this works).



Why prio is outside of your predicate?

Well, how would I get it into the lambda? In C++ I would use a
comparision class, and the constructor of the container would take an
instance of that class. But D's RedBlackTree-constructor does not take
such a (run-time) argument. And I dont see a way to get prio into the
predicate, which is a template-argument.


This is true, RedBlackTree does not take a functor as a parameter to the 
constructor, it just uses an alias.


This should work if your RedBlackTree is scoped inside a function where 
the data exists. But at the time of declaration inside the class, the 
instance of prio doesn't exist, so you can't alias it.


It would be a good enhancement I think to add support for function objects.

-Steve



Re: problem with custom predicate

2015-05-27 Thread Timon Gehr via Digitalmars-d-learn

On 05/27/2015 05:30 PM, Steven Schveighoffer wrote:

On 5/27/15 9:11 AM, Simon =?UTF-8?B?QsO8cmdlciI=?=
simon.buer...@rwth-aachen.de wrote:

On Wednesday, 27 May 2015 at 14:58:39 UTC, drug wrote:


Do you want to dynamically change priority?

Actually yes. In my actual code I am not using a RedBlackTree but my own
Container (a heap with the possibility to modify elements inside), which
is notified when prio changes so it can do a (local) reordering.


It would be a very bad idea to change prio if the RBTree is constructed
already (and if this works).


Why prio is outside of your predicate?

Well, how would I get it into the lambda? In C++ I would use a
comparision class, and the constructor of the container would take an
instance of that class. But D's RedBlackTree-constructor does not take
such a (run-time) argument. And I dont see a way to get prio into the
predicate, which is a template-argument.


This is true, RedBlackTree does not take a functor as a parameter to the
constructor, it just uses an alias.

This should work if your RedBlackTree is scoped inside a function where
the data exists. But at the time of declaration inside the class, the
instance of prio doesn't exist, so you can't alias it.

It would be a good enhancement I think to add support for function objects.

-Steve



Also, apart from missing implementation, there is no good reason why the 
code given should not work, is there? I think nested template 
instantiation should work in all scopes. (Of course, there is the issue 
that nested structs are somewhat arbitrarily not given a context pointer 
when declared within a class/struct, so this might be a little 
inconsistent in case it also works for structs, no question for classes 
though: just instantiate as nested class.)


Re: problem with custom predicate

2015-05-27 Thread Meta via Digitalmars-d-learn
On Wednesday, 27 May 2015 at 15:30:28 UTC, Steven Schveighoffer 
wrote:
On 5/27/15 9:11 AM, Simon =?UTF-8?B?QsO8cmdlciI=?= 
simon.buer...@rwth-aachen.de wrote:

On Wednesday, 27 May 2015 at 14:58:39 UTC, drug wrote:


Do you want to dynamically change priority?
Actually yes. In my actual code I am not using a RedBlackTree 
but my own
Container (a heap with the possibility to modify elements 
inside), which
is notified when prio changes so it can do a (local) 
reordering.


It would be a very bad idea to change prio if the RBTree is 
constructed already (and if this works).



Why prio is outside of your predicate?

Well, how would I get it into the lambda? In C++ I would use a
comparision class, and the constructor of the container would 
take an
instance of that class. But D's RedBlackTree-constructor does 
not take
such a (run-time) argument. And I dont see a way to get prio 
into the

predicate, which is a template-argument.


This is true, RedBlackTree does not take a functor as a 
parameter to the constructor, it just uses an alias.


This should work if your RedBlackTree is scoped inside a 
function where the data exists. But at the time of declaration 
inside the class, the instance of prio doesn't exist, so you 
can't alias it.


It would be a good enhancement I think to add support for 
function objects.


-Steve


I thought unaryFun *does* work with all callables, including 
structs/classes with opCall?


Re: Null argument and function resolution

2015-05-27 Thread Andrea Fontana via Digitalmars-d-learn

The first answer is the one I was looking for. Very useful.
You should add to next this week in d tips.

On Wednesday, 27 May 2015 at 14:09:48 UTC, Adam D. Ruppe wrote:

Two options:

1) add an overload that takes typeof(null)

this(typeof(null)) { /* handles the null literal specially */ }

2) Cast null to a specific type when making it:

new YourClass(cast(ubyte*) null);




Re: problem with custom predicate

2015-05-27 Thread drug via Digitalmars-d-learn

On 27.05.2015 13:50, Simon Bürger simon.buer...@rwth-aachen.de wrote:

I am trying to use a Container class with a custom predicate, but the
following code does not compile. Any hints on how to do it?

import std.container;

class C
{
 int[] prio;
 RedBlackTree!(int, (a,b)=prio[a]prio[b]) tree;
}

I think I understand the reason why this does not work (the lambda
cannot access the 'this' to get to 'prio'), but I can't think of a way
to make it work. Any Ideas?


Do you want to dynamically change priority? Why prio is outside of your 
predicate?


Re: problem with custom predicate

2015-05-27 Thread via Digitalmars-d-learn

On Wednesday, 27 May 2015 at 14:58:39 UTC, drug wrote:


Do you want to dynamically change priority?
Actually yes. In my actual code I am not using a RedBlackTree but 
my own Container (a heap with the possibility to modify elements 
inside), which is notified when prio changes so it can do a 
(local) reordering.



Why prio is outside of your predicate?
Well, how would I get it into the lambda? In C++ I would use a 
comparision class, and the constructor of the container would 
take an instance of that class. But D's RedBlackTree-constructor 
does not take such a (run-time) argument. And I dont see a way to 
get prio into the predicate, which is a template-argument.


As I am implementing my own container anyway, I could of course 
do it the C++-way. But I was wondering if there is another way.


Re: Null argument and function resolution

2015-05-27 Thread ketmar via Digitalmars-d-learn
On Wed, 27 May 2015 14:09:47 +, Adam D. Ruppe wrote:

 Two options:
 
 1) add an overload that takes typeof(null)
 
 this(typeof(null)) { /* handles the null literal specially */ }

you keep breaking my box of thinking. ;-) never thought about such 
declaration.

signature.asc
Description: PGP signature


Re: problem with custom predicate

2015-05-27 Thread Meta via Digitalmars-d-learn
On Wednesday, 27 May 2015 at 17:56:27 UTC, Steven Schveighoffer 
wrote:

On 5/27/15 9:59 AM, Meta wrote:


I thought unaryFun *does* work with all callables, including
structs/classes with opCall?


It (binaryFun actually) declares an alias. But you need an 
actual instance to use in this case. You simply can't declare 
that in a type definition, and it won't be set up properly in 
the constructor even if you could. You need an instance in 
order to use an alias to that instance.


What likely would happen is if the functor is a callable type, 
and that type is not a functor itself (i.e. doesn't define 
static opCall), is that RedBlackTree would then require an 
instance of that type as part of the constructor.


-Steve


Yes, sorry, I meant binaryFun. I remember having a similar 
problem with tee, and decided to just disallow passing in the 
name of a struct/class with opCall as a template argument, and 
instead require that an instance is passed at runtime.


Re: Null argument and function resolution

2015-05-27 Thread Meta via Digitalmars-d-learn

On Wednesday, 27 May 2015 at 19:38:16 UTC, ketmar wrote:

On Wed, 27 May 2015 14:09:47 +, Adam D. Ruppe wrote:


Two options:

1) add an overload that takes typeof(null)

this(typeof(null)) { /* handles the null literal specially */ }


you keep breaking my box of thinking. ;-) never thought about 
such

declaration.


Unfortunately, there is no equivalent way to disable assignment 
of [] at compile time.


struct ArrayWrapper(T)
{
T[] a;

this(T[] arr) { a = arr; }

//Doesn't work; Conflicts with this(T[] arr)
//@disable this(typeof(this)[]);

//Doesn't work; hijacked by this(T[] arr)
//@disable this(typeof([]));
}


Re: Null argument and function resolution

2015-05-27 Thread Timon Gehr via Digitalmars-d-learn

On 05/27/2015 10:09 PM, Meta wrote:

On Wednesday, 27 May 2015 at 19:38:16 UTC, ketmar wrote:

On Wed, 27 May 2015 14:09:47 +, Adam D. Ruppe wrote:


Two options:

1) add an overload that takes typeof(null)

this(typeof(null)) { /* handles the null literal specially */ }


you keep breaking my box of thinking. ;-) never thought about such
declaration.


Unfortunately, there is no equivalent way to disable assignment of [] at
compile time.

struct ArrayWrapper(T)
{
 T[] a;

 this(T[] arr) { a = arr; }

 //Doesn't work; Conflicts with this(T[] arr)
 //@disable this(typeof(this)[]);

 //Doesn't work; hijacked by this(T[] arr)
 //@disable this(typeof([]));
}


https://issues.dlang.org/show_bug.cgi?id=7007


Re: problem with gc?

2015-05-27 Thread zhmt via Digitalmars-d-learn

On Wednesday, 27 May 2015 at 05:51:21 UTC, zhmt wrote:
I noticed that the cpu% falls from 99% down to 4% as well when 
the throughput falls down.


I tried again for several times, the cpu is still busy, 98.9%.


Re: problem with gc?

2015-05-27 Thread zhmt via Digitalmars-d-learn

I think it is not problem of gc, it is my fault:
The operations is serialized:
clent send  - server recv  - server send  - client recv,
so if one operation takes too long time, the throughput will 
definitely fall down.


I cant explain why it so fast when buffer is big enough, and so 
low when buffer is smaller than data size.


Re: problem with gc?

2015-05-27 Thread zhmt via Digitalmars-d-learn

On Wednesday, 27 May 2015 at 19:04:53 UTC, Márcio Martins wrote:

On Wednesday, 27 May 2015 at 05:48:13 UTC, zhmt wrote:




The code you posted is the client code, but the issue seems to 
be on the server side.

Can you post the server code and also the timing code?



@Márcio Martins

here is the server code:

void echoServer()
{
Acceptor acc2 = new Acceptor();
acc2.listen(0.0.0.0,8881,100);
acc2.accept((Ptr!Conn c){
scope(exit) c.close();

ubyte[5] buf;
while(true)
{
int n = c.readSome(buf);
if(n=0){
break;
}
c.write(buf[0..n]);
}
writeFlush(close server sock);
});
}

and I found the reason:
the buffer size on server is too small, just five,so if the data 
size is more than 5(1 for example),it will need two read 
operation。


The length of “1” is 5,it perfectly matched the point of 
falling down。

If change the buffer size to 100, it runs well。

But why it falls sharply down to about 24,I will continue to 
study。


Template problem - if arg[0]==something; call arg[1];

2015-05-27 Thread wobbles via Digitalmars-d-learn

I have some code that I'd really like to template-ize.

Currently, I've the same construct all over my code, repeated(yet 
slightly different) about 15 times.

Too much for me, and I'd like to make it into a template.

This is a sample of my current code:
http://dpaste.dzfl.pl/76814846898e

I essentially want to change the
string helper(){
snip
foreach(child; toSpawn.children){
switch(child.name){
case NameOfArg1:
str ~= FunctionToCall1;
break;
case NameOfArg2:
str ~= FunctionToCall2;
break;
snip
}
function to something like:

handleParseTreeHelper!( string NameOfArg1, delegate 
FunctionToCall1,
string NameOfArg2, delegate 
FunctionToCall2 );


Any idears?


Re: Template problem - if arg[0]==something; call arg[1];

2015-05-27 Thread ketmar via Digitalmars-d-learn
On Wed, 27 May 2015 22:49:18 +, ketmar wrote:

i.e. `constructCases~(Aliases[2..$])`
`constructCases!(Aliases[2..$])`, of course ;-)

signature.asc
Description: PGP signature


Re: Template problem - if arg[0]==something; call arg[1];

2015-05-27 Thread ketmar via Digitalmars-d-learn
On Wed, 27 May 2015 22:35:54 +, wobbles wrote:

quick looking thru your code raises two questions:

1. didn't you forgot to add `.stringof` in `constructCases`?
2. didn't you forgot `!` in `constructCases(Aliases[2..$])`?
   i.e. `constructCases~(Aliases[2..$])`

it may work with that.

signature.asc
Description: PGP signature


Re: problem with gc?

2015-05-27 Thread thedeemon via Digitalmars-d-learn

On Wednesday, 27 May 2015 at 05:48:13 UTC, zhmt wrote:

What happened when the code changes a little? Who will give an 
explaination,Thanks a lot?


Yes, on the first sight it looks like your allocations in the 
loop make GC spend too much time. I don't think scope does 
anything here. Try adding delete str; to the end of the loop, 
it should do what your scope meant. I know it's deprecated but 
sometimes it's just right.


Re: problem with gc?

2015-05-27 Thread jklp via Digitalmars-d-learn

On Wednesday, 27 May 2015 at 05:48:13 UTC, zhmt wrote:

I am writing a echoclient, as below:

Ptr!Conn conn = connect(127.0.0.1,8881);
ubyte[100] buf;
for(int i=0; iN; i++)
{
scope string str = format(%s,i);
conn.write((cast(ubyte*)str.ptr)[0..str.length]);
conn.read(buf[0..str.length]);
n++;
}
conn.close();


When it loops about more than 10 times,the throughput will 
fall from 6request/second to 26request/second.


If the code changes a little as below:

 scope string str = format(%s,1); //changes to a constant

It will runs normally with speed of 6request/second. I test 
for 13minutes, everything seems well.




What happened when the code changes a little? Who will give an 
explaination,Thanks a lot?


Have you tried to declare str in the upper scope ?

---
Ptr!Conn conn = connect(127.0.0.1,8881);
ubyte[100] buf;
string str;
for(int i=0; iN; i++)
{
str = format(%s,i);
conn.write(cast(ubyte[]) str);
conn.read(buf[0..str.length]);
n++;
}
conn.close();
---

And also you could try to surround the whole block with 
`GC.disable` and `GC.enable`. This would help to determine if the 
GC is involved:


---
Ptr!Conn conn = connect(127.0.0.1,8881);
GC.disable;
ubyte[100] buf;
string str;
for(int i=0; iN; i++)
{
str = format(%s,i);
conn.write(cast(ubyte[]) str);
conn.read(buf[0..str.length]);
n++;
}
GC.enable;
conn.close();
---


Re: problem with gc?

2015-05-27 Thread Daniel Kozák via Digitalmars-d-learn

On Wed, 27 May 2015 05:48:11 +
zhmt via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote:

 I am writing a echoclient, as below:
 
 Ptr!Conn conn = connect(127.0.0.1,8881);
 ubyte[100] buf;
 for(int i=0; iN; i++)
 {
   scope string str = format(%s,i);
   conn.write((cast(ubyte*)str.ptr)[0..str.length]);
   conn.read(buf[0..str.length]);
   n++;
 }
 conn.close();
 
 
 When it loops about more than 10 times,the throughput will 
 fall from 6request/second to 26request/second.
 
 If the code changes a little as below:
 
   scope string str = format(%s,1); //changes to a constant
 
 It will runs normally with speed of 6request/second. I test 
 for 13minutes, everything seems well.
 
 
 
 What happened when the code changes a little? Who will give an 
 explaination,Thanks a lot?

what happend if you use sformat instead?

Ptr!Conn conn = connect(127.0.0.1,8881);
ubyte[100] buf;
char[100] buf2;
for(int i=0; iN; i++)
{
auto str = sformat(buf2, %s,i);
conn.write((cast(ubyte*)str.ptr)[0..str.length]);
conn.read(buf[0..str.length]);
n++;
}
conn.close();


Prevent slices from referencing old array after realocation?

2015-05-27 Thread Gr8Ape via Digitalmars-d-learn

I'm writing a program that handles lots of slices of larger
arrays. I need the data of each of them all together for one
operation, but otherwise it's more convenient to handle the
elements in small groups. I could just store a pointer to my
array, an offset, and a length but since I'm using D I'd much
prefer to use slices...

So I was looking through the documentation on slices and found
out that if the array has to realocate then slices pointing to
that array will keep on pointing to that block of memory, rather
than the new location. Given that I'll be working with arrays
that can vary wildly in size and that giving all of them the
maximum capacity so they never realocate would be extremely
inefficent, I need a workaround.

So I'm wondering if there's a way to tell all the slices of an
underlying array to now point to the new underlying array upon
reallocation? Or would I have to iterate through and reassign
every slice?


Re: problem with gc?

2015-05-27 Thread Kagamin via Digitalmars-d-learn

On Wednesday, 27 May 2015 at 05:48:13 UTC, zhmt wrote:

I am writing a echoclient, as below:

Ptr!Conn conn = connect(127.0.0.1,8881);
ubyte[100] buf;
for(int i=0; iN; i++)
{
scope string str = format(%s,i);
conn.write((cast(ubyte*)str.ptr)[0..str.length]);
conn.read(buf[0..str.length]);
n++;
}
conn.close();


You can use sformat 
http://dlang.org/phobos/std_format.html#.sformat with your buffer.