Regex.Replace takes 100 ms in SmartFox.Send

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

wren
Posts: 6
Joined: 05 Nov 2012, 06:57

Regex.Replace takes 100 ms in SmartFox.Send

Postby wren » 08 Nov 2012, 06:41

Hi,


In my test project, the Unity Profiler shows that Regex.Place called inside SmartFox.Send takes about 100ms, which sums up to about 90% of the script CPU time each frame.

Is there any way to get around this expensive use of Regex?

Thanks.

Some details:

- The call stack shown in the Unity Profiler is:

SmartFox.Send()
BitSwarmClient.Send()
SFSProtocolCodex.OnPacketWrite()
SFSIOHandler.OnDataWrite()
BBClient.Send()
BBClient.SendRequest()
SFSWebClient.UploadValuesAsync()
Regex.Replace()

- My code looks like this:

MyGameObject obj = entry.Value;
transferObjectSend = new SFSObject();
transferObjectSend.PutUtfString("messageID", "gameObject");
transferObjectSend.PutUtfString("objectID", obj.objectID);
transferObjectSend.PutUtfString("userID", obj.userID);
transferObjectSend.PutUtfString("prefab", obj.prefab);
transferObjectSend.PutFloatArray("locomotion", new float[]
{
obj.transform.position.x,
obj.transform.position.y,
obj.transform.position.z,
obj.transform.rotation.x,
obj.transform.rotation.y,
obj.transform.rotation.z,
obj.transform.rotation.w
});
transferObjectSend.PutUtfString("currentState", obj.currentState);
smartFox.Send(new ObjectMessageRequest(transferObjectSend, smartFox.LastJoinedRoom));
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Regex.Replace takes 100 ms in SmartFox.Send

Postby Lapo » 08 Nov 2012, 10:15

Just one note. A profiler will always show you a relative percentage of the CPU usage. 90% in this case doesn't mean 90% of the CPU time.
The CPU could very well be at 2% and within that 2% most of the time (90%) is spent in that part of the code.
Lapo
--
gotoAndPlay()
...addicted to flash games
wren
Posts: 6
Joined: 05 Nov 2012, 06:57

Re: Regex.Replace takes 100 ms in SmartFox.Send

Postby wren » 08 Nov 2012, 14:51

Thanks Lapo.

I'm totally aware of this. That's why I said "script CPU time", meaning the CPU time spent on script. Within this time, the call of SmartFox.Send takes 90%, and it take 100 ms (I know when profiling, code takes longer time, but 100 ms is still too much). And actually it makes the frame rate < 5 fps. Isn't it a pity when the bottleneck is not network but some Regex work?

Sorry for pushing this hard but performance is kind of crucial for our game.

Thanks
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Regex.Replace takes 100 ms in SmartFox.Send

Postby Lapo » 08 Nov 2012, 15:02

On what kind of CPU is this? Is it a computer or mobile phone?
Also does this happen all the times? VM usually need some warm-up times to speed up code execution.

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
wren
Posts: 6
Joined: 05 Nov 2012, 06:57

Re: Regex.Replace takes 100 ms in SmartFox.Send

Postby wren » 10 Nov 2012, 15:51

It's running in the Unity Editor, on a pretty much cutting edge PC with i7 cpu.
Yes, it happens all the time. In the Unity Profiler, It shows big spikes on the frames where SmartFox.Send is called.
I've tried making the strings shorter, and send fewer times, but it just doesn't seem to meet a real time game's need. Please check the attached screen shot.

I also run into some other problems:
- At times, a client may not receive any message for a couple of seconds, after that it goes back to normal again, then it comes again a few seconds not receiving message.
- When restart or stop game in Unity Editor, it often cause Unity Editor to freeze. I already disconnect it in the application quit call back, and I'm sure it's properly called, but it doesn't help.

Thanks.
Attachments
Untitled.png
(195.34 KiB) Not downloaded yet
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Regex.Replace takes 100 ms in SmartFox.Send

Postby Lapo » 10 Nov 2012, 18:58

I will need ThomasLund to look into this.
As a side note you're using and HTTP tunneled connection (BlueBox). I would not expect this to happen with regular sockets.

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Re: Regex.Replace takes 100 ms in SmartFox.Send

Postby ThomasLund » 25 Nov 2012, 15:51

We only use regex 2 places and both these are in bluebox code only. Meaning the web based protocol when socket connections fail.

So the quick answer is to use sockets if you want highest performance.

But I'll take a look at the code and see if we can rework the 2 regex into something cheaper performance wise! I'm somewhat sure I can remove one of them at least.

Edit after taking a closer look:
The "easy to replace one" is the one taking 0.89 ms in your profiler (the split). I doubt that can be done faster. The other one is not taking 100 ms, but 50.26 ms. It uses a real regex so will be somewhat harder to rework. But as promised, I'll try.

On the 2 other issues
- At times, a client may not receive any message for a couple of seconds, after that it goes back to normal again, then it comes again a few seconds not receiving message.
Uncertain what what could be and if its client or server related. Without more info I cant do anything here.

- When restart or stop game in Unity Editor, it often cause Unity Editor to freeze. I already disconnect it in the application quit call back, and I'm sure it's properly called, but it doesn't help.
Make certain you run one of the latest released API versions. Long time ago we fixed this - and internally on all our editors we havent had a freeze for months. And we have been doing a lot of daily testing with SFS.

And edit #2 after doing some testing:
I've replaced the expensive regex with a TrimEnd instead. My small tests on a 50 kb string showed huge improvements. If you want to test in your scenario, shoot me your email in a PM and I'll send you a developer build from SVN.

/Thomas
Full Control - maker of Unity/C# and Java SFS API and indie games
Follow on twitter: http://twitter.com/thomas_h_lund

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 47 guests