SVG vs XML

The SVG component is very fast. I thought I would shave off some of its overhead by directly using XML commands in a loop to create squares. Below is the code that does that.

When using the SVG Component commands the loop is really fast. When the IfElse is False so XML is used, the loop is slower, and comes practically to a stop before completing when around 3000 squares are being created.

Any idea why using XML code would be slower?

		If (True)		
			$ref:=SVG_New_rect ($svg;$x;$y;$cSize;$cSize;0;0;"white";$obj.color)
			SVG_SET_ID ($ref;$name)
		Else 
			$ref:=DOM Create XML element($svg;"rect";"x";$x;"y";$y;"width";$cSize;\
			"height";$cSize;"stroke";"white";"fill";$obj.color;\
			"id";$name)
		End if 

Running a small sample below generates SVG code that looks right.

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><metadata xmlns="http://www.4D.com/4DSVG"><generator>4D SVG v18R3.104 ©4D SAS, 2008-2020.</generator><about>4D SVG is the quintessential vector drawing tool for 4D developers.</about><generation date="2020-06-10" system="macOS 10.14.6 (18G5033)" time="PT12H4M39S"/></metadata><defs id="4D"/>
<rect fill="black" height="40" id="sq1" stroke="white" width="40" x="1" y="1"/>
<rect fill="black" height="40" id="sq2" stroke="white" width="40" x="2" y="2"/>
<rect fill="black" height="40" id="sq3" stroke="white" width="40" x="3" y="3"/>
</svg>

---4D Code for the above SVG---
$svg:=SVG_New 
$x:=1
$y:=1
$cSize:=40
$ref:=SVG_New_rect ($svg;$x;$y;$cSize;$cSize;0;0;"white";"black")
SVG_SET_ID ($ref;"sq1")

$x:=2
$y:=2
$ref:=DOM Create XML element($svg;"rect";"x";$x;"y";$y;"width";$cSize;\
"height";$cSize;"stroke";"white";"fill";"black";\
"id";"sq2")

$x:=3
$y:=3
$ref:=DOM Create XML element($svg;"rect";"x";$x;"y";$y;"width";$cSize;\
"height";$cSize;"stroke";"white";"fill";"black";\
"id";"sq3")

$txt:=SVG_Export_to_XML ($svg)
OBJECT Get pointer(Object named;"pic")->:=SVG_Export_to_picture ($svg)
SVG_CLEAR ($svg)
SET TEXT TO PASTEBOARD($txt)

Thanks - Keith

without knowing why, I’d try to replace $obj.color with a $locale first. If it doesn’t change anything, I’d suspect some trick in the component to replace DOM Create XML element, because what you describe sounds like an increasingly difficult course. As a partner, I seem to remember the source code is available: no?
(I’m unable to remember where 4d components sources are, BTW)

Hi,

DOM Create XML element uses xpath. Maybe that is the slow part…

Can you try using this instead and let us know ?

$ref:=DOM Append XML child node($svg;XML ELEMENT;"rect")
DOM SET XML ATTRIBUTE($ref;"x";$x;\
"y";$y;\
"width";$cSize;\
"height";$cSize;\
"stroke";"white";\
"fill";"black";\
"id";"sq4")
1 Like
1 Like

I took the code to create a rect from the SVG source, and stopped investigating as soon as I saw “…:=DOM Create XML element($svg;“rect”;“x”…”. I can’t find the word “child” in the source of the “SVG_New_rect.4dm” text. It does create only the first four attributes on that line of code. The rest are added later using “DOM SET XML ATTRIBUTE”.

I was going to substitute a local for the object, but the attribute is used only once in the loop, and can change with each interation.

The substitute code appending the child node works, and is noticably faster. I would not have guessed this alternate approach.

Thanks for the help.

Keith

1 Like