Skip to content

Commit

Permalink
Performance improvements when serialize/deserialize big data
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyxo committed Nov 3, 2014
1 parent 13469e0 commit 1cf534d
Show file tree
Hide file tree
Showing 31 changed files with 3,671 additions and 3,537 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#Protocol Buffers for Swift

[![Platform](http://img.shields.io/badge/platform-ios%20%7C%20osx-green.svg)](https://github.com/alexeyxo/protobuf-swift)
[![Release](http://img.shields.io/github/tag/alexeyxo/protobuf-swift.svg)](https://github.com/alexeyxo/protobuf-swift/releases/tag/v1.0)

An implementation of Protocol Buffers in Swift.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0610"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3F85569819C71BCF003802F2"
BuildableName = "ProtocolBuffers.framework"
BlueprintName = "ProtocolBuffers"
ReferencedContainer = "container:ProtocolBuffers.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3F8556A319C71BCF003802F2"
BuildableName = "ProtocolBuffersTests.xctest"
BlueprintName = "UnitTesting"
ReferencedContainer = "container:ProtocolBuffers.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3F85569819C71BCF003802F2"
BuildableName = "ProtocolBuffers.framework"
BlueprintName = "ProtocolBuffers"
ReferencedContainer = "container:ProtocolBuffers.xcodeproj">
</BuildableReference>
</MacroExpansion>
</TestAction>
<LaunchAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3F85569819C71BCF003802F2"
BuildableName = "ProtocolBuffers.framework"
BlueprintName = "ProtocolBuffers"
ReferencedContainer = "container:ProtocolBuffers.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3F85569819C71BCF003802F2"
BuildableName = "ProtocolBuffers.framework"
BlueprintName = "ProtocolBuffers"
ReferencedContainer = "container:ProtocolBuffers.xcodeproj">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let BUFFER_SIZE:Int32 = 4096;

public class CodedInputStream
{
public var buffer:[Byte]!
public var buffer:[Byte]
private var input:NSInputStream!
private var bufferSize:Int32 = 0
private var bufferSizeAfterLimit:Int32 = 0
Expand All @@ -39,7 +39,7 @@ public class CodedInputStream
public init (data aData:[Byte])
{
buffer = aData
bufferSize = Int32(buffer!.count)
bufferSize = Int32(buffer.count)
currentLimit = INT_MAX
recursionLimit = DEFAULT_RECURSION_LIMIT
sizeLimit = DEFAULT_SIZE_LIMIT
Expand Down Expand Up @@ -87,7 +87,7 @@ public class CodedInputStream

if input != nil
{
bufferSize = Int32(input!.read(&buffer!, maxLength:buffer!.count))
bufferSize = Int32(input!.read(&buffer, maxLength:buffer.count))

}

Expand Down Expand Up @@ -131,30 +131,33 @@ public class CodedInputStream

if (size <= bufferSize - bufferPos) {

var data = [Byte](count: buffer!.count - Int(bufferPos), repeatedValue: 0)
data[0...data.count-1] = buffer![Int(bufferPos)...Int(buffer!.count-1)]
var data = [Byte](count: buffer.count - Int(bufferPos), repeatedValue: 0)
// data[0...data.count-1] = buffer![Int(bufferPos)...Int(buffer!.count-1)]
memcpy(&data, &buffer + Int(bufferPos), UInt(buffer.count - Int(bufferPos)))
bufferPos += size;
return data;
}
else if (size < BUFFER_SIZE) {

var bytes = [Byte](count:Int(size), repeatedValue: 0)
var pos:Int32 = bufferSize - bufferPos;
bytes[0...bytes.count-1] = buffer![Int(bufferPos)...Int(buffer!.count-1)]
// bytes[0...bytes.count-1] = buffer![Int(bufferPos)...Int(buffer!.count-1)]
memcpy(&bytes, &buffer + Int(bufferPos), UInt(pos))
bufferPos = bufferSize;

refillBuffer(true)

while (size - pos > bufferSize)
{

bytes[Int(pos)...Int(bufferSize)] = buffer![0...Int(bufferSize)]
memcpy(&bytes + Int(pos), &buffer, UInt(bufferSize))
// bytes[Int(pos)...Int(bufferSize)] = buffer![0...Int(bufferSize)]
pos += bufferSize
bufferPos = bufferSize
refillBuffer(true)
}

bytes[Int(pos)...Int(bufferSize)] = buffer![0...Int(size - pos)]
// bytes[Int(pos)...Int(bufferSize)] = buffer![0...Int(size - pos)]
memcpy(&bytes + Int(pos), &buffer, UInt(size - pos))
bufferPos = size - pos;
return bytes

Expand Down Expand Up @@ -198,18 +201,20 @@ public class CodedInputStream
var bytes:[Byte] = [Byte](count: Int(size), repeatedValue: 0)
var pos:Int = originalBufferSize - originalBufferPos;

bytes[0...bytes.count-1] = buffer![Int(originalBufferSize)...Int(pos)]

// bytes[0...bytes.count-1] = buffer[Int(originalBufferSize)...Int(pos)]
memcpy(&bytes, &buffer + Int(originalBufferPos), UInt(pos))
for chunk in chunks
{
bytes[Int(pos)..<bytes.count] = chunk[0..<chunk.count]
// bytes[Int(pos)..<bytes.count] = chunk[0..<chunk.count]
memcpy(&bytes + pos, chunk, UInt(chunk.count))
pos += chunk.count
}

return bytes

}
}



public func skipRawData(var size:Int32)
Expand Down Expand Up @@ -427,7 +432,7 @@ public class CodedInputStream
{
refillBuffer(true)
}
var res = buffer![Int(bufferPos++)]
var res = buffer[Int(bufferPos++)]
return res
}

Expand Down Expand Up @@ -497,8 +502,8 @@ public class CodedInputStream
var size:Int32 = readRawVarint32()
if (size <= (bufferSize - bufferPos) && size > 0)
{

var data = buffer[Int(bufferPos)..<Int(bufferPos+size)]
var data:[Byte] = [Byte](count: Int(size), repeatedValue: 0)
memcpy(&data, &buffer+Int(bufferPos), UInt(data.count))
var result:String = String(bytes: data, encoding: NSUTF8StringEncoding)!
bufferPos += size
return result
Expand All @@ -517,10 +522,10 @@ public class CodedInputStream
let size = readRawVarint32()
if (size < bufferSize - bufferPos && size > 0)
{
var result:[Byte] = [Byte](count: Int(size), repeatedValue: 0)
result[0..<result.count] = buffer![Int(bufferPos)...Int(size)]
bufferPos += size;
return result;
var data:[Byte] = [Byte](count: Int(size), repeatedValue: 0)
memcpy(&data, &buffer+Int(bufferPos), UInt(data.count))
bufferPos += size
return data
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ public class CodedOutputStream
writeRawData(data, offset:0, length: Int32(data.count))

}
public func writeRawData(data:[Byte], var offset aOffset:Int32, var length aLength:Int32)
public func writeRawData(data:[Byte], var offset:Int32, var length:Int32)
{
while (aLength > 0)
while (length > 0)
{
var written:Int32 = buffer.appendData(data, offset: aOffset, length: aLength)
aOffset += Int32(written)
aLength -= Int32(written)
if (written == 0 && aLength > 0)
var written:Int32 = buffer.appendData(data, offset: offset, length: length)
offset += Int32(written)
length -= Int32(written)
if (written == 0 || length > 0)
{
flush()
}
Expand Down Expand Up @@ -237,13 +237,13 @@ public class CodedOutputStream
writeMessageNoTag(value)
}

public func writeDataNoTag(data:[Byte]?)
public func writeDataNoTag(data:[Byte])
{
writeRawVarint32(Int32(data!.count))
writeRawData(data!)
writeRawVarint32(Int32(data.count))
writeRawData(data)
}

public func writeData(fieldNumber:Int32, value:[Byte]?)
public func writeData(fieldNumber:Int32, value:[Byte])
{
writeTag(fieldNumber, format: WireFormat.WireFormatLengthDelimited)
writeDataNoTag(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,17 @@ internal class RingBuffer
return true
}

func appendData(data:[Byte], var offset:Int32, var length:Int32) -> Int32
func appendData(var input:[Byte], var offset:Int32, var length:Int32) -> Int32
{
var totalWritten:Int32 = 0

if (position >= tail)
{
totalWritten = min(Int32(buffer.count) - Int32(position), Int32(length))
var subdata = data[Int(offset)..<data.count]
buffer[Int(position)..<(Int(position)+Int(totalWritten))] = subdata
memcpy(&buffer + Int(position), &input + Int(offset), UInt(totalWritten))
position += totalWritten
if totalWritten == length
{
return Int32(length)
return length
}
length -= Int32(totalWritten)
offset += Int32(totalWritten)
Expand All @@ -88,9 +86,7 @@ internal class RingBuffer
}

let written:Int32 = min(Int32(freeSpaces), length);

var subdata = data[Int(offset)..<data.count]
buffer[Int(position)...Int(written)] = subdata
memcpy(&buffer + Int(position), &input + Int(offset), UInt(written));
position += written
totalWritten += written

Expand All @@ -101,11 +97,10 @@ internal class RingBuffer
{
var totalWritten:Int32 = 0

var data = [Byte](count: Int(buffer.count - Int(tail)), repeatedValue: 0)
data[0..<data.count] = buffer[Int(tail)..<Int(buffer.count)]
var data = buffer
if tail > position
{
var written:Int = stream.write(&data, maxLength:Int(buffer.count - Int(tail)))
var written:Int = stream.write(&data + Int(tail), maxLength:Int(buffer.count - Int(tail)))
if written <= 0
{
return totalWritten
Expand All @@ -119,7 +114,7 @@ internal class RingBuffer

if (tail < position) {

var written:Int = stream.write(&data, maxLength:Int(position - tail))
var written:Int = stream.write(&data + Int(tail), maxLength:Int(position - tail))
if (written <= 0)
{
return totalWritten
Expand Down
Loading

0 comments on commit 1cf534d

Please sign in to comment.