- Home /
Issues with memory stream or File saving potentially affecting performance?
Hello all I am working on a project where I am saving and storing a lot of to mobile devices. roughly 10 megabytes of of JSON Data converted to a string and stored in player prefs and converting a float array to bytes data and storing those bytes in a directory in the persistent data path .
I have checked and re-checked my code and all the lists and objects we are using to store and track the data are being de-allocated yet whenever we write the audio bytes to our memory stream we gradually see performance drops.
the amount of data isn't even the problem , it takes 6 save attempts before the performance drops so far that the project is basically paused . and this rung true for 6 5 second playbacks the same it did for 6 1 minute playbacks .
I just wanted to know if anyone who has done something similar has seen such performance drops and if there are any known ways to rectify the issue ? below is some code of my save process as it is now .
string audioClipName ="Slot"+pslot.ToString()+ "clip" + L.ToString(); string filePath = "";
if(Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
{
//*** Here we create a file path based on combining our file name with our data path
filePath = Path.Combine(Application.dataPath ,audioClipName);
// if this file exists , we read the existing bytes
if(File.Exists(filePath)){
//*** we remove pre-existing data
System.IO.File.Delete(filePath);
}
//else if unity is currently on an iphone or android device , load bytes from the persistent data path
}else if(Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android){
//*** Here we create a file path based on combining our file name with our data path
filePath = Path.Combine(Application.persistentDataPath ,audioClipName);
// if this file exists , we read the existing bytes
if(File.Exists(filePath)){
//*** we remove pre-existing data
System.IO.File.Delete(filePath);
}
}
//yield return new WaitForEndOfFrame();
bytes = ConvertFloatsToBytes(audioFloatData);
DebugTools.Log(" converting bytes to float ");
yield return new WaitForEndOfFrame();
System.IO.File.WriteAllBytes(filePath,bytes);
DebugTools.Log(" Saved bytes ");
We use the yield statements to give a brief pause in between actions, we thought running intensive process immediately after intensive process was possibly making the save flow even more processor intensive than it had to be .
I nearly included the playerprefs save functionality but I honestly don't think that's where the performance drop is coming from . I haven't done much save functionality so any and all advice or resources are appreciated.
File operations tend to be slow, especially if you're reading or writing a large number of bytes. If you can't split those I/O operations into smaller pieces (for example, handling a few bytes per frame rather than all of them at once), you might try waiting until there's a lull in the gameplay, perhaps when switching levels or some such time.
Unfortunately our entire game takes place in primarily one scene but I actually will take a look into breaking down the byte writing into separate smaller sections , that's a fantastic suggestion , Thank you !!!!