Commit c8c5b829 authored by Oleg Korshul's avatar Oleg Korshul

cashed scripts data scheme (may be disable)

parent 2e99efee
...@@ -1185,6 +1185,53 @@ namespace NSFile ...@@ -1185,6 +1185,53 @@ namespace NSFile
} }
return sPath; return sPath;
} }
// CommonFunctions
static std::wstring GetFileExtention(const std::wstring& sPath)
{
std::wstring::size_type nPos = sPath.rfind('.');
if (nPos != std::wstring::npos)
return sPath.substr(nPos + 1);
return sPath;
}
static std::wstring GetFileName(const std::wstring& sPath)
{
std::wstring::size_type nPos1 = sPath.rfind('\\');
std::wstring::size_type nPos2 = sPath.rfind('/');
std::wstring::size_type nPos = std::wstring::npos;
if (nPos1 != std::wstring::npos)
{
nPos = nPos1;
if (nPos2 != std::wstring::npos && nPos2 > nPos)
nPos = nPos2;
}
else
nPos = nPos2;
if (nPos == std::wstring::npos)
return sPath;
return sPath.substr(nPos + 1);
}
static std::wstring GetDirectoryName(const std::wstring& sPath)
{
std::wstring::size_type nPos1 = sPath.rfind('\\');
std::wstring::size_type nPos2 = sPath.rfind('/');
std::wstring::size_type nPos = std::wstring::npos;
if (nPos1 != std::wstring::npos)
{
nPos = nPos1;
if (nPos2 != std::wstring::npos && nPos2 > nPos)
nPos = nPos2;
}
else
nPos = nPos2;
if (nPos == std::wstring::npos)
return sPath;
return sPath.substr(0, nPos);
}
} }
#endif //_BUILD_FILE_CROSSPLATFORM_H_ #endif //_BUILD_FILE_CROSSPLATFORM_H_
...@@ -172,7 +172,7 @@ public: ...@@ -172,7 +172,7 @@ public:
return true; return true;
} }
bool OpenFile(const std::wstring& sBasePath, const std::wstring& path, const std::string& sString) bool OpenFile(const std::wstring& sBasePath, const std::wstring& path, const std::string& sString, const std::wstring& sCachePath)
{ {
LOGGER_SPEED_START LOGGER_SPEED_START
...@@ -181,7 +181,15 @@ public: ...@@ -181,7 +181,15 @@ public:
v8::TryCatch try_catch; v8::TryCatch try_catch;
v8::Local<v8::String> source = v8::String::NewFromUtf8(m_isolate, sString.c_str()); v8::Local<v8::String> source = v8::String::NewFromUtf8(m_isolate, sString.c_str());
v8::Local<v8::Script> script = v8::Script::Compile(source); v8::Local<v8::Script> script;
CCacheDataScript oCachedScript(sCachePath);
if (sCachePath.empty())
script = v8::Script::Compile(source);
else
{
script = oCachedScript.Compile(m_context, source);
}
LOGGER_SPEED_LAP("compile") LOGGER_SPEED_LAP("compile")
...@@ -463,6 +471,8 @@ namespace NSDoctRenderer ...@@ -463,6 +471,8 @@ namespace NSDoctRenderer
CDocBuilderParams m_oParams; CDocBuilderParams m_oParams;
bool m_bIsInit; bool m_bIsInit;
bool m_bIsCacheScript;
public: public:
CDocBuilder_Private() CDocBuilder_Private()
{ {
...@@ -478,6 +488,7 @@ namespace NSDoctRenderer ...@@ -478,6 +488,7 @@ namespace NSDoctRenderer
m_pAdditionalData = NULL; m_pAdditionalData = NULL;
m_bIsInit = false; m_bIsInit = false;
m_bIsCacheScript = true;
} }
void Init() void Init()
...@@ -1125,7 +1136,11 @@ namespace NSDoctRenderer ...@@ -1125,7 +1136,11 @@ namespace NSDoctRenderer
m_pWorker->m_nFileType = m_nFileType; m_pWorker->m_nFileType = m_nFileType;
m_pWorker->m_sUtf8ArgumentJSON = m_oParams.m_sArgumentJSON; m_pWorker->m_sUtf8ArgumentJSON = m_oParams.m_sArgumentJSON;
bool bOpen = m_pWorker->OpenFile(m_sX2tPath, m_sFileDir, GetScript()); std::wstring sCachePath = L"";
if (m_bIsCacheScript)
sCachePath = GetScriptCache();
bool bOpen = m_pWorker->OpenFile(m_sX2tPath, m_sFileDir, GetScript(), sCachePath);
if (!bOpen) if (!bOpen)
return false; return false;
} }
...@@ -1181,6 +1196,37 @@ namespace NSDoctRenderer ...@@ -1181,6 +1196,37 @@ namespace NSDoctRenderer
return strScript; return strScript;
} }
std::wstring GetScriptCache()
{
std::vector<std::wstring>* arSdkFiles = NULL;
switch (m_nFileType)
{
case 0:
{
arSdkFiles = &m_arDoctSDK;
break;
}
case 1:
{
arSdkFiles = &m_arPpttSDK;
break;
}
case 2:
{
arSdkFiles = &m_arXlstSDK;
break;
}
default:
return L"";
}
if (0 < arSdkFiles->size())
{
return NSCommon::GetDirectoryName(*arSdkFiles->begin()) + L"/sdk-all.cache";
}
return L"";
}
std::string ReadScriptFile(const std::wstring& strFile) std::string ReadScriptFile(const std::wstring& strFile)
{ {
NSFile::CFileBinary oFile; NSFile::CFileBinary oFile;
...@@ -1450,6 +1496,8 @@ namespace NSDoctRenderer ...@@ -1450,6 +1496,8 @@ namespace NSDoctRenderer
m_pInternal->m_oParams.m_bCheckFonts = true; m_pInternal->m_oParams.m_bCheckFonts = true;
else if (sParam == "--work-directory") else if (sParam == "--work-directory")
m_pInternal->m_oParams.m_sWorkDir = std::wstring(value); m_pInternal->m_oParams.m_sWorkDir = std::wstring(value);
else if (sParam == "--cache-scripts")
m_pInternal->m_bIsCacheScript = (std::wstring(value) == L"true");
else if (sParam == "--argument") else if (sParam == "--argument")
{ {
std::wstring sArg(value); std::wstring sArg(value);
......
...@@ -70,6 +70,8 @@ namespace NSDoctRenderer ...@@ -70,6 +70,8 @@ namespace NSDoctRenderer
bool m_bIsOnlyOnePage; bool m_bIsOnlyOnePage;
bool m_bIsCachedScripts;
public: public:
CExecuteParams() : m_arChanges() CExecuteParams() : m_arChanges()
{ {
...@@ -94,6 +96,7 @@ namespace NSDoctRenderer ...@@ -94,6 +96,7 @@ namespace NSDoctRenderer
m_nSaveToPDFParams = 0; m_nSaveToPDFParams = 0;
m_bIsOnlyOnePage = false; m_bIsOnlyOnePage = false;
m_bIsCachedScripts = true;
} }
~CExecuteParams() ~CExecuteParams()
{ {
...@@ -151,6 +154,9 @@ namespace NSDoctRenderer ...@@ -151,6 +154,9 @@ namespace NSDoctRenderer
if (nParams & 0x02) if (nParams & 0x02)
m_nSaveToPDFParams = 1; m_nSaveToPDFParams = 1;
if (nParams & 0x04)
m_bIsCachedScripts = false;
m_bIsOnlyOnePage = (oNode.ReadValueInt(L"OnlyOnePage", 0) == 1) ? true : false; m_bIsOnlyOnePage = (oNode.ReadValueInt(L"OnlyOnePage", 0) == 1) ? true : false;
return true; return true;
...@@ -584,7 +590,7 @@ namespace NSDoctRenderer ...@@ -584,7 +590,7 @@ namespace NSDoctRenderer
} }
bool ExecuteScript(const std::string& strScript, std::wstring& strError, std::wstring& strReturnParams) bool ExecuteScript(const std::string& strScript, const std::wstring& sCachePath, std::wstring& strError, std::wstring& strReturnParams)
{ {
bool bIsBreak = false; bool bIsBreak = false;
v8::Isolate* isolate = CV8Worker::getInitializer()->CreateNew(); v8::Isolate* isolate = CV8Worker::getInitializer()->CreateNew();
...@@ -604,7 +610,15 @@ namespace NSDoctRenderer ...@@ -604,7 +610,15 @@ namespace NSDoctRenderer
v8::Context::Scope context_scope(context); v8::Context::Scope context_scope(context);
v8::TryCatch try_catch; v8::TryCatch try_catch;
v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate, strScript.c_str()); v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate, strScript.c_str());
v8::Local<v8::Script> script = v8::Script::Compile(source); v8::Local<v8::Script> script;
CCacheDataScript oCachedScript(sCachePath);
if (sCachePath.empty())
script = v8::Script::Compile(source);
else
{
script = oCachedScript.Compile(context, source);
}
// COMPILE // COMPILE
if (try_catch.HasCaught()) if (try_catch.HasCaught())
...@@ -1096,8 +1110,14 @@ namespace NSDoctRenderer ...@@ -1096,8 +1110,14 @@ namespace NSDoctRenderer
strScript += "\n\n"; strScript += "\n\n";
} }
std::wstring sCachePath = L"";
if (NULL != arSdkFiles) if (NULL != arSdkFiles)
{ {
if (m_pInternal->m_oParams.m_bIsCachedScripts && (0 < arSdkFiles->size()))
{
sCachePath = NSFile::GetDirectoryName(*arSdkFiles->begin()) + L"/sdk-all.cache";
}
for (std::vector<std::wstring>::iterator i = arSdkFiles->begin(); i != arSdkFiles->end(); i++) for (std::vector<std::wstring>::iterator i = arSdkFiles->begin(); i != arSdkFiles->end(); i++)
{ {
strScript += m_pInternal->ReadScriptFile(*i); strScript += m_pInternal->ReadScriptFile(*i);
...@@ -1109,7 +1129,7 @@ namespace NSDoctRenderer ...@@ -1109,7 +1129,7 @@ namespace NSDoctRenderer
strScript += "\n$.ready();"; strScript += "\n$.ready();";
std::wstring sReturnParams = L""; std::wstring sReturnParams = L"";
bool bResult = m_pInternal->ExecuteScript(strScript, strError, sReturnParams); bool bResult = m_pInternal->ExecuteScript(strScript, sCachePath, strError, sReturnParams);
if (strError.length() != 0) if (strError.length() != 0)
{ {
......
...@@ -977,4 +977,69 @@ bool Doct_renderer_SaveFile_ForBuilder(int nFormat, const std::wstring& strDstFi ...@@ -977,4 +977,69 @@ bool Doct_renderer_SaveFile_ForBuilder(int nFormat, const std::wstring& strDstFi
v8::TryCatch& try_catch, v8::TryCatch& try_catch,
std::wstring& strError); std::wstring& strError);
class CCacheDataScript
{
private:
BYTE* Data;
int Length;
v8::ScriptCompiler::Source* Source;
v8::ScriptCompiler::CachedData* CachedData;
std::wstring Path;
public:
CCacheDataScript(const std::wstring& sPath)
{
Data = NULL;
Length = 0;
if (!sPath.empty())
{
BYTE* _data = NULL;
DWORD _data_length = 0;
if (NSFile::CFileBinary::ReadAllBytes(sPath, &_data, _data_length))
{
Data = _data;
Length = (int)_data_length;
}
}
Source = NULL;
CachedData = NULL;
Path = sPath;
}
~CCacheDataScript()
{
//RELEASEOBJECT(Source);
//RELEASEOBJECT(CachedData);
RELEASEARRAYOBJECTS(Data);
}
v8::Local<v8::Script> Compile(const v8::Local<v8::Context>& _context, const v8::Local<v8::String>& source)
{
v8::Local<v8::Script> script;
if (NULL == Data)
{
Source = new v8::ScriptCompiler::Source(source);
script = v8::ScriptCompiler::Compile(_context, Source, v8::ScriptCompiler::kProduceCodeCache).ToLocalChecked();
const v8::ScriptCompiler::CachedData* _cachedData = Source->GetCachedData();
NSFile::CFileBinary oFileTest;
if (oFileTest.CreateFileW(Path))
{
oFileTest.WriteFile(_cachedData->data, (DWORD)_cachedData->length);
oFileTest.CloseFile();
}
}
else
{
CachedData = new v8::ScriptCompiler::CachedData(Data, Length);
Source = new v8::ScriptCompiler::Source(source, CachedData);
script = v8::ScriptCompiler::Compile(_context, Source, v8::ScriptCompiler::kConsumeCodeCache).ToLocalChecked();
}
return script;
}
};
#endif // NATIVECONTROL #endif // NATIVECONTROL
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment