Commits

Anonymous committed f257106

Refactor things to make timings much easier as we start to optimize

Comments (0)

Files changed (10)

Executable/stdafx.h

 //
 
 #pragma once
-
 #include "targetver.h"
 
 #include <stdio.h>

ManagedAPI/ManagedAPI.h

 {
 namespace Managed
 {
-	void Log(std::string message)
+	void Log(const std::string& message)
 	{
 		System::Diagnostics::Trace::WriteLine(gcnew System::String(message.c_str()));
 	}

ManagedAPI/Stdafx.h

 
 #pragma once
 
-

UnitTests/Stdafx.h

 
 #pragma once
 
-
 		typedef std::auto_ptr<Ppmz> SafePpmz;
     public:
 
-		Coder(void (*loggingCallback)(std::string))
+		Coder(void (*loggingCallback)(const std::string&))
 			: _loggingCallback(loggingCallback)
 		{}
 
 
     private:
         static const unsigned int PREAMBLE = 1024;
-		void (*_loggingCallback)(std::string);
+		void (*_loggingCallback)(const std::string&);
 
         unsigned int EncodeArrayPrivate(unsigned char *rawBuf, unsigned int rawLen, unsigned char *compBuf, CodingOptions options)
         {

ppmz2/CodingMetrics.h

 {
 	__int64 UpdatingTime;
 	__int64 LOETime;
-	__int64 DetTime;
+	__int64 DetCodingTime;
+	__int64 DetUpdatingTime;
+	__int64 MinusOneOrderTime;
+	__int64 CodeFromOrderTime;
+	int BytesDetEncoded;
 };
 #include "stdafx.h"
 #include "PPMZ2.h"
+#include "Stopwatch.h"
+
+__int64 __declspec(dllexport) Ppmz2::Ppmz::Update(Context** contexts, int sym, unsigned long cntx, int order)
+{
+	Stopwatch timer;
+
+	if ( ! (_options & CodingOptions::NoUpdate) )
+	{
+	    int codedOrder = max(order, 0);
+
+	    for(order = 0; order <= _order; ++order)
+	    {					 
+            contexts[order]->Update(sym, cntx, _see, codedOrder);				            
+	    }
+	}
+	return timer.Elapsed();
+}
+
+__declspec(dllexport) __int64 Ppmz2::Ppmz::EncodeOrderMinusOne(int sym)
+{
+	Stopwatch timer;
+	// encode raw with order -1
+	if ( _options & CodingOptions::TextMode )
+		EncodeOrderMinusOneText(sym);
+	else
+		EncodeOrderMinusOne(sym, 256);
+	return timer.Elapsed();
+}
+
+__declspec(dllexport) __int64 Ppmz2::Ppmz::EncodeFromOrder(Context** contexts, int sym, unsigned long cntx, bool* useFull, CodingMetrics* metrics)
+{
+	Stopwatch timer;
+	// do the initial LOE to pick a start order
+    int LOE_Order = LocalOrderEstimation::ChooseOrder(_loeType, contexts, cntx, PPMZ2_Order, _exclude, _see, *useFull);
+
+	// go down the orders
+
+	for(int order = LOE_Order; ; )
+	{
+	    // try to coder from order
+	    
+	    if ( EncodeFromContext(contexts[order], cntx, sym, useFull) )
+	    {
+	        break;
+	    }
+    
+	    if ( order == 0 )
+		{
+			metrics->MinusOneOrderTime += EncodeOrderMinusOne(sym);
+	        break;
+		}
+    		
+	    // maybe skip down a few :
+
+        order = LocalOrderEstimation::ChooseOrder(_loeType, contexts, cntx, order-1, _exclude, _see, useFull);
+	}
+	return timer.Elapsed();
+}
+
+__declspec(dllexport) bool Ppmz2::Ppmz::DetEncode(unsigned char* rawPtr, unsigned char* rawBuf, int sym, Context** contexts, bool* useFull, __int64* codingTime)
+{
+	Stopwatch timer;
+	bool retVal = _det->Encode(_arithInfo, rawPtr, rawBuf, sym, _exclude, contexts[PPMZ2_Order], useFull);
+	__int64 elapsed = timer.Elapsed();
+	*codingTime += elapsed;
+	return retVal;
+}
+
+
+__declspec(dllexport) __int64 Ppmz2::Ppmz::DetUpdate(unsigned char* rawPtr, unsigned char* rawBuf, int sym)
+{
+	Stopwatch timer;
+	if ( ! (_options & CodingOptions::NoUpdate) )
+	{
+		_det->Update(rawPtr, rawBuf, sym);
+	}
+	return timer.Elapsed();
+}
 #include "Context.h"
 #include "ContextData.h"
 #include "IntMath.h"
+#include "CodingMetrics.h"
 
 const unsigned int PPMZ2_MaxContextLen	=32;
 const unsigned int PPMZ2_SeedBytes		=8;
         int _detMegs;
         CodingOptions _options;
         LocalOrderEstimation::LOEType _loeType;
-		void (*_loggingCallback)(std::string);
+		void (*_loggingCallback)(const std::string&);
 
     public:
-        static Ppmz* Create(int order, int trieMegs, int detMegs, CodingOptions options, LocalOrderEstimation::LOEType loeType, void (*loggingCallback)(std::string))
+        static Ppmz* Create(int order, int trieMegs, int detMegs, CodingOptions options, LocalOrderEstimation::LOEType loeType, void (*loggingCallback)(const std::string&))
         {
             // TODO: validate arguments
             Ppmz* ppz = new Ppmz();
             return ppz;
         }
 
-        static Ppmz* Create(CodingOptions options, void (*loggingCallback)(std::string))
+        static Ppmz* Create(CodingOptions options, void (*loggingCallback)(const std::string&))
         {
 			return Create(8, 72, 4, options, LocalOrderEstimation::LOEType::LOETYPE_MPS, loggingCallback);
         }
         }
 
         unsigned int EncodeArraySub(unsigned char* rawBuf, unsigned int rawLen, unsigned char* compBuf)
-        {			
+        {
+			CodingMetrics metrics = {0};
+			
 	        unsigned char* rawPtr = rawBuf;
 	        unsigned char* rawBufEnd = rawBuf + rawLen;
 
 
 	        while(rawPtr < rawBufEnd)
 	        {
-				std::vector<unsigned long> tindeces(_order);
-                // TODO: new this up and delete it
+				std::vector<unsigned long> tindeces(_order);                
 	            Context* contexts[PPMZ2_Order+1];
 	            int order, codedOrder;
 
 
 		        bool useFull = true;
 
-		        if ( ! _det->Encode(_arithInfo, rawPtr, rawBuf, sym, _exclude, contexts[PPMZ2_Order], &useFull) )
+
+				if (DetEncode(rawPtr, rawBuf, sym, contexts, &useFull, &(metrics.DetCodingTime)))
+				{
+					metrics.BytesDetEncoded++;
+				}
+				else
 		        {
-			        // do the initial LOE to pick a start order
-
-                    int LOE_Order = LocalOrderEstimation::ChooseOrder(_loeType, contexts, cntx, PPMZ2_Order, _exclude, _see, useFull);
-
-			        // go down the orders
-
-			        for(order = LOE_Order; ; )
-			        {
-			            // try to coder from order
-			            
-			            if ( EncodeFromContext(contexts[order], cntx, sym, &useFull) )
-			            {					            
-				            break;
-			            }
-            		
-			            if ( order == 0 )
-						{
-							// encode raw with order -1
-							if ( _options & CodingOptions::TextMode )
-								EncodeOrderMinusOneText(sym);
-							else
-								EncodeOrderMinusOne(sym, 256);
-				            break;
-						}
-            				
-			            // maybe skip down a few :
-
-                        order = LocalOrderEstimation::ChooseOrder(_loeType, contexts, cntx, order-1, _exclude, _see, useFull);
-			        }
-            		
-			        if ( ! (_options & CodingOptions::NoUpdate) )
-			        {
-			            codedOrder = max(order, 0);
-
-			            for(order=0; order <= _order; ++order)
-			            {
-                            contexts[order]->Update(sym, cntx, _see, codedOrder);				            
-			            }
-			        }
-		        }
-		        else
-		        {
-                    // TODO: logging and stats
+			        metrics.CodeFromOrderTime += EncodeFromOrder(contexts, sym, cntx, &useFull, &metrics);
+					metrics.UpdatingTime += Update(contexts, sym, cntx, codedOrder);
 		        }
 
-		        if ( ! (_options & CodingOptions::NoUpdate) )
-		        {
-			        _det->Update(rawPtr, rawBuf, sym);
-		        }
+		        metrics.DetUpdatingTime += DetUpdate(rawPtr, rawBuf, sym);
 
 		        rawPtr++;
-		        
-                // TODO: logging
-		        // int i = rawPtr - rawBuf;
+
+				//for (int i = 0; i < PPMZ2_Order+1; ++i)
+				//	delete contexts[i];
 	        }
 
             unsigned int compLen = _arithInfo->EncodeDoneMinimal() + PPMZ2_SeedBytes;
 			SafeLog("Done encoding");
             return compLen;
         }
+
+		__declspec(dllexport) __int64 EncodeOrderMinusOne(int sym);
+		__declspec(dllexport) __int64 Update(Context** contexts, int sym, unsigned long cntx, int order);
+		__declspec(dllexport) __int64 EncodeFromOrder(Context** contexts, int sym, unsigned long cntx, bool* useFull, CodingMetrics* metrics);
+		__declspec(dllexport) bool DetEncode(unsigned char* rawPtr, unsigned char* rawBuf, int sym, Context** contexts, bool* useFull, __int64* codingTime);
+		__declspec(dllexport) __int64 DetUpdate(unsigned char* rawPtr, unsigned char* rawBuf, int sym);
         
         bool DecodeArraySub(unsigned char* rawBuf, unsigned int rawLength, unsigned char* compBuf)
         {
+			CodingMetrics metrics = {0};
+
             unsigned char* rawPtr = rawBuf;
 	        unsigned char* rawBufEnd = rawBuf + rawLength;
 
 				        // maybe skip down a few :
                         order = LocalOrderEstimation::ChooseOrder(_loeType, contexts, cntx, order-1, _exclude, _see, useFull);
 			        }
-        			
-			        if ( ! (_options & CodingOptions::NoUpdate) )
-			        {
-				        codedOrder = max(order, 0);
 
-				        for(order = 0; order <= PPMZ2_Order; ++order)
-				        {
-                            contexts[order]->Update(sym, cntx, _see, codedOrder);
-				        }
-			        }
+					metrics.UpdatingTime += Update(contexts, sym, cntx, codedOrder);
 		        }
 
 		        if ( ! (_options & CodingOptions::NoUpdate) )
 			SafeLog("Done decoding");
             return true;
         }
-
         
         void ReInitCoder()
         {            
         return false;
         }
 
-		void SafeLog(std::string message)
-		{			
+		void SafeLog(const std::string& message)
+		{
 			if (_loggingCallback)
 				_loggingCallback(message);
 		}

ppmz2/Stopwatch.cpp

 #include "stdafx.h"
 
-#include <windows.h>
 #include "Stopwatch.h"
 
 Stopwatch::Stopwatch()
 	QueryPerformanceCounter(&m_liPerfStart);
 }
 
-__int64 Stopwatch::Now() const
+__int64 Stopwatch::Elapsed() const
 {
 	LARGE_INTEGER liPerfNow;
 	QueryPerformanceCounter(&liPerfNow);
-	return (((liPerfNow.QuadPart - m_liPerfStart.QuadPart) * 1000) / m_liPerfFreq.QuadPart);
-}
-
-__int64 Stopwatch::NowInMicro() const
-{
-	LARGE_INTEGER liPerfNow;
-	QueryPerformanceCounter(&liPerfNow);
-	return (((liPerfNow.QuadPart - m_liPerfStart.QuadPart) * 1000000) / m_liPerfFreq.QuadPart);
+	return liPerfNow.QuadPart - m_liPerfStart.QuadPart;
 }

ppmz2/Stopwatch.h

 #pragma once
 
+#include <windows.h>
+
 class Stopwatch
 {
 public:
-	Stopwatch();
+	__declspec(dllexport) Stopwatch();
 
-	void Start();
+	__declspec(dllexport) void Start();
 
-	__int64 Now() const;
-
-	__int64 NowInMicro() const;
+	__declspec(dllexport) __int64 Elapsed() const;
 
 private:
 	LARGE_INTEGER m_liPerfFreq;